Generating a PDF417 barcode from a Byte array in c#

ぐ巨炮叔叔 提交于 2019-12-11 06:55:38

问题


I need a way of generating PDF417 barcodes from byte arrays.

I have found numerous SDKs that can generate barcodes but all of them expect a string as the data instead of a byte array, unfortunately this isn't a possibility since I need to conform to a pre-existing standard

Thanks in advance

Code that I have tried, as requested by Wyatt Earp.

    /// <summary>
    /// Writes the barcode data to a specified location
    /// </summary>
    /// <param name="data">Data of the barcode</param>
    /// <param name="Location">Location to save barcode</param>
    public void Write(byte[] data, string Location)
    {
        ///* Keep Automation Barcode Creator
        KeepAutomation.Barcode.Crystal.BarCode KABarcode = new KeepAutomation.Barcode.Crystal.BarCode();
        KABarcode.Symbology = KeepAutomation.Barcode.Symbology.PDF417;
        KABarcode.PDF417DataMode = KeepAutomation.Barcode.PDF417DataMode.Auto;
        KABarcode.CodeToEncode = data;
        KABarcode.ImageFormat = System.Drawing.Imaging.ImageFormat.Png;
        KABarcode.generateBarcodeToImageFile(Location);
        //*/

        ///* BarcodeLib Creator
        BarcodeLib.Barcode.PDF417 barcodeLibBar = new BarcodeLib.Barcode.PDF417();
        barcodeLibBar.Data = data;
        var BarLibImage = barcodeLibBar.drawBarcode();
        BarLibImage.Save(Location);
        //*/

        ///* PQScan.Barcode Creator
        PQScan.BarcodeCreator.Barcode PQScanBarcode = new PQScan.BarcodeCreator.Barcode();
        PQScanBarcode.BarType = PQScan.BarcodeCreator.BarCodeType.PDF417;
        PQScanBarcode.Data = data;
        PQScanBarcode.PictureFormat = System.Drawing.Imaging.ImageFormat.Png;
        var PQScanImage = PQScanBarcode.CreateBarcode();
        PQScanImage.Save(Location);
        //*/
    }          

None of these will build since they all expect strings as the barcode data, I need to give them a byte[]

Unfortunately I already deleted the code from the other SDK's but all of them tend to follow the same pattern.

Comprehensive list of SDK's I have tried are:

  • SharpPdf417
  • ZXing
  • PQScan.BarcodeCreator
  • TBarcode
  • KeepAutomation.Barcode
  • BarcodeLib

All of these SDKs only accept a string as the barcode data, I need the byte array that is read to be the byte array that is inputted into this void,

I am unable to give you the exact byte array, but it has a size of 454 and uses several different encoding methods throughout the byte array.


回答1:


Well, you could convert the byte array to an ASCII string, and use it to create the barcode. The consumer of this barcode will need to know that the string is encoded as ASCII in order to convert back. For example:

/// <summary>
/// Writes the barcode data to a specified location
/// </summary>
/// <param name="data">Data of the barcode</param>
/// <param name="Location">Location to save barcode</param>
public void Write(byte[] data, string Location)
{
    ///* Keep Automation Barcode Creator
    KeepAutomation.Barcode.Crystal.BarCode KABarcode = new KeepAutomation.Barcode.Crystal.BarCode();
    KABarcode.Symbology = KeepAutomation.Barcode.Symbology.PDF417;
    KABarcode.PDF417DataMode = KeepAutomation.Barcode.PDF417DataMode.Auto;
    KABarcode.CodeToEncode = System.Text.Encoding.ASCII.GetString(data);
    KABarcode.ImageFormat = System.Drawing.Imaging.ImageFormat.Png;
    KABarcode.generateBarcodeToImageFile(Location);
    //*/

    ///* BarcodeLib Creator
    BarcodeLib.Barcode.PDF417 barcodeLibBar = new BarcodeLib.Barcode.PDF417();
    barcodeLibBar.Data = System.Text.Encoding.ASCII.GetString(data);
    var BarLibImage = barcodeLibBar.drawBarcode();
    BarLibImage.Save(Location);
    //*/

    ///* PQScan.Barcode Creator
    PQScan.BarcodeCreator.Barcode PQScanBarcode = new PQScan.BarcodeCreator.Barcode();
    PQScanBarcode.BarType = PQScan.BarcodeCreator.BarCodeType.PDF417;
    PQScanBarcode.Data = System.Text.Encoding.ASCII.GetString(data);
    PQScanBarcode.PictureFormat = System.Drawing.Imaging.ImageFormat.Png;
    var PQScanImage = PQScanBarcode.CreateBarcode();
    PQScanImage.Save(Location);
    //*/
}  



回答2:


Thanks, but Since I needed to keep it in the format it already was in Wyatt Earps Answer didn't work for me but it does seem like it would work for others.

For me I managed to find that Aspose.Barcode had a feature that would allow me to generate barcodes straight from the byte array instead of having to convert it.

Below is the code I use incase it is of interest to anyone

    /// <summary>
    /// Writes the barcode data to a specified location
    /// </summary>
    /// <param name="data">Byte data of barcode</param>
    /// <param name="Location">Location to save barcode</param>
    public void Write(byte[] data, string location)
    {
        //Define the barcode builder with properties
        BarCodeBuilder builder = new BarCodeBuilder()
        {
            SymbologyType = Symbology.Pdf417,
            Rows = 30
        };

        //Set Data
        builder.SetBinaryCodeText(data);

        //Generate Barcode
        var barcodeBitmap = builder.GenerateBarCodeImage();

        //Save it to disk
        barcodeBitmap.Save(location);
    }                    


来源:https://stackoverflow.com/questions/39451577/generating-a-pdf417-barcode-from-a-byte-array-in-c-sharp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!