public static void GetBarcode(int height, int width, BarcodeLib.TYPE type, string code, out System.Drawing.Image image, string fileSaveUrl) { try { image = null; BarcodeLib.Barcode b = new BarcodeLib.Barcode(); b.BackColor = System.Drawing.Color.White;//图片背景颜色 b.ForeColor = System.Drawing.Color.Black;//条码颜色 b.IncludeLabel = true; b.Alignment = BarcodeLib.AlignmentPositions.LEFT; b.LabelPosition = BarcodeLib.LabelPositions.BOTTOMCENTER;//code的显示位置 b.ImageFormat = System.Drawing.Imaging.ImageFormat.Jpeg;//图片格式 System.Drawing.Font font = new System.Drawing.Font("verdana", 10f);//字体设置 b.LabelFont = font; b.Height = height;//图片高度设置(px单位) b.Width = width;//图片宽度设置(px单位) image = b.Encode(type, code);//生成图片 image.Save(fileSaveUrl, System.Drawing.Imaging.ImageFormat.Jpeg); } catch (Exception err) { err.ToString(); image = null; } }
/// <summary> /// Encode the raw data using the JAN-13 algorithm. /// </summary> private string Encode_JAN13() { if (!Raw_Data.StartsWith("49")) Error("EJAN13-1: Invalid Country Code for JAN13 (49 required)"); if (!BarcodeLib.CheckNumericOnly(Raw_Data)) Error("EJAN13-2: Numeric Data Only"); EAN13 ean13 = new EAN13(Raw_Data); return ean13.Encoded_Value; }//Encode_JAN13
private static byte[] GetBarCode(string code, int w, int h, string title) { var bcl = new BarcodeLib(code, BarCodeType.ISBN) { Width = w, Height = h, ForeColor = Color.Black, BackColor = Color.White, ImageFormat = ImageFormat.Png, IncludeLabel = true, LabelPosition = LabelPositions.TOPCENTER, RawData = title }; bcl.Encode(); return(bcl.GetImageData(SaveTypes.PNG)); }
}//Standard2of5 /// <summary> /// Encode the raw data using the Standard 2 of 5 algorithm. /// </summary> private string Encode_Standard2of5() { if (!BarcodeLib.CheckNumericOnly(Raw_Data)) { Error("ES25-1: Numeric Data Only"); } string result = "11011010"; foreach (char c in Raw_Data) { result += S25_Code[Int32.Parse(c.ToString())]; }//foreach //add ending bars result += "1101011"; return(result); }//Encode_Standard2of5
/// <summary> /// Encode the raw data using the UPC Supplemental 2-digit algorithm. /// </summary> private string Encode_UPCSupplemental_2() { if (Raw_Data.Length != 2) { Error("EUPC-SUP2-1: Invalid data length. (Length = 2 required)"); } if (!BarcodeLib.CheckNumericOnly(Raw_Data)) { Error("EUPC-SUP2-2: Numeric Data Only"); } string pattern = ""; try { pattern = this.UPC_SUPP_2[Int32.Parse(Raw_Data.Trim()) % 4]; }//try catch { Error("EUPC-SUP2-3: Invalid Data. (Numeric only)"); } string result = "1011"; int pos = 0; foreach (char c in pattern) { if (c == 'a') { //encode using odd parity result += EAN_CodeA[Int32.Parse(Raw_Data[pos].ToString())]; }//if else if (c == 'b') { //encode using even parity result += EAN_CodeB[Int32.Parse(Raw_Data[pos].ToString())]; }//else if if (pos++ == 0) { result += "01"; //Inter-character separator } }//foreach return(result); }//Encode_UPSSupplemental_2
/// <summary> /// Encode the raw data using the Bookland/ISBN algorithm. /// </summary> private string Encode_ISBN_Bookland() { if (!BarcodeLib.CheckNumericOnly(Raw_Data)) { Error("EBOOKLANDISBN-1: Numeric Data Only"); } string type = "UNKNOWN"; if (Raw_Data.Length == 10 || Raw_Data.Length == 9) { if (Raw_Data.Length == 10) { Raw_Data = Raw_Data.Remove(9, 1); } Raw_Data = "978" + Raw_Data; type = "ISBN"; }//if else if (Raw_Data.Length == 12 && Raw_Data.StartsWith("978")) { type = "BOOKLAND-NOCHECKDIGIT"; }//else if else if (Raw_Data.Length == 13 && Raw_Data.StartsWith("978")) { type = "BOOKLAND-CHECKDIGIT"; Raw_Data = Raw_Data.Remove(12, 1); }//else if //check to see if its an unknown type if (type == "UNKNOWN") { Error("EBOOKLANDISBN-2: Invalid input. Must start with 978 and be length must be 9, 10, 12, 13 characters."); } EAN13 ean13 = new EAN13(Raw_Data); return(ean13.Encoded_Value); }//Encode_ISBN_Bookland
/// <summary> /// Encode the raw data using the EAN-8 algorithm. /// </summary> private string Encode_EAN8() { //check length if (Raw_Data.Length != 8 && Raw_Data.Length != 7) { Error("EEAN8-1: Invalid data length. (7 or 8 numbers only)"); } //check numeric only if (!BarcodeLib.CheckNumericOnly(Raw_Data)) { Error("EEAN8-2: Numeric only."); } //encode the data string result = "101"; //first half (Encoded using left hand / odd parity) for (int i = 0; i < Raw_Data.Length / 2; i++) { result += EAN_CodeA[Int32.Parse(Raw_Data[i].ToString())]; }//for //center guard bars result += "01010"; //second half (Encoded using right hand / even parity) for (int i = Raw_Data.Length / 2; i < Raw_Data.Length; i++) { result += EAN_CodeC[Int32.Parse(Raw_Data[i].ToString())]; }//for result += "101"; return(result); }//Encode_EAN8
}//UPCE /// <summary> /// Encode the raw data using the UPC-E algorithm. /// </summary> private string Encode_UPCE() { if (Raw_Data.Length != 6 && Raw_Data.Length != 8 && Raw_Data.Length != 12) { Error("EUPCE-1: Invalid data length. (8 or 12 numbers only)"); } if (!BarcodeLib.CheckNumericOnly(Raw_Data)) { Error("EUPCE-2: Numeric only."); } int CheckDigit = Int32.Parse(Raw_Data[Raw_Data.Length - 1].ToString()); int NumberSystem = Int32.Parse(Raw_Data[0].ToString()); //Convert to UPC-E from UPC-A if necessary if (Raw_Data.Length == 12) { string UPCECode = ""; //break apart into components string Manufacturer = Raw_Data.Substring(1, 5); string ProductCode = Raw_Data.Substring(6, 5); //check for a valid number system if (NumberSystem != 0 && NumberSystem != 1) { Error("EUPCE-3: Invalid Number System (only 0 & 1 are valid)"); } if (Manufacturer.EndsWith("000") || Manufacturer.EndsWith("100") || Manufacturer.EndsWith("200") && Int32.Parse(ProductCode) <= 999) { //rule 1 UPCECode += Manufacturer.Substring(0, 2); //first two of manufacturer UPCECode += ProductCode.Substring(2, 3); //last three of product UPCECode += Manufacturer[2].ToString(); //third of manufacturer }//if else if (Manufacturer.EndsWith("00") && Int32.Parse(ProductCode) <= 99) { //rule 2 UPCECode += Manufacturer.Substring(0, 3); //first three of manufacturer UPCECode += ProductCode.Substring(3, 2); //last two of product UPCECode += "3"; //number 3 }//else if else if (Manufacturer.EndsWith("0") && Int32.Parse(ProductCode) <= 9) { //rule 3 UPCECode += Manufacturer.Substring(0, 4); //first four of manufacturer UPCECode += ProductCode[4]; //last digit of product UPCECode += "4"; //number 4 }//else if else if (!Manufacturer.EndsWith("0") && Int32.Parse(ProductCode) <= 9 && Int32.Parse(ProductCode) >= 5) { //rule 4 UPCECode += Manufacturer; //manufacturer UPCECode += ProductCode[4]; //last digit of product }//else if else { Error("EUPCE-4: Illegal UPC-A entered for conversion. Unable to convert."); } Raw_Data = UPCECode; }//if //get encoding pattern string pattern = ""; if (NumberSystem == 0) { pattern = UPCE_Code_0[CheckDigit]; } else { pattern = UPCE_Code_1[CheckDigit]; } //encode the data string result = "101"; int pos = 0; foreach (char c in pattern) { int i = Int32.Parse(Raw_Data[pos++].ToString()); if (c == 'a') { result += EAN_CodeA[i]; }//if else if (c == 'b') { result += EAN_CodeB[i]; } //else if } //foreach //guard bars result += "01010"; //end bars result += "1"; return(result); }//Encode_UPCE
public BarcodeRow AddBarcodeRow( string Type, string RawData, string EncodedValue, double EncodingTime, bool IncludeLabel, string Forecolor, string Backcolor, string CountryAssigningManufacturingCode, int ImageWidth, int ImageHeight, string Image, System.Drawing.RotateFlipType RotateFlipType, BarcodeLib.LabelPositions LabelPosition, BarcodeLib.AlignmentPositions Alignment, string LabelFont, string ImageFormat) { BarcodeRow rowBarcodeRow = ((BarcodeRow)(this.NewRow())); object[] columnValuesArray = new object[] { Type, RawData, EncodedValue, EncodingTime, IncludeLabel, Forecolor, Backcolor, CountryAssigningManufacturingCode, ImageWidth, ImageHeight, Image, RotateFlipType, LabelPosition, Alignment, LabelFont, ImageFormat}; rowBarcodeRow.ItemArray = columnValuesArray; this.Rows.Add(rowBarcodeRow); return rowBarcodeRow; }
/// <summary> /// Encode the raw data using the UPC Supplemental 5-digit algorithm. /// </summary> private string Encode_UPCSupplemental_5() { if (Raw_Data.Length != 5) { Error("EUPC-SUP5-1: Invalid data length. (Length = 5 required)"); } if (!BarcodeLib.CheckNumericOnly(Raw_Data)) { Error("EUPCA-2: Numeric Data Only"); } //calculate the checksum digit int even = 0; int odd = 0; //odd for (int i = 0; i <= 4; i += 2) { odd += Int32.Parse(Raw_Data.Substring(i, 1)) * 3; }//for //even for (int i = 1; i < 4; i += 2) { even += Int32.Parse(Raw_Data.Substring(i, 1)) * 9; }//for int total = even + odd; int cs = total % 10; string pattern = UPC_SUPP_5[cs]; string result = ""; int pos = 0; foreach (char c in pattern) { //Inter-character separator if (pos == 0) { result += "1011"; } else { result += "01"; } if (c == 'a') { //encode using odd parity result += EAN_CodeA[Int32.Parse(Raw_Data[pos].ToString())]; }//if else if (c == 'b') { //encode using even parity result += EAN_CodeB[Int32.Parse(Raw_Data[pos].ToString())]; } //else if pos++; } //foreach return(result); } //Encode_UPCSupplemental_5
}//Code11 /// <summary> /// Encode the raw data using the Code 11 algorithm. /// </summary> private string Encode_Code11() { if (!BarcodeLib.CheckNumericOnly(Raw_Data.Replace("-", ""))) { Error("EC11-1: Numeric data and '-' Only"); } //calculate the checksums int weight = 1; int CTotal = 0; string Data_To_Encode_with_Checksums = Raw_Data; //figure the C checksum for (int i = Raw_Data.Length - 1; i >= 0; i--) { //C checksum weights go 1-10 if (weight == 10) { weight = 1; } if (Raw_Data[i] != '-') { CTotal += Int32.Parse(Raw_Data[i].ToString()) * weight++; } else { CTotal += 10 * weight++; } }//for int checksumC = CTotal % 11; Data_To_Encode_with_Checksums += checksumC.ToString(); //K checksums are recommended on any message length greater than or equal to 10 if (Raw_Data.Length >= 1) { weight = 1; int KTotal = 0; //calculate K checksum for (int i = Data_To_Encode_with_Checksums.Length - 1; i >= 0; i--) { //K checksum weights go 1-9 if (weight == 9) { weight = 1; } if (Data_To_Encode_with_Checksums[i] != '-') { KTotal += Int32.Parse(Data_To_Encode_with_Checksums[i].ToString()) * weight++; } else { KTotal += 10 * weight++; } }//for int checksumK = KTotal % 11; Data_To_Encode_with_Checksums += checksumK.ToString(); }//if //encode data string space = "0"; string result = C11_Code[11] + space; //start-stop char + interchar space foreach (char c in Data_To_Encode_with_Checksums) { int index = (c == '-' ? 10 : Int32.Parse(c.ToString())); result += C11_Code[index]; //inter-character space result += space; }//foreach //stop bars result += C11_Code[11]; return(result); }//Encode_Code11
}//MSI /// <summary> /// Encode the raw data using the MSI algorithm. /// </summary> private string Encode_MSI() { //check for non-numeric chars if (!BarcodeLib.CheckNumericOnly(Raw_Data)) { Error("EMSI-1: Numeric Data Only"); } string PreEncoded = Raw_Data; //get checksum if (Encoded_Type == BarCodeType.MSI_Mod10 || Encoded_Type == BarCodeType.MSI_2Mod10) { string odds = ""; string evens = ""; for (int i = PreEncoded.Length - 1; i >= 0; i -= 2) { odds = PreEncoded[i].ToString() + odds; if (i - 1 >= 0) { evens = PreEncoded[i - 1].ToString() + evens; } }//for //multiply odds by 2 odds = Convert.ToString((Int32.Parse(odds) * 2)); int evensum = 0; int oddsum = 0; foreach (char c in evens) { evensum += Int32.Parse(c.ToString()); } foreach (char c in odds) { oddsum += Int32.Parse(c.ToString()); } int checksum = 10 - ((oddsum + evensum) % 10); PreEncoded += checksum.ToString(); }//if if (Encoded_Type == BarCodeType.MSI_Mod11 || Encoded_Type == BarCodeType.MSI_Mod11_Mod10) { int sum = 0; int weight = 2; for (int i = PreEncoded.Length - 1; i >= 0; i--) { if (weight > 7) { weight = 2; } sum += Int32.Parse(PreEncoded[i].ToString()) * weight++; }//foreach int checksum = 11 - (sum % 11); PreEncoded += checksum.ToString(); }//else if (Encoded_Type == BarCodeType.MSI_2Mod10 || Encoded_Type == BarCodeType.MSI_Mod11_Mod10) { //get second check digit if 2 mod 10 was selected or Mod11/Mod10 string odds = ""; string evens = ""; for (int i = PreEncoded.Length - 1; i >= 0; i -= 2) { odds = PreEncoded[i].ToString() + odds; if (i - 1 >= 0) { evens = PreEncoded[i - 1].ToString() + evens; } }//for //multiply odds by 2 odds = Convert.ToString((Int32.Parse(odds) * 2)); int evensum = 0; int oddsum = 0; foreach (char c in evens) { evensum += Int32.Parse(c.ToString()); } foreach (char c in odds) { oddsum += Int32.Parse(c.ToString()); } int checksum = 10 - ((oddsum + evensum) % 10); PreEncoded += checksum.ToString(); }//if string result = "110"; foreach (char c in PreEncoded) { result += MSI_Code[Int32.Parse(c.ToString())]; }//foreach //add stop character result += "1001"; return(result); }//Encode_MSI
/// <summary> /// Encode the raw data using the ITF-14 algorithm. /// </summary> private string Encode_ITF14() { //check length of input if (Raw_Data.Length > 14 || Raw_Data.Length < 13) { Error("EITF14-1: Data length invalid. (Length must be 13 or 14)"); } if (!BarcodeLib.CheckNumericOnly(Raw_Data)) { Error("EITF14-2: Numeric data only."); } string result = "1010"; for (int i = 0; i < Raw_Data.Length; i += 2) { bool bars = true; string patternbars = ITF14_Code[Int32.Parse(Raw_Data[i].ToString())]; string patternspaces = ITF14_Code[Int32.Parse(Raw_Data[i + 1].ToString())]; string patternmixed = ""; //interleave while (patternbars.Length > 0) { patternmixed += patternbars[0].ToString() + patternspaces[0].ToString(); patternbars = patternbars.Substring(1); patternspaces = patternspaces.Substring(1); }//while foreach (char c1 in patternmixed) { if (bars) { if (c1 == 'N') { result += "1"; } else { result += "11"; } }//if else { if (c1 == 'N') { result += "0"; } else { result += "00"; } } //else bars = !bars; } //foreach } //foreach //add ending bars result += "1101"; return(result); }//Encode_ITF14
/// <summary> /// Encode the raw data using the UPC-A algorithm. /// </summary> private string Encode_UPCA() { //check length of input if (Raw_Data.Length != 11 && Raw_Data.Length != 12) { Error("EUPCA-1: Data length invalid. (Length must be 11 or 12)"); } if (!BarcodeLib.CheckNumericOnly(Raw_Data)) { Error("EUPCA-2: Numeric Data Only"); } CheckDigit(); string result = "101"; //start with guard bars //first number result += UPC_CodeA[Int32.Parse(Raw_Data[0].ToString())]; //second (group) of numbers int pos = 0; while (pos < 5) { result += UPC_CodeA[Int32.Parse(Raw_Data[pos + 1].ToString())]; pos++; }//while //add divider bars result += "01010"; //third (group) of numbers pos = 0; while (pos < 5) { result += UPC_CodeB[Int32.Parse(Raw_Data[(pos++) + 6].ToString())]; }//while //forth result += UPC_CodeB[Int32.Parse(Raw_Data[Raw_Data.Length - 1].ToString())]; //add ending guard bars result += "101"; //get the manufacturer assigning country this.init_CountryCodes(); string twodigitCode = "0" + Raw_Data.Substring(0, 1); try { _Country_Assigning_Manufacturer_Code = CountryCodes[twodigitCode].ToString(); }//try catch { Error("EUPCA-3: Country assigning manufacturer code not found."); }//catch finally { CountryCodes.Clear(); } return(result); }//Encode_UPCA
/// <summary> /// Encode the raw data using the EAN-13 algorithm. (Can include the checksum already. If it doesnt exist in the data then it will calculate it for you. Accepted data lengths are 12 + 1 checksum or just the 12 data digits) /// </summary> private string Encode_EAN13() { //check length of input if (Raw_Data.Length < 12 || Raw_Data.Length > 13) { Error("EEAN13-1: Data length invalid. (Length must be 12 or 13)"); } if (!BarcodeLib.CheckNumericOnly(Raw_Data)) { Error("EEAN13-2: Numeric Data Only"); } string patterncode = EAN_Pattern[Int32.Parse(Raw_Data[0].ToString())]; string result = "101"; //first //result += EAN_CodeA[Int32.Parse(RawData[0].ToString())]; //second int pos = 0; while (pos < 6) { if (patterncode[pos] == 'a') { result += EAN_CodeA[Int32.Parse(Raw_Data[pos + 1].ToString())]; } if (patterncode[pos] == 'b') { result += EAN_CodeB[Int32.Parse(Raw_Data[pos + 1].ToString())]; } pos++; }//while //add divider bars result += "01010"; //get the third pos = 1; while (pos <= 5) { result += EAN_CodeC[Int32.Parse(Raw_Data[(pos++) + 6].ToString())]; }//while //checksum digit int cs = Int32.Parse(Raw_Data[Raw_Data.Length - 1].ToString()); //add checksum result += EAN_CodeC[cs]; //add ending bars result += "101"; //get the manufacturer assigning country init_CountryCodes(); _Country_Assigning_Manufacturer_Code = "N/A"; string twodigitCode = Raw_Data.Substring(0, 2); string threedigitCode = Raw_Data.Substring(0, 3); try { _Country_Assigning_Manufacturer_Code = CountryCodes[threedigitCode].ToString(); }//try catch { try { _Country_Assigning_Manufacturer_Code = CountryCodes[twodigitCode].ToString(); }//try catch { Error("EEAN13-3: Country assigning manufacturer code not found."); } //catch } //catch finally { CountryCodes.Clear(); } return(result); }//Encode_EAN13