简体   繁体   中英

How to use Barcode4J to get the barcode number?

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);
    }

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.

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM