Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing image as a parameter jasperreports

Tags:

java

I try to pass image to ireports as parameter logo but when report appears it shows a string definition of the image instead of the logo.

In ireports i created the parameter logo and made its expression as object and dragged image on it from pallete. The image i made its expression to image

private void generateRptForm(String sql, String reportloader) {
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    ByteArrayOutputStream baos = null;
    Connection connect = null;
    try {
        baos = new ByteArrayOutputStream();
        Class.forName("com.mysql.jdbc.Driver").newInstance();
        connect = DriverManager.getConnection("jdbc:mysql://localhost:3306/" + "school", user, password);
        pstmt = connect.prepareStatement("SELECT image FROM picture WHERE id = 1");
        rs = pstmt.executeQuery();
        InputStream imageStream = null;
        BufferedImage image = null;
        while (rs.next()) {
            imageStream = rs.getBinaryStream(1);
            image = ImageIO.read(imageStream);
        }
        System.out.println("image..." + image);
        connect.close();
        connect.close();

        Class.forName("com.mysql.jdbc.Driver");
        conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/" + "school", user, password);
        stmt = conn.createStatement();
        JasperDesign jd = JRXmlLoader.load(reportloader);
        String sqltrans = sql;
        JRDesignQuery newQuery = new JRDesignQuery();
        newQuery.setText(sqltrans);
        jd.setQuery(newQuery);
        // Get data from registration table

        String[] split = null;
        String schname = this.getTitle();
        split = schname.split("\\[");
        schname = split[1];
        split = schname.split("\\]");
        String[] regdetails = dbutils.checker.regdetails(split[0]);

        Map<String, Object> param = new HashMap<String, Object>();

        param.put("schoolname", regdetails[0]);
        param.put("address", regdetails[3]);
        param.put("zipcode", regdetails[4]);
        param.put("telephone", regdetails[5]);
        param.put("location", regdetails[1]);
        param.put("country", regdetails[2]);
        param.put("email", regdetails[6]);

        param.put("logo", image);

        JasperReport jr = JasperCompileManager.compileReport(jd);
        JasperPrint jp = JasperFillManager.fillReport(jr, param, conn);
        JasperViewer.viewReport(jp, false);
    } catch (Exception e) {
        JOptionPane.showMessageDialog(null, e);
        System.out.println(e);
    }
}
like image 445
user3114376 Avatar asked Jan 21 '26 05:01

user3114376


2 Answers

I think the way you are passing the image is OK. Use it in the jrxml as:

<image>
    <reportElement ... />
    <imageExpression class="java.awt.Image"><![CDATA[$P{logo}]]></imageExpression>
</image>

I.e. make sure the expression class is java.awt.Image and the expression itself points to the correct parameter.

like image 149
Nikos Paraskevopoulos Avatar answered Jan 23 '26 21:01

Nikos Paraskevopoulos


Try below and create a parameter of object type and set the expression field of image as $P{Image}

ImageIcon imageIcon = new ImageIcon(new ImageIcon(img).getImage);
parameter.put("Image", imageIcon.getImage());
like image 22
Ujjwal tater Avatar answered Jan 23 '26 20:01

Ujjwal tater



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!