barnhill / barcodelib

C# Barcode Image Generation Library
Apache License 2.0
731 stars 239 forks source link

Access Encoded Value Without Running Through Image Generation #70

Closed unitedsoftwork closed 5 years ago

unitedsoftwork commented 5 years ago

I think it would be very beneficial to move the encoding of the barcode to its own public function so that you can get the actual binary value of the barcode without actually having to go through the whole generation process.

I am writing a library that interfaces with yours in order to render barcodes as dxf files(for AutoCad drawings). Returning the binary EncodedValue of the Barcode object is fine, but the Encode method runs through the whole image rendering process before setting the EncodedValue. (Line 481 of BarcodeLib.cs)

Moving the actual encoding of the barcode away from the image generation (which is on line 484) into its own public function (maybe called from the constructor) would allow this to happen without generating the image.

Here is a (working) example of the code.

public Barcode(string data, TYPE iType)
{
    this.Raw_Data = data;
    this.Encoded_Type = iType;
    GenerateBarcode(); //Auto generate on this constructor 
}

internal Image Encode()
{
    ibarcode.Errors.Clear();

    DateTime dtStartTime = DateTime.Now;

    GenerateBarcode(); //Generation moved to its own function

    this.Encoded_Value = ibarcode.Encoded_Value;
    this.Raw_Data = ibarcode.RawData;

    _Encoded_Image = (Image)Generate_Image();

    this.EncodedImage.RotateFlip(this.RotateFlipType);

    this.EncodingTime = ((TimeSpan)(DateTime.Now - dtStartTime)).TotalMilliseconds;

    _XML = GetXML();

    return EncodedImage;
}//Encode

public string GenerateBarcode(string raw_data = "") // The public generate function
{
    if (raw_data != "")
    {
        Raw_Data = raw_data;
    }

    //make sure there is something to encode
    if (Raw_Data.Trim() == "")
        throw new Exception("EENCODE-1: Input data not allowed to be blank.");

    if (this.EncodedType == TYPE.UNSPECIFIED)
        throw new Exception("EENCODE-2: Symbology type not allowed to be unspecified.");

    this.Encoded_Value = "";
    this._Country_Assigning_Manufacturer_Code = "N/A";

    switch (this.Encoded_Type)
    {
        case TYPE.UCC12:
        case TYPE.UPCA: //Encode_UPCA();
            ibarcode = new UPCA(Raw_Data);
            break;
        case TYPE.UCC13:
        case TYPE.EAN13: //Encode_EAN13();
            ibarcode = new EAN13(Raw_Data);
            break;
        case TYPE.Interleaved2of5_Mod10:
        case TYPE.Interleaved2of5: //Encode_Interleaved2of5();
            ibarcode = new Interleaved2of5(Raw_Data, Encoded_Type);
            break;
        case TYPE.Industrial2of5_Mod10:
        case TYPE.Industrial2of5:
        case TYPE.Standard2of5_Mod10:
        case TYPE.Standard2of5: //Encode_Standard2of5();
            ibarcode = new Standard2of5(Raw_Data, Encoded_Type);
            break;
        case TYPE.LOGMARS:
        case TYPE.CODE39: //Encode_Code39();
            ibarcode = new Code39(Raw_Data);
            break;
        case TYPE.CODE39Extended:
            ibarcode = new Code39(Raw_Data, true);
            break;
        case TYPE.CODE39_Mod43:
            ibarcode = new Code39(Raw_Data, false, true);
            break;
        case TYPE.Codabar: //Encode_Codabar();
            ibarcode = new Codabar(Raw_Data);
            break;
        case TYPE.PostNet: //Encode_PostNet();
            ibarcode = new Postnet(Raw_Data);
            break;
        case TYPE.ISBN:
        case TYPE.BOOKLAND: //Encode_ISBN_Bookland();
            ibarcode = new ISBN(Raw_Data);
            break;
        case TYPE.JAN13: //Encode_JAN13();
            ibarcode = new JAN13(Raw_Data);
            break;
        case TYPE.UPC_SUPPLEMENTAL_2DIGIT: //Encode_UPCSupplemental_2();
            ibarcode = new UPCSupplement2(Raw_Data);
            break;
        case TYPE.MSI_Mod10:
        case TYPE.MSI_2Mod10:
        case TYPE.MSI_Mod11:
        case TYPE.MSI_Mod11_Mod10:
        case TYPE.Modified_Plessey: //Encode_MSI();
            ibarcode = new MSI(Raw_Data, Encoded_Type);
            break;
        case TYPE.UPC_SUPPLEMENTAL_5DIGIT: //Encode_UPCSupplemental_5();
            ibarcode = new UPCSupplement5(Raw_Data);
            break;
        case TYPE.UPCE: //Encode_UPCE();
            ibarcode = new UPCE(Raw_Data);
            break;
        case TYPE.EAN8: //Encode_EAN8();
            ibarcode = new EAN8(Raw_Data);
            break;
        case TYPE.USD8:
        case TYPE.CODE11: //Encode_Code11();
            ibarcode = new Code11(Raw_Data);
            break;
        case TYPE.CODE128: //Encode_Code128();
            ibarcode = new Code128(Raw_Data);
            break;
        case TYPE.CODE128A:
            ibarcode = new Code128(Raw_Data, Code128.TYPES.A);
            break;
        case TYPE.CODE128B:
            ibarcode = new Code128(Raw_Data, Code128.TYPES.B);
            break;
        case TYPE.CODE128C:
            ibarcode = new Code128(Raw_Data, Code128.TYPES.C);
            break;
        case TYPE.ITF14:
            ibarcode = new ITF14(Raw_Data);
            break;
        case TYPE.CODE93:
            ibarcode = new Code93(Raw_Data);
            break;
        case TYPE.TELEPEN:
            ibarcode = new Telepen(Raw_Data);
            break;
        case TYPE.FIM:
            ibarcode = new FIM(Raw_Data);
            break;
        case TYPE.PHARMACODE:
            ibarcode = new Pharmacode(Raw_Data);
            break;

        default: throw new Exception("EENCODE-2: Unsupported encoding type specified.");
    }//switch

    return this.Encoded_Value;

}
barnhill commented 5 years ago

Of course ... Create a PR and I'll review it for sure!!

unitedsoftwork commented 5 years ago

I submitted the request.

I wasn't quite sure what to name the method so I simply did GenerateBarcode.

Naming the new method Encode seems more descriptive, and passing the image generation to a different function named "GenerateImage" or something seems to be more ideal, but something like that would no doubt break the API since the "Encode" method is currently so heavily used.

If you think of a more descriptive name of what is happening, please feel free to change it.

barnhill commented 5 years ago

related to https://github.com/barnhill/barcodelib/pull/71

barnhill commented 5 years ago

I gotta find time to review this

barnhill commented 5 years ago

Merged