How to use Barcode4J to get the barcode number?

2.3k Views Asked by At

I am using barcode4j to generate some barcode image, which works fine.

But the UI team wanna me to write some service to return the barcode number in String for them for some strange reasons. I can't figure out how to do this.

Below is the code snippet for how to generate the Barcode Image.

final File outputFile = new File(folderPath + "/" + TgtCoreConstants.TARGET_BARCODE_FILE_PREFIX
            + orderId + BARCODE_FILENAME_EXTENSION);
    OutputStream out = null;
    try {
        out = new FileOutputStream(outputFile);
        final BitmapCanvasProvider canvas = new BitmapCanvasProvider(
                out, BARCODE_MIME_TYPE, cncBarcodeDpi, BufferedImage.TYPE_BYTE_BINARY, false, 0);
        bean.generateBarcode(canvas, (storeNumber + orderId));
        canvas.finish();
    }
    catch (final FileNotFoundException e) {
        LOG.error("Error while generating the barcode file for order: " + orderId, e);
        throw new GenerateBarCodeFailedException(e);
    }
    catch (final IOException e) {
        LOG.error("Error while generating the barcode file for order: " + orderId, e);
        throw new GenerateBarCodeFailedException(e);
    }
2

There are 2 best solutions below

1
On

The above snippet tells barcode4j to encode, render as bitmap and write in outputFile the string storeNumber + orderId, so the requested service needs just to

return (storeNumber + orderId);

If you need to decode the barcode given just the outputFile, then look at the ZXing project.

0
On

Since your existing method is working, just create new method & passed (storeNumber + orderId) as the method paramater.

*Existing method...*
String barText=storeNumber + orderId;
getBarcodeText(barText);

public String getBarcodeText(String barText) {

    <other checking>
    return barText;
}