I want to print a JasperReports 's report via Java.
So I wrote a code as follows
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
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?
My jrxml had following code snippet
<imageExpression><![CDATA["flower1.png"]]></imageExpression>
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);
<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>
The more information about using images you can find here
Info about reading resources with Java and where to store it:
Info about how to read image with Java :
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.