[英]Getting net.sf.jasperreports.engine.JRException: Byte data not found at location when pass java.awt.Image as parameter
I get the exception above when I try to display an image chosen from JFileChooser for passing to JasperReports report, can anyone please help me to figure out it?当我尝试显示从JFileChooser选择的图像以传递给JasperReports报告时出现上述异常,有人可以帮我弄清楚吗?
The steps I followed are:我遵循的步骤是:
$P{photo}
从调色板中拖放图像并将其表达式 class 作为java.awt.Image并将图像表达式赋给$P{photo}
This is the image element in.jrxml file这是.jrxml文件中的图像元素
<image>
<reportElement x="315" y="0" width="195" height="77" uuid="59390988-25fc-4d62-80c4-bc19b80b0fa0"/>
<imageExpression><![CDATA[$P{photo}]]></imageExpression>
</image>
These are the code examples i referred and used to generate the report.这些是我参考并用于生成报告的代码示例。
Image photo;
JFileChooser fc = new JFileChooser();
private void getPicture() {
fc.setFileFilter(new FileNameExtensionFilter("Images", "jpg", "gif", "bmp"));
if (fc.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
fc.setCurrentDirectory(fc.getCurrentDirectory());
ImageIcon icon = new ImageIcon(fc.getSelectedFile().getAbsolutePath());
icon = new ImageIcon(icon.getImage().getScaledInstance(350, 350, Image.SCALE_DEFAULT));
photo = icon.getImage();
}
if (photo == null) {
System.out.println("null");
}
System.out.println("path= " + fc.getSelectedFile().getAbsolutePath());
Map<String, Object> param = new HashMap<String, Object>();
param.put("photo", photo);
generateReports(param);
}
private void generateReports(Map param) {
try {
String source = "C:\\newReport.jrxml";
if (new File(source).exists() == false) {
System.out.println("Please report Source does not exist");
return;
}
JasperReport jasperReport = JasperCompileManager.compileReport(source);
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, param, new JREmptyDataSource());
JasperViewer.viewReport(jasperPrint, false);
} catch (Exception e) {
System.out.println("reports Error " + e.toString());
}
}
go to your xml and do this, the location of my image is as seen below it works for me thanks 转到您的xml并执行此操作,图像的位置如下所示,对我有用
imageExpression>![CDATA["src\\Forms\\leaf_banner_gray.png imageExpression>![CDATA [“ src \\ Forms \\ leaf_banner_gray.png
The below line of code working fine when I do preview of the PDF from JasperStudio but not while creating PDF from Java.当我从 JasperStudio 预览 PDF 但从 Java 创建 PDF 时,下面的代码行工作正常。
<imageExpression>![CDATA["src\Forms\leaf_banner_gray.png"]]</imageExpression>
Finally, we found the solution.最后,我们找到了解决方案。
//declared a property leafBanner of type InputStream
<parameter name="leafBanner" class="java.io.InputStream"/>
//leafBanner property has been used below
<imageExpression>![CDATA[$P{leafBanner}]]</imageExpression>
Below is the code to pass image as stream from Java.下面是从 Java 传递图像为 stream 的代码。
Map<String, Object> params = new HashMap<>();
File leafBanner= ResourceUtils.getFile("src//main//resources//images//leafBanner.PNG");
FileInputStream leafBannerStream = new FileInputStream(leafBanner);
params.put("leafBanner", leafBannerStream);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.