简体   繁体   English

条形码人类可读放置与条形码平行

[英]barcode human readable placing parallel to barcode

Here is the code to generate a barcode based on the Id passed, the barcode is generated fine: 这是根据传递的Id生成条形码的代码,条形码生成正常:

 @Override  
 public byte[] generateBarcodeForId(String Id) throws VisitMastException{

     BarcodeUtil util = BarcodeUtil.getInstance();
     BarcodeGenerator gen;
     ByteArrayOutputStream bao = null;
     try {
         bao = new ByteArrayOutputStream();

         //Create the barcode bean
         Code128Bean bean = new Code128Bean();

         int dpi = 150;

         //Configure the barcode generator
         bean.setModuleWidth(UnitConv.in2mm(1.1f / dpi)); //makes the narrow bar, width exactly one pixel
         bean.doQuietZone(true);
         bean.setBarHeight(4);
         //bean.setVerticalQuietZone(3);
         bean.setQuietZone(0);
         bean.setMsgPosition(HumanReadablePlacement.HRP_TOP);
         BitmapCanvasProvider canvas = new BitmapCanvasProvider(
             bao, "image/jpeg", dpi, BufferedImage.TYPE_BYTE_BINARY, false, 0);
         bean.generateBarcode(canvas, Id);
         canvas.finish();
     } catch (IOException  e) {
         throw new VisitMastException(VisitMastException.BAD_REQUEST,
                    messageSource.getMessage(CodeEnum.BARCODE_GENERATING_ERROR.getValue(), null, Locale.ENGLISH));
     }
     return bao.toByteArray();
 }

样品生成条形码图像

This code places the human readable value above the barcode: 此代码将人类可读值置于条形码上方:

bean.setMsgPosition(HumanReadablePlacement.HRP_TOP);

The human readable value can be placed either at the bottom or top or neither. 人类可读的值可以放在底部或顶部,也可以两者都不放置。 Is it possible to add the human readable value parallel to the barcode or next to it. 是否可以添加与条形码平行或旁边的人类可读值。

Also could we reduce the size of the human readable value? 我们还可以减小人类可读值的大小吗?

This is not supported out of the box by Barcode4J. Barcode4J不支持此功能。 One solution (aside adding this feature to Barcode4J) could be to create a new image with the double size and copy the barcode and the text area into it. 一种解决方案(除了将此功能添加到Barcode4J)可以创建具有双倍大小的新图像并将条形码和文本区域复制到其中。

Find a small PoC snippet demonstrating the general idea. 找一个小的PoC片段来展示一般的想法。

BitmapCanvasProvider canvas = new BitmapCanvasProvider(dpi, 
        BufferedImage.TYPE_BYTE_BINARY, false, 0);
bean.generateBarcode(canvas, Id);
canvas.finish();

BufferedImage image = canvas.getBufferedImage();
BufferedImage temp = new BufferedImage(image.getWidth() * 2, 
        image.getHeight() / 2 - 1, image.getType());
Graphics2D g = temp.createGraphics();
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
        RenderingHints.VALUE_ANTIALIAS_OFF);
g.drawImage(image, 0, -image.getHeight() / 2, null);
g.drawImage(image, image.getWidth(), 0, null);

g.dispose();        
bao.reset();
ImageIO.write(temp, "png", bao);

The generated bytes are stored to file as 生成的字节存储为文件

byte[] byteArray = generateBarcodeForId("1111");
BufferedImage image = ImageIO.read(new ByteArrayInputStream(byteArray));
ImageIO.write(image, "jpg", new File("code128.jpg"));

the resulting image code128.jpg . 结果图像code128.jpg

在此输入图像描述

Another possiblity could be to generate the barcode with HumanReadablePlacement.HRP_NONE and later draw the text using canvas.deviceText(...) . 另一种HumanReadablePlacement.HRP_NONE是使用canvas.deviceText(...)生成条形码, HumanReadablePlacement.HRP_NONE使用canvas.deviceText(...)绘制文本。

Why don't you create a method that allow user to set the parameters of the barcode? 为什么不创建一个允许用户设置条形码参数的方法? Allowing the user to change the barText, rotation, dpi number, & fontSize or anything. 允许用户更改barText,rotation,dpi number,&fontSize或任何内容。 The program shall return a string path to the generated barcode image. 程序应返回生成的条形码图像的字符串路径。 The image can be stored in temp folder to avoid setting fix folder in any machine. 图像可以存储在临时文件夹中,以避免在任何机器中设置修复文件夹。

Here is my code that I used previously... 这是我之前用过的代码......

public class CustomBarcode {

    public String getBufferedBarcodeImage(String barText, Integer rotation,
            Integer dpi2, double fontSize) throws IOException {

        ByteArrayOutputStream os = null;
        ByteArrayInputStream fis = null;
        OutputStream out = null;

        // Configure the barcode generator
        Code128Bean barcode128Bean = new Code128Bean();
        barcode128Bean.setCodeset(Code128Constants.CODESET_A);
        final int dpi = dpi2;

        barcode128Bean.setBarHeight(15.0);
        barcode128Bean.setFontSize(fontSize);
        barcode128Bean.setQuietZone(5.0);
        barcode128Bean.doQuietZone(true);
        barcode128Bean.setModuleWidth(UnitConv.in2mm(1.6f / dpi)); // makes the
                                                                    // narrow
                                                                    // bar

        String mime = MimeTypes.MIME_PNG;
        File temp = null;
        String tempFile = "";
        try {
            os = new ByteArrayOutputStream();

            BitmapCanvasProvider canvasProvider = new BitmapCanvasProvider(os,
                    "image/x-png", dpi, BufferedImage.TYPE_BYTE_BINARY, false,
                    rotation);

            barcode128Bean.generateBarcode(canvasProvider, barText);
            canvasProvider.finish();

            final BitmapEncoder encoder = BitmapEncoderRegistry
                    .getInstance(mime);
            encoder.encode(canvasProvider.getBufferedImage(), os, mime, dpi); // get
                                                                                // created
                                                                                // barcode
            fis = new ByteArrayInputStream(os.toByteArray());

            temp = File.createTempFile("barcode", ".png");
            IOUtils.copy(fis, new FileOutputStream(temp));
            tempFile = temp.getAbsolutePath();
            System.out.println("tempFile :" + tempFile);
            temp.deleteOnExit();

            // byte[] imageData = os.toByteArray();
        }

        catch (IOException ex) {
            System.out.println("An Exception");
            ex.printStackTrace();
        }

        finally {
            os.flush();
            os.close();
            fis.close();
        }
        return tempFile;
    }
}

I'm using barcode4j-2.1.jar, poi-3.9.jar 我正在使用barcode4j-2.1.jar,poi-3.9.jar

This way, you can easily reduce the size of the "human readable value" by setting the fontSize 这样,您可以通过设置fontSize轻松减小“人类可读值”的大小

This is the sample of the generated barcode Image. 这是生成的条形码图像的样本。 在此输入图像描述

You can even rotate the barcode easily by setting rotation (eg 90) 您甚至可以通过设置旋转轻松旋转条形码(例如90) 在此输入图像描述

You can follow SubOptimal's suggestion on placing barcode text next to it. 您可以按照SubOptimal关于在其旁边放置条形码文本的建议。

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

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