Java 将图像作为参数传递jasperreports

Java 将图像作为参数传递jasperreports,java,Java,我尝试将图像作为参数logo传递给ireports,但当报告出现时,它会显示图像的字符串定义,而不是logo 在ireports中,我创建了参数徽标,将其表达为对象,并从pallete中将图像拖到其上。我对图像进行了表达 private void generateRptForm(String sql, String reportloader) { PreparedStatement pstmt = null; ResultSet rs = null; ByteArrayO

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

在ireports中,我创建了参数徽标,将其表达为对象,并从pallete中将图像拖到其上。我对图像进行了表达

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);
    }
}
私有void生成器表单(字符串sql、字符串报告加载器){
PreparedStatement pstmt=null;
结果集rs=null;
ByteArrayOutputStream baos=null;
连接=空;
试一试{
bas=新的ByteArrayOutputStream();
Class.forName(“com.mysql.jdbc.Driver”).newInstance();
connect=DriverManager.getConnection(“jdbc:mysql://localhost:3306/“+”学校“,用户,密码);
pstmt=connect.prepareStatement(“从id=1的图片中选择图像”);
rs=pstmt.executeQuery();
InputStream imageStream=null;
BuffereImage图像=空;
while(rs.next()){
imageStream=rs.getBinaryStream(1);
image=ImageIO.read(imageStream);
}
System.out.println(“图像…”+图像);
connect.close();
connect.close();
Class.forName(“com.mysql.jdbc.Driver”);
conn=DriverManager.getConnection(“jdbc:mysql://localhost:3306/“+”学校“,用户,密码);
stmt=conn.createStatement();
JasperDesign jd=JRXmlLoader.load(reportloader);
字符串sqltrans=sql;
JRDesignQuery newQuery=新JRDesignQuery();
setText(sqltrans);
jd.setQuery(newQuery);
//从注册表中获取数据
String[]split=null;
String schname=this.getTitle();
split=schname.split(“\\[”);
schname=split[1];
split=schname.split(“\\]”);
字符串[]regdetails=dbutils.checker.regdetails(拆分[0]);
Map param=new HashMap();
参数put(“学校名称”,regdetails[0]);
参数put(“地址”,regdetails[3]);
参数put(“zipcode”,regdetails[4]);
参数put(“电话”,regdetails[5]);
参数put(“位置”,regdetails[1]);
参数put(“国家”,regdetails[2]);
参数put(“电子邮件”,regdetails[6]);
参数放置(“徽标”,图像);
JasperReport jr=jaspecompilemanager.compileReport(jd);
JasperPrint jp=JasperFillManager.fillReport(jr,param,conn);
JasperViewer.viewReport(jp,false);
}捕获(例外e){
showMessageDialog(null,e);
系统输出打印ln(e);
}
}

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

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


也就是说,确保expression类是
java.awt.Image
,并且表达式本身指向正确的参数。

您必须在报表中放置图像图标,并将其图像表达式设置为图像参数(在您的示例中为logo参数)


请参阅:

尝试下面的操作,创建对象类型的参数,并将image的expression字段设置为
$p{image}

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

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

String logoPath = "E:/logo.png";
Map<String, Object> param = new HashMap<String, Object>();
param.put("logo", logoPath);
String logoPath=“E:/logo.png”;
Map param=new HashMap();
参数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字符串。

一个不相关的注意事项:您不需要打开到DB的两个连接,将第一个连接用于第二个查询也可以,而且性能更好。还有一个相关的问题:您如何使用
jrxml
文件中的图像?你能发布相关部分吗?谢谢你的指导。@Nikos Paraskevopoulos,1。你能给我演示/解释一下如何将图像作为参数传递给.jrxml文件吗。2.如何格式化jrxml文件以显示图像。我将不胜感激。