Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

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

Sample produced barcode image

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?

like image 805
user630209 Avatar asked Jun 20 '26 12:06

user630209


1 Answers

This is not supported out of the box by 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.

Find a small PoC snippet demonstrating the general idea.

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.

enter image description here

Another possiblity could be to generate the barcode with HumanReadablePlacement.HRP_NONE and later draw the text using canvas.deviceText(...).

like image 57
SubOptimal Avatar answered Jun 22 '26 01:06

SubOptimal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!