简体   繁体   English

如何使用Barcode4J获取条形码编号?

[英]How to use Barcode4J to get the barcode number?

I am using barcode4j to generate some barcode image, which works fine. 我正在使用条形码4j生成一些条形码图像,效果很好。

But the UI team wanna me to write some service to return the barcode number in String for them for some strange reasons. 但是UI团队希望我出于某种奇怪的原因为他们编写一些服务以返回String的条形码编号。 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 上面的代码片段告诉条形码4j进行编码,渲染为位图,并在字符串storeNumber + orderId字符串storeNumber + orderId写入outputFile ,因此所请求的服务只需要

return (storeNumber + orderId);

If you need to decode the barcode given just the outputFile , then look at the ZXing project. 如果只需要解码outputFile给出的条形码,则查看ZXing项目。

Since your existing method is working, just create new method & passed (storeNumber + orderId) as the method paramater. 由于您现有的方法有效,因此只需创建新方法并通过(storeNumber + orderId)作为方法参数即可。

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

public String getBarcodeText(String barText) {

    <other checking>
    return barText;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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