当我尝试显示从 JFileChooser 中选择的图像以传递给 JasperReports 报告时,我得到了上述例外,有人可以帮我解决一下吗?
我遵循的步骤是:
$P{photo}
这是.jrxml文件中的图像元素
<image>
<reportElement x="315" y="0" width="195" height="77" uuid="59390988-25fc-4d62-80c4-bc19b80b0fa0"/>
<imageExpression><![CDATA[$P{photo}]]></imageExpression>
</image>
这些是我引用并用于生成报告的代码示例。
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());
}
}
答案 0 :(得分:0)
转到你的xml并执行此操作,我的图片的位置如下所示,它对我有用谢谢
imageExpression&GT;![CDATA [&#34; SRC \形式\ leaf_banner_gray.png