java iReport 中的 google.zxing 条码生成器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7626013/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 20:44:53  来源:igfitidea点击:

google.zxing barcode generator in iReport

javajasper-reportsireportbarcodezxing

提问by masoud

I want put a barcode in my page and can preview it. The barcode generator is google.zxingand my reporting tool is iReport.

我想在我的页面中放置一个条形码并可以预览它。条形码生成器是google.zxing,我的报告工具是iReport

But i dont know, how to configure Image Expressionand Expression Classof an image in iReport.

但我不知道如何配置Image Expression,并Expression Class在图像的iReport的

采纳答案by mdahlman

The two key ideas are first to write a bit of Java code to create the relevant image and then to design the report to reference this code appropriately. Perhaps the simplest way to generate the image is in a scriptlet like this:

两个关键的想法是首先编写一些 Java 代码来创建相关图像,然后设计报告以适当地引用此代码。也许生成图像的最简单方法是在这样的脚本中:

package com.jaspersoft.alliances.mdahlman;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import net.sf.jasperreports.engine.JRDefaultScriptlet;
import net.sf.jasperreports.engine.JRScriptletException;

public class QRCodeScriptlet extends JRDefaultScriptlet {
    public void afterDetailEval() throws JRScriptletException {
        QRCodeWriter writer = new QRCodeWriter();
        BitMatrix matrix = null;
        try {
            matrix = writer.encode(getFieldValue("barcode_text").toString(), BarcodeFormat.QR_CODE, 256, 256);
            this.setVariableValue("BarCodeImage", MatrixToImageWriter.toBufferedImage(matrix) );
        } catch (WriterException e) {
            e.printStackTrace();
        }
    }
}

That's full of hard-coded ugliness, but the key ideas are all shown. Then you need to define the report like this:

那满是硬编码的丑陋,但关键的想法都显示出来了。然后你需要像这样定义报告:

  1. Sample query: select 'some text' as barcode_textI included this only to reinforce the point that my scriptlet hard-codes the field name barcode_text. (This is bad.)
  2. Variable: BarCodeImageof type java.awt.image.BufferedImagewith calculation System. This name is hard-coded in the scriptlet too. (This is equally bad.)
  3. Add to iReport's classpath:
    • The compiled scriptlet .jar file
    • core.jar (from ZXing)
    • javase.jar (from ZXing)
  4. Add an Image element to the report with Expression $V{BarCodeImage}.
  1. 示例查询:select 'some text' as barcode_text我包含这个只是为了强调我的 scriptlet 对字段 name 进行了硬编码barcode_text。(这不好。)
  2. 变量:BarCodeImage类型java.awt.image.BufferedImage与计算System。该名称也在 scriptlet 中硬编码。(这同样糟糕。)
  3. 添加到 iReport 的类路径:
    • 编译后的 scriptlet .jar 文件
    • core.jar(来自ZXing)
    • javase.jar(来自ZXing)
  4. 使用 Expression 将 Image 元素添加到报告中$V{BarCodeImage}

The result is a happy happy QR-code in your generated JasperReport:

结果是您生成的 JasperReport 中出现了一个快乐的二维码:

Generated QR-Code

生成的二维码

I recall a sample that I have seen which does things much more cleanly. It actually included a nice plug-in so you could easily install this functionality into iReport with minimal effort. If I can track that down, then I'll update this post. But until then this at least covers all of the critical points.

我记得我见过的一个样本做得更干净。它实际上包含了一个不错的插件,因此您可以轻松地将此功能安装到 iReport 中。如果我能追踪到它,那么我会更新这篇文章。但在那之前,这至少涵盖了所有关键点。

回答by Marc Nuri

The image expression should return any subclass of java.awt.Image. The easiest way to achieve this is to use your own helper class to generate the Image. You can create a static method that generates a barcode from a Stringand call that method from IReport.

图像表达式应返回 的任何子类java.awt.Image。实现此目的的最简单方法是使用您自己的帮助程序类来生成图像。您可以创建一个静态方法,从 a 生成条形码String并从 IReport 调用该方法。

In the case of ZXing I don't know the method to use, but I can tell what I use as ImageExpression using the Barbecue library.

在 ZXing 的情况下,我不知道要使用的方法,但我可以使用 Barbecue 库告诉我用作 ImageExpression 的内容。

        net.sourceforge.barbecue.BarcodeImageHandler.getImage(
MyBarcodeGenerator.getFromString($F{field})

MyBarcodeGeneratorclass contains the method getFromString(...)that returns a net.sourceforge.barbecue.Barcodein my case a net.sourceforge.barbecue.linear.code39.Code39Barcode

MyBarcodeGenerator类包含在我的情况下getFromString(...)返回 a的方法net.sourceforge.barbecue.Barcodeanet.sourceforge.barbecue.linear.code39.Code39Barcode

The Expression Classis ignored.

Expression Class被忽略。

--Edited:

——编辑:

To encode an Image in zxing you should use MatrixToImageWriter

要在 zxing 中编码图像,您应该使用 MatrixToImageWriter

The following code will encode a QRCode into a BufferedImage which you can use in the Image Expression field:

以下代码将 QRCode 编码为 BufferedImage,您可以在 Image Expression 字段中使用它:

MatrixToImageWriter.toBufferedImage(new QRCodeWriter().encode("BARCODE CONTENT", BarcodeFormat.QR_CODE,     400 /*Width*/, 400/*Height*/));