将图像作为参数传递给jasperreports

时间:2014-01-15 16:58:30

标签: java

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

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

4 个答案:

答案 0 :(得分:0)

我认为你传递图像的方式还可以。在jrxml中使用它:

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

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

答案 1 :(得分:0)

您必须在报告中添加图片图标并将其设置为图像表达式到图像参数(在您的案例徽标参数中)

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

答案 2 :(得分:0)

尝试以下操作并创建对象类型的参数,并将图像的表达式字段设置为$P{Image}

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

答案 3 :(得分:0)

除了@Nikos Paraskevopoulos 的回答之外,如果您使用的是最新版本的 jasper,您可以使用以下内容:

String logoPath = "E:/logo.png";
Map<String, Object> param = new HashMap<String, Object>();
param.put("logo", logoPath);

在JRXML文件中修改

<image onErrorType="Blank">
    <reportElement x="0" y="5" width="130" height="40" uuid="71aa5ef6-4718-4cee-8a4b-e696aa90dd05"/>
    <imageExpression><![CDATA[$P{logo}]]></imageExpression>
</image>

这个方法对我有用。我就在把它贴在这里之前试过了。只需将徽标路径视为字符串。只需确保您在 jasper studio 中添加了一个参数作为徽标作为 java 字符串。