JasperReports API。在报表中使用Image时出错:net.sf.jasperreports.engine.JRException:未找到字节数据 [英] JasperReports API. Getting error when using Image in report: net.sf.jasperreports.engine.JRException: Byte data not found at

查看:2423
本文介绍了JasperReports API。在报表中使用Image时出错:net.sf.jasperreports.engine.JRException:未找到字节数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过Java打印 JasperReports 的报告。

I want to print a JasperReports's report via Java.

所以我写了如下代码

 try {
            String r ="C:\\ireport\\Foods.jrxml";
            JasperReport jr =JasperCompileManager.compileReport(r);
            JasperPrint jp = JasperFillManager.fillReport(jr, null, conn);
            JasperViewer.viewReport(jp);
        } catch(Exception e) {
            System.out.println(e);
        }

但是当我运行程序时出现以下错误。

But when I run the program I got following error.

net.sf.jasperreports.engine.JRException: Byte data not found at : flower1.png

我在jasper社区搜索了这个错误,但我无法理解他们解释解决方案的方式,因为我是编程的新手。
那么有人可以给我一个解决方案吗?

I searched about this error in jasper community but I can't get understand the way they have explained the solution since I'm atotally newbie to the programming. So can anyone give me a solution please?

我的jrxml有以下代码片段

My jrxml had following code snippet

<imageExpression><![CDATA["flower1.png"]]></imageExpression>


推荐答案

使用报告参数传递的好方法图片。我认为将图像作为 BufferedImage 对象传递是一个不错的选择。

The good way it to use report's parameter for passing the image. And I believe that passing image as BufferedImage object is a good choice in your case.

Map<String, Object> parameters = new HashMap<>();
try (InputStream inputStream = YourClass.class.getClassLoader().getResourceAsStream("images/flower1.png")) {
    parameters.put("flowerImage", ImageIO.read(new ByteArrayInputStream(JRLoader.loadBytes(inputStream))));
} catch (JRException | IOException e) {
     throw new RuntimeException("Failed to load images", e);
}
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, connection);



jrxml文件的片段



The snippet of jrxml file

<parameter name="logoImage" class="java.awt.Image"/>
...
<image scaleImage="FillFrame">
    <reportElement x="10" y="0" width="224" height="43"/>
    <imageExpression><![CDATA[$P{flowerImage}]]></imageExpression>
</image>








  1. 有关使用图像的更多信息,请参阅此处

有关使用 Java 阅读资源以及存储位置的信息:

Info about reading resources with Java and where to store it:

如何阅读web
应用程序中的属性文件?

放置位置以及如何读取配置资源文件
基于servlet的
申请?

有关如何使用 Java 读取图像:

Info about how to read image with Java:

  • How do I properly load a BufferedImage in java?

这篇关于JasperReports API。在报表中使用Image时出错:net.sf.jasperreports.engine.JRException:未找到字节数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆