简体   繁体   English

带条码Code128的JasperReports(Barcode4J):PNG与PDF不同

[英]JasperReports with Barcode Code128 (Barcode4J): PNG differs from PDF

I'm printing a label with following number "1000049722ABCD2F" as a barcode, using a Jasper-template and the barcode-element "Code128" from Barcode4J. 我正在使用Jasper模板和Barcode4J的条形码元素“Code128”打印带有以下编号“1000049722ABCD2F”的标签作为条形码。 Printed as a PDF, the barcode is perfect and scannable. 以PDF格式打印,条形码完美且可扫描。 Printing same barcode as a PNG-file, the barcode is not scannable and it obviously differs from the PDF-barcode : 打印相同的条形码作为PNG文件,条形码不可扫描,它明显不同于PDF条形码

Same barcode: PDF (upper part of an image) and PNG (lower part of an image): 相同的条形码:PDF(图像的上半部分)和PNG(图像的下半部分):

相同的条形码:PDF(上)和PNG(下)

Following code is used to generate the PDF-barcode : 以下代码用于生成PDF条形码

    byte[] data = JasperExportManager.exportReportToPdf(jasperPrint);

To generate PNG, it's not that simple : 要生成PNG,它并不那么简单

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

    // DPI = 200, inch = 25.4
    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 am using: barcode4j-2.1 / jasperreports-5.0.0 / 200 DPI is a required size for my label printer 我正在使用:barcode4j-2.1 / jasperreports-5.0.0 / 200 DPI是我的标签打印机所需的尺寸

I tried to change quite some settings (BufferedImage.TYPE-value, width of barcode, Barbecue-barcode 128B and others), but there is always a difference between the PDF and PNG-barcode. 我试图改变一些设置(BufferedImage.TYPE值,条形码宽度,烧烤条形码128B等),但PDF和PNG条形码之间总是存在差异。

GOAL: PNG-barcode should be exactly the same as the PDF-barcode. 目标: PNG条形码应与PDF条形码完全相同。

Could anyone help me with this? 任何人都可以帮我这个吗? I would greatly appreciate it! 我将不胜感激!

Instead of using ImageWriter, why don't you try using MimeTypes.MIME_PNG from org.krysalis.barcode4j.tools.MimeTypes (from barcode4j-2.1.jar itself). 为什么不尝试使用org.krysalis.barcode4j.tools.MimeTypes(来自barcode4j-2.1.jar本身)的MimeTypes.MIME_PNG ,而不是使用ImageWriter。

I'll not repeat my answer but you can refer to my codes. 我不会重复我的回答,但你可以参考我的代码。 Here's the link to my other stackoverflow: 这是我的其他stackoverflow的链接:

Barcode4j as png image Barcode4j为png图像

and I've never face this issue (unable to scan barcode) using the program. 我从来没有遇到过使用该程序的问题(无法扫描条形码)。

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

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