I want to print a JasperReports\'s report via Java.
So I wrote a code as follows
try {
String r =\"C:\\\\ireport\\\\Foods.jrxml\"
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:
Different ways of loading a file as an InputStream
How to read properties file in web application?
Where to place and how to read configuration resource files in servlet based application?
Info about how to read image with Java: