C # bar code generation (2) -- Code128 abstract class

Source: Internet
Author: User

Code128 abstract class, the design idea is to use a template, C # Drawing is still unfamiliar, began to use a Pen to draw a vertical bar, and then the figure scanner did not recognize, finally, based on the existing code writing method on the Internet and using the SetPixel method, the generated Code128 code scanner can be correctly read, but the bar code size standards have not been followed and need to be improved, after finishing the GS1-128 part, it will modify this part of the Image Code

/// <Summary> // Code128 abstract class /// </summary> public abstract class absCode128: IBarCode {protected string _ encodedData; // encode the data protected string _ rawData; // original data protected string _ presentationData = null; // The data displayed to people under the bar code. If it is null, obtain the original data protected bool _ dataDisplay = true; // whether the font protected byte _ barCellWidth = 1; // The unit width of the module. The unit is Pix. The default value is 1 protected bool _ showBlank = true; // whether the left and right white spaces are displayed. Iple = 10; // The multiples of the horizontal left and right blank corresponding to the module protected byte _ verticalMulriple = 8; // The multiples of the vertical up and down blank corresponding to the module protected byte _ barHeight = 32; // bar code height. Unit: 32 by default. protected Color _ backColor = Color. white; // bar code background Color protected Color _ barColor = Color. black; // bar code color protected byte _ fontPadding; // The space interval between the font and the bar code protected float _ emSize; // The font size. protected FontFamily _ fontFamily; // font style protected FontStyle _ fontStyle; // font style protected StringAlignment _ TextAlignment; // font layout location protected Color _ fontColor; // font Color: protected bool _ fontPositionOnBottom; // whether the font position is at the bottom. If not, then at the top, /// <summary> /// current barcode type /// </summary> public string BarCodeType {get {return System. reflection. methodBase. getCurrentMethod (). declaringType. name ;}//< summary> /// raw data /// </summary> public string RawData {get {return this. _ rawData ;}/// <summary> // bar code display data /// </sum Mary> public string PresentationData {get {return string. IsNullOrEmpty (this. _ presentationData )? This. _ rawData: this. _ presentationData ;}/// <summary> /// encode data corresponding to the barcode /// </summary> public string EncodedData {get {return this. _ encodedData ;}//< summary> /// whether to display the data on the barcode /// </summary> public bool DataDisplay {get {return this. _ dataDisplay;} set {this. _ dataDisplay = value ;}/// <summary> // bar code height, which must be at least 0.15 times the bar code width or 6.35mm, whichever the two are larger, /// the default value is 32, in the unit of mm /// </summary> public byte BarHei Ght {get {return this. _ barHeight;} set {this. _ barHeight = value ;}/// <summary> // unit of module width: pix // default width 1pix /// </summary> public byte BarCellWidth {get {return this. _ barCellWidth;} set {if (value = 0) {this. _ barCellWidth = 1;} else {this. _ barCellWidth = value ;}}/// <summary> /// whether the Left and Right spaces are displayed, standard Display by default /// </summary> public bool ShowBlank {get {return this. _ showBlank;} set {this. _ ShowBlank = value ;}/// <summary> /// a multiple of the width of the left and right blank corresponding to the module. The minimum value of the international standard is 10. If the value is lower than 10, take 10 /// </summary> public byte HorizontalMulriple {get {return this. _ horizontalMulriple;} set {if (value <10) {this. _ horizontalMulriple = 10;} else {this. _ horizontalMulriple = value ;}}/// <summary> // horizontal blank pix /// </summary> public int HorizontalMargin {get {if (this. showBlank) {return this. _ barCellWidth * t His. _ horizontalMulriple;} else {return 0 ;}}} /// <summary> /// multiples of the upper and lower vertical spaces corresponding to the module /// </summary> public byte VerticalMulriple {get {return this. _ verticalMulriple;} set {this. _ verticalMulriple = value ;}/// <summary> /// vertical blank /// </summary> public int VerticalMargin {get {if (this. showBlank) {return this. _ barCellWidth * this. _ verticalMulriple;} else {return 0 ;}}/// <summary> // font The space interval from the barcode. Unit: Pix // </summary> public byte FontPadding {get {return this. _ fontPadding;} set {this. _ fontPadding = value ;}/// <summary> // font size /// </summary> public float FontSize {get {return this. _ emSize;} set {this. _ emSize = value ;}/// <summary> // font style /// </summary> public FontFamily {get {return this. _ fontFamily;} set {this. _ fontFamily = value ;}/// <summar Y> // font style /// </summary> public FontStyle {get {return this. _ fontStyle;} set {this. _ fontStyle = value ;}/// <summary> // font layout position /// </summary> public StringAlignment TextAlignment {get {return this. _ textAlignment;} set {this. _ textAlignment = value ;}/// <summary> // font Color /// </summary> public Color FontColor {get {return this. _ fontColor;} set {this. _ fontColor = valu E ;}} public absCode128 (string rawData) {this. _ rawData = rawData; if (string. isNullOrEmpty (this. _ rawData) {throw new Exception ("empty string cannot generate barcode");} this. _ rawData = this. _ rawData. trim (); if (! This. rawDataCheck () {throw new Exception (rawData + "noncompliant" + this. barCodeType + "standard");} this. _ encodedData = this. getEncodedData (); // whether to add the maximum number of characters that can be encoded exceeds the standard 48. It seems that the maximum number of characters in EAN128 must not exceed 48 this. fontInit () ;}/// <summary> // font initialization /// </summary> private void FontInit () {this. _ fontPadding = 4; this. _ emSize = 12; this. _ fontFamily = new FontFamily ("Times New Roman"); this. _ fontStyle = FontStyle. regular; this. _ TextAlignment = StringAlignment. center; this. _ fontColor = Color. black;} protected int GetBarCodePhyWidth () {// In the BS unit such as 212222, to calculate the ratio of the module width corresponding to the bsGroup, the total length should be reduced by 1 (because the Stop length is 7), and then the result is multiplied by 11 and divided by 6, add 2 (Stop is two more module groups than normal BS) after adding the Left and Right spaces. int bsNum = (this. _ encodedData. length-1) * 11/6 + 2; // + (this. _ showBlank? This. _ blankMulriple * 2: 0) return bsNum * this. _ barCellWidth ;} /// <summary> /// verify the correctness of data input /// </summary> /// <returns> </returns> protected abstract bool RawDataCheck (); /// <summary> /// obtain the encoding Data corresponding to the current Data (empty combination) /// </summary> /// <returns> </returns> protected abstract string GetEncodedData (); # region logout //// <summary> //// obtain the image corresponding to the barcode, graphics drawing cannot be read //// </summary> //// <returns> </returns>/ /Public Image GetBarCodeImage () // {// float x, y; // x = this. _ blockWidth * this. _ blankMulriple; // y = x; // Bitmap image = new Bitmap (int) this. getBarCodePhyWidth () + 1, (int) (this. _ barHeight + y * 2) + 1); // Graphics g = Graphics. fromImage (image); // g. clear (this. _ backColor); // clear the background // g. smoothingMode = System. drawing. drawing2D. smoothingMode. antiAlias; // g. pageUnit = GraphicsUnit. display ;// Pen p = new Pen (this. _ barColor); // for (int I = 0; I <this. _ encodedData. length; I ++) // {// byte num = (byte) char. getNumericValue (this. _ encodedData [I]); // if (I % 2 = 0) // {// The even bits are bar and the odd bits are sp, except for the last Stop, each other character is 6 Characters // p. width = num * this. _ blockWidth; // g. drawLine (p, x, y, x, y + this. _ barHeight); //} // x + = num * this. _ blockWidth; //} // System. IO. memoryStream MS = new System. IO. memory Stream (); // image. save (MS, System. drawing. imaging. imageFormat. jpeg); // end plotting // p. dispose (); // g. dispose (); // image. dispose (); // return Image. fromStream (MS ); //}# endregion // <summary> // obtain the complete barcode /// </summary> /// <returns> </returns> public Image GetBarCodeImage () {Image barImage = this. getBarOnlyImage (); int width = barImage. width; int height = barImage. height; width + = this. horizonta LMargin * 2; height + = this. verticalMargin * 2; if (this. _ dataDisplay) {height + = this. _ fontPadding + (int) this. _ emSize;} Image image = new Bitmap (width, height); Graphics g = Graphics. fromImage (image); g. clear (this. _ backColor); g. drawImage (barImage, this. horizontalMargin, this. verticalMargin, barImage. width, barImage. height); if (this. _ dataDisplay) {Font drawFont = new Font (this. _ fontFamily, t His. _ emSize, this. _ fontStyle, GraphicsUnit. pixel); Brush drawBrush = new SolidBrush (this. _ fontColor); StringFormat drawFormat = new StringFormat (); drawFormat. alignment = this. _ textAlignment; RectangleF reF = new RectangleF (0, barImage. height + this. verticalMargin + this. _ fontPadding, width, this. _ emSize); g. drawString (this. presentationData, drawFont, drawBrush, reF, drawFormat); drawFont. dispose (); DrawBrush. dispose (); drawFormat. dispose ();} System. IO. memoryStream MS = new System. IO. memoryStream (); image. save (MS, System. drawing. imaging. imageFormat. jpeg); // end the painting g. dispose (); image. dispose (); return Image. fromStream (MS );} /// <summary> /// obtain the Image containing only the barcode /// </summary> /// <returns> </returns> private Image GetBarOnlyImage () {int width = (int) this. getBarCodePhyWidth (); Bitmap image = new Bit Map (width, this. _ barHeight); int ptr = 0; for (int I = 0; I <this. _ encodedData. length; I ++) {int w = (int) char. getNumericValue (this. _ encodedData [I]); w * = this. _ barCellWidth; Color c = I % 2 = 0? This. _ barColor: this. _ backColor; for (int j = 0; j <w; j ++) {for (int h = 0; h <this. _ barHeight; h ++) {image. setPixel (ptr, h, c) ;}ptr ++ ;}} return image ;}}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.