我尝试将图像作为参数徽标传递给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);
}
}
我认为你传递图像的方式还可以。在jrxml
中使用它:
<image>
<reportElement ... />
<imageExpression class="java.awt.Image"><![CDATA[$P{logo}]]></imageExpression>
</image>
即确保表达式类是java.awt.Image
,表达式本身指向正确的参数。
你必须在你的报告中放置图像图标并将其图像表达式设置为图像参数(在你的案例中logo参数)
见:https://gilbertadjin.wordpress.com/2009/07/01/inserting-images-from-database-into-jasper-reports/
尝试下面并创建对象类型的参数,并将图像的表达式字段设置为$P{Image}
ImageIcon imageIcon = new ImageIcon(new ImageIcon(img).getImage);
parameter.put("Image", imageIcon.getImage());