gpt4 book ai didi

java - 使用 JRImageLoader.loadAwtImageFromBytes 方法加载图像时出现错误

转载 作者:行者123 更新时间:2023-12-02 09:52:18 25 4
gpt4 key购买 nike

我正在使用 JasperSoft Studio 6.8.0,但遇到了一个棘手的错误:

我的 Java 类 EnvParameter 有一个 logo 属性。

该属性是一个byte[]

我将 Java 类传递给 Jasper Studio,因此我尝试将图像加载到 Logo 属性中,如下所示:

net.sf.jasperreports.engine.util.JRImageLoader.loadAwtImageFromBytes($P{EnvParameter}.logo)

编辑

我也尝试过

net.sf.jasperreports.engine.util.JRImageLoader.loadAwtImageFromBytes($P{EnvParameter}.getLogo())

但是我在运行时遇到这个错误(编译没问题):

net.sf.jasperreports.engine.fill.JRExpressionEvalException: Error evaluating expression for source text: net.sf.jasperreports.engine.util.JRImageLoader.loadAwtImageFromBytes($P{EnvParameter}.logo) at net.sf.jasperreports.engine.fill.JREvaluator.handleEvaluationException(JREvaluator.java:284) at net.sf.jasperreports.compilers.GroovyEvaluator.handleEvaluationException(GroovyEvaluator.java:98) at net.sf.jasperreports.engine.fill.JREvaluator.evaluate(JREvaluator.java:316) at net.sf.jasperreports.engine.fill.JRCalculator.evaluate(JRCalculator.java:672) at net.sf.jasperreports.engine.fill.JRCalculator.evaluate(JRCalculator.java:640) at net.sf.jasperreports.engine.fill.JRFillElement.evaluateExpression(JRFillElement.java:1169) at net.sf.jasperreports.engine.fill.JRFillImage.evaluateImage(JRFillImage.java:466) at net.sf.jasperreports.engine.fill.JRFillImage.evaluate(JRFillImage.java:440) at net.sf.jasperreports.engine.fill.JRFillElementContainer.evaluate(JRFillElementContainer.java:383) at net.sf.jasperreports.engine.fill.JRFillBand.evaluate(JRFillBand.java:548) at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillTitle(JRVerticalFiller.java:323) at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReport(JRVerticalFiller.java:150) at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:615) at net.sf.jasperreports.engine.fill.BaseReportFiller.fill(BaseReportFiller.java:414) at net.sf.jasperreports.engine.fill.JRFillSubreport.fillSubreport(JRFillSubreport.java:814) at net.sf.jasperreports.engine.fill.JRSubreportRunnable.run(JRSubreportRunnable.java:61) at net.sf.jasperreports.engine.fill.AbstractThreadSubreportRunner.run(AbstractThreadSubreportRunner.java:221) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) at java.lang.Thread.run(Thread.java:745) Caused by: groovy.lang.MissingMethodException: No signature of method: static net.sf.jasperreports.engine.util.JRImageLoader.loadAwtImageFromBytes() is applicable for argument types: ([B) values: [[-119, 80, 78, 71, 13, 10, 26, 10, 0, 0, 0, 13, 73, 72, 68, ...]] Possible solutions: loadAwtImageFromBytes([B) at groovy.lang.MetaClassImpl.invokeStaticMissingMethod(MetaClassImpl.java:1518) at groovy.lang.MetaClassImpl.invokeStaticMethod(MetaClassImpl.java:1504) at org.codehaus.groovy.runtime.callsite.StaticMetaClassSite.call(StaticMetaClassSite.java:52) at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:47) at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:116) at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:128) at rptVTitolo_1558437271760_434544.evaluate(calculator_rptVTitolo_1558437271760_434544:203) at net.sf.jasperreports.engine.fill.JREvaluator.evaluate(JREvaluator.java:302) ... 17 common frames omitted mag 21, 2019 1:17:36 PM org.apache.catalina.core.StandardWrapperValve invoke

最佳答案

出了什么问题?

看起来您使用了错误的方法来加载图像。JRImageLoader 类在迁移到 6.x 版本引擎期间发生了更改,并且没有静态方法loadAwtImageFromBytes(bytep[]) 在新版本的 JasperReports 的此类中(我相信它在 JR 5.5.0 中就存在)。

工作示例

您可以尝试这个小例子。

用于测试的小 Bean:

package ru.alex.model;

public class MyBean {

private byte[] logo;

public byte[] getLogo() {
return logo;
}

public void setLogo(byte[] logo) {
this.logo = logo;
}
}

模板(jrxml 文件)非常简单(我使用的是 JasperReports 6.4):

<?xml version="1.0" encoding="UTF-8"?>
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="ImageExample" pageWidth="612" pageHeight="792" whenNoDataType="AllSectionsNoDetail" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20">
<parameter name="mybean" class="ru.alex.model.MyBean" isForPrompting="false"/>
<title>
<band height="72">
<image>
<reportElement x="200" y="10" width="50" height="50"/>
<imageExpression><![CDATA[net.sf.jasperreports.engine.util.JRImageLoader.getInstance(DefaultJasperReportsContext.getInstance()).loadAwtImageFromBytes($P{mybean}.getLogo())]]></imageExpression>
</image>
</band>
</title>
</jasperReport>

我用了JRImageLoader.loadAwtImageFromBytes(byte[])从字节数组加载图像的方法。正如您所看到的,我们需要首先获取 JRImageLoader 的实例 - 我使用以下代码使用默认上下文:JRImageLoader.getInstance(DefaultJasperReportsContext.getInstance())

生成报告的代码很简单:

JasperReport jasperReport;
try (InputStream inputStream = JRLoader.getResourceInputStream("image.jrxml")) {
jasperReport = JasperCompileManager.compileReport(JRXmlLoader.load(inputStream));
}
Map<String, Object> params = new HashMap<>();
MyBean myBean = new MyBean();

File file = new File(getClass().getClassLoader().getResource("smile.jpg").getFile());
myBean.setLogo(Files.readAllBytes(file.toPath()));

params.put("mybean", myBean);
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, params, new JREmptyDataSource());

SimplePdfExporterConfiguration configuration = new SimplePdfExporterConfiguration();
File outputFile = new File("output.pdf");
try (ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
OutputStream fileOutputStream = new FileOutputStream(outputFile)) {
JRPdfExporter exporter = new JRPdfExporter();
exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(byteArrayOutputStream));
exporter.setConfiguration(configuration);
exporter.exportReport();
byteArrayOutputStream.writeTo(fileOutputStream);
}

生成的报告如下所示:

enter image description here

关于java - 使用 JRImageLoader.loadAwtImageFromBytes 方法加载图像时出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56237198/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com