Java 将图像作为参数传递 jasperreports

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21143590/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 06:53:30  来源:igfitidea点击:

Passing image as a parameter jasperreports

java

提问by user3114376

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.

我尝试将图像作为参数徽标传递给 ireports,但是当报告出现时,它显示图像的字符串定义而不是徽标。

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

在 ireports 中,我创建了参数徽标并将其表达为对象并从调色板中将图像拖到其上。我对图像表达的图像

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

回答by Nikos Paraskevopoulos

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

我认为您传递图像的方式是可以的。使用它jrxml作为:

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

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

即确保表达式类是java.awt.Image并且表达式本身指向正确的参数。

回答by zsh

you have to put Image icon in your report and set it's Image Expression to image parameter (in your case logo parameter)

你必须把图像图标放在你的报告中,并将它的图像表达式设置为图像参数(在你的情况下是标志参数)

see: https://gilbertadjin.wordpress.com/2009/07/01/inserting-images-from-database-into-jasper-reports/

见:https: //gilbertadjin.wordpress.com/2009/07/01/inserting-images-from-database-into-jasper-reports/

回答by Ujjwal tater

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

尝试在下面创建一个对象类型的参数并将图像的表达式字段设置为$P{Image}

ImageIcon imageIcon = new ImageIcon(new ImageIcon(img).getImage);
parameter.put("Image", imageIcon.getImage());