Dynamic Image in Jasper Report

jrxml content

<image>
                <reportElement x="5" y="5" width="200" height="90"/>
                <imageExpression class="java.awt.Image"><![CDATA[$P{logo}]]></imageExpression>
            </image>


Java Code to convert image from URL into BufferedImage

       URL url = new URL("http://localhost/image/image.jpg");

        // Read the image ...
        InputStream inputStream = url.openStream();

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];

        int n = 0;
        while (-1 != (n = inputStream.read(buffer))) {
            baos.write(buffer, 0, n);
        }
       
        baos.flush();
        byte[] imageInByte = baos.toByteArray();
        baos.close();

        // convert byte array back to BufferedImage
        InputStream in = new ByteArrayInputStream(imageInByte);
        BufferedImage bImageFromConvert = ImageIO.read(in);


Add into Map to pass into jasper

Map inputParameters = new HashMap();
inputParameters.put("logo", bImageFromConvert);

Comments

  1. i' try to implement your example in my code. here my code

    if (conn!=null)
    {

    byte[] imgData = null ;
    String load = "SELECT SP_STAFF_PHOTO FROM STAFF_PHOTO WHERE SP_STAFF_ID='xxx' ";

    try {


    PreparedStatement pstmt_load= conn.prepareStatement(load);
    ResultSet rset_load=pstmt_load.executeQuery ();

    if(rset_load.next())
    {

    sImage = rset_load.getBinaryStream(1);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    // InputStream inputStream = new InputStream();
    byte[] buffer = new byte[1024];

    int n = 0;
    while (-1 != (n = sImage.read(buffer))) {
    baos.write(buffer, 0, n);
    }

    baos.flush();
    byte[] imageInByte = baos.toByteArray();
    baos.close();

    // convert byte array back to BufferedImage
    InputStream in = new ByteArrayInputStream(imageInByte);
    BufferedImage bImageFromConvert = ImageIO.read(in);


    String fileName="report3.jrxml";

    String filePath=request.getRealPath(""+"/WEB-INF/report/exhibition/"+fileName);
    String filePathDir=request.getRealPath(""+"/WEB-INF/report/exhibition/");
    String filePathLogo=request.getRealPath(""+"/cms/exhibition/images/");

    jasperReport = JasperCompileManager.compileReport(filePath);
    Map parameters = new HashMap();
    parameters.put("logo", bImageFromConvert);
    jasperPrint = JasperFillManager.fillReport(jasperReport, parameters,conn);
    response.setContentType("application/pdf");
    response.addHeader("Content-Type", "application/pdf");
    response.setHeader("Cache-Control", "no-cache");
    JasperExportManager.exportReportToPdfStream(jasperPrint,response.getOutputStream());


    }



    rset_load.close();
    pstmt_load.close();


    }
    catch(Exception ee)
    {
    System.out.println(ee);
    }


    }

    ReplyDelete
  2. But getting error, i using jsp and running on tomcat : Here the error:

    incompatible java.awt.image.BufferedImage value assigned to parameter logo in the report3 dataset

    Why ?

    Thanks

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete

Post a Comment

Popular posts from this blog

Simple Invoice Creation With Jasper Report

Auto Increment Oracle Table Id Mapping With JPA Entity