JasperReports API. Getting error when using Image in report: net.sf.jasperreports.engine.JRException: Byte data not found at

前端 未结 1 1239
小鲜肉
小鲜肉 2021-01-29 03:26

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

So I wrote a code as follows

 try {
            String r =\"C:\\\\ireport\\\\Foods.jrxml\"         


        
相关标签:
1条回答
  • 2021-01-29 04:17

    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.

    The sample of usage

    The Java code snippet

    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);
    

    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. The more information about using images you can find here

    2. 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?

    3. Info about how to read image with Java:

      • How do I properly load a BufferedImage in java?
    0 讨论(0)
提交回复
热议问题