如何在JasperReport中使用条形码?

时间:2018-11-14 14:00:40

标签: java jasper-reports barcode barbecue

我想在JasperRepost上显示一个EAN13条形码。我在字段列表中输入了13位数字(SQL查询也可以),但是在JasperSoft Studio 6.6.0中,“调色板”中的“条形码”元素似乎不适用于拖放操作(不会悲伤地做任何事情...)。 所以...我进入了源代码视图,并进行了一些Google搜索,条形码元素的外观如下:

<componentElement>
                <reportElement x="0" y="100" width="200" height="50" uuid="48d9d636-7e04-43df-9fa2-5c3f6edf27da"/>
                <c:barbecue xmlns:c="http://jasperreports.sourceforge.net/jasperreports/components" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/components http://jasperreports.sourceforge.net/xsd/components.xsd" type="EAN13" drawText="true" checksumRequired="false">
                    <c:codeExpression><![CDATA[$F{EAN}]]></c:codeExpression>
                </c:barbecue>
            </componentElement>

但是,这似乎也不可行,因为如果要预览它,我会收到此错误:

net.sf.jasperreports.engine.JRException: net.sf.jasperreports.engine.JRRuntimeException: Error creating barcode.
    at com.jaspersoft.studio.editor.preview.view.control.ReportController.fillReport(ReportController.java:536)
    at com.jaspersoft.studio.editor.preview.view.control.ReportController.access$17(ReportController.java:511)
    at com.jaspersoft.studio.editor.preview.view.control.ReportController$1.run(ReportController.java:429)
    at org.eclipse.core.internal.jobs.Worker.run(Worker.java:56)
Caused by: net.sf.jasperreports.engine.JRRuntimeException: Error creating barcode.
    at net.sf.jasperreports.components.barbecue.BarcodeProviders.createBarcode(BarcodeProviders.java:114)
    at net.sf.jasperreports.components.barbecue.BarbecueFillComponent.setBarcodeImage(BarbecueFillComponent.java:142)
    at net.sf.jasperreports.components.barbecue.BarbecueFillComponent.fill(BarbecueFillComponent.java:111)
    at net.sf.jasperreports.engine.fill.JRFillComponentElement.fill(JRFillComponentElement.java:198)
    at net.sf.jasperreports.engine.fill.JRFillElementContainer.fillElements(JRFillElementContainer.java:1039)
    at net.sf.jasperreports.engine.fill.JRFillBand.fill(JRFillBand.java:469)
    at net.sf.jasperreports.engine.fill.JRFillBand.fill(JRFillBand.java:417)
    at net.sf.jasperreports.engine.fill.JRHorizontalFiller.fillFixedBand(JRHorizontalFiller.java:2553)
    at net.sf.jasperreports.engine.fill.JRHorizontalFiller.fillDetail(JRHorizontalFiller.java:853)
    at net.sf.jasperreports.engine.fill.JRHorizontalFiller.fillReportStart(JRHorizontalFiller.java:264)
    at net.sf.jasperreports.engine.fill.JRHorizontalFiller.fillReport(JRHorizontalFiller.java:116)
    at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:615)
    at net.sf.jasperreports.engine.fill.BaseFillHandle$ReportFill.run(BaseFillHandle.java:135)
    at java.lang.Thread.run(Thread.java:748)
Caused by: net.sourceforge.barbecue.BarcodeException: Invalid data length
    at net.sourceforge.barbecue.linear.upc.UPCABarcode.<init>(Unknown Source)
    at net.sourceforge.barbecue.linear.upc.UPCABarcode.<init>(Unknown Source)
    at net.sourceforge.barbecue.linear.ean.EAN13Barcode.<init>(Unknown Source)
    at net.sourceforge.barbecue.BarcodeFactory.createEAN13(Unknown Source)
    at net.sf.jasperreports.components.barbecue.BarcodeProviders$EAN13Provider.createBaseBarcode(BarcodeProviders.java:241)
    at net.sf.jasperreports.components.barbecue.BaseBarcodeProvider.createBarcode(BaseBarcodeProvider.java:40)
    at net.sf.jasperreports.components.barbecue.BarcodeProviders.createBarcode(BarcodeProviders.java:110)
    ... 13 more

可能是水吗?无效的数据长度?我的测试条形码值实际上是:6941057402314,正好是13位数字。此值以VARCHAR的形式存储在数据库中,这可能是问题所在吗?

编辑。:

好吧,我发现这可能是烧烤问题:Babrbecue Exception,但是已经有10年历史了...

谢谢。

1 个答案:

答案 0 :(得分:0)

事实证明,EAN13条形码实际上是12位数字长,因此计算出了13位数字。因此,如果我将其更改为12位数字,则会生成条形码。