I am printing a label with the following number "1000049722ABCD2F" as a barcode using the Jasper template and the barcode "Code128" from Barcode4J. Printed as a PDF, the barcode is perfect and scannable. Printing the same barcode as a PNG file, the barcode is not scanned and, obviously, differs from the PDF barcode :
Same barcode: PDF (top of image) and PNG (bottom of image):

The following code is used to create a PDF barcode :
byte[] data = JasperExportManager.exportReportToPdf(jasperPrint);
To generate PNG is not so simple :
ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream();
ImageOutputStream imageOutputStream = ImageIO.createImageOutputStream(byteOutputStream);
ImageWriter imageWriter = ImageIO.getImageWritersBySuffix("png").next();
imageWriter.setOutput(imageOutputStream);
float zoom = getZoomFactor(jasperPrint);
BufferedImage image = new BufferedImage(
(int)(jasperPrint.getPageWidth() * zoom ) + 1,
(int)(jasperPrint.getPageHeight() * zoom) + 1,
BufferedImage.TYPE_INT_RGB);
JRGraphics2DExporterNoAntialias exporter = new JRGraphics2DExporterNoAntialias();
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRGraphics2DExporterParameter.GRAPHICS_2D, image.getGraphics());
exporter.setParameter(JRExporterParameter.PAGE_INDEX, Integer.valueOf(page));
exporter.setParameter(JRGraphics2DExporterParameter.ZOOM_RATIO, new Float(zoom));
exporter.exportReport();
IIOMetadata imageMetaData = imageWriter.getDefaultImageMetadata(new ImageTypeSpecifier(image), null);
double dotsPerMilli = 200 / 25.4;
IIOMetadataNode horiz = new IIOMetadataNode("HorizontalPixelSize");
horiz.setAttribute("value", Double.toString(dotsPerMilli));
IIOMetadataNode vert = new IIOMetadataNode("VerticalPixelSize");
vert.setAttribute("value", Double.toString(dotsPerMilli));
IIOMetadataNode dim = new IIOMetadataNode("Dimension");
dim.appendChild(horiz);
dim.appendChild(vert);
IIOMetadataNode root = new IIOMetadataNode("javax_imageio_1.0");
root.appendChild(dim);
imageMetaData.mergeTree("javax_imageio_1.0", root);
imageWriter.write(null, new IIOImage(image, null, imageMetaData), null);
imageOutputStream.close();
imageWriter.dispose();
byte[] data = byteOutputStream.toByteArray();
I use: barcode4j-2.1 / jasperreports-5.0.0 / 200 DPI - the required size for my label printer
(BufferedImage.TYPE-, -, Barbecue-barcode 128B ), - PDF PNG.
: PNG-- , PDF--.
- ? !