2
I am learning a little about Jasper/ireport and am having some problems generating a report with an image.
I followed some tutorials that I found on the Internet but I’m making a mistake that I’m not seeing. My test report is extremely simple. It contains only one image. I am compiling the report and passing my image by parameter, but when Gero the pdf from the report what appears is
"java.io.Bytearrayinputstream@6df0eb9d"
see my code :
public class ReportGenerator {
/**
* gera o relatório.
* @param image {@link BufferedImage}
*/
public void generateReport(BufferedImage image){
try {
InputStream input = toStream(image);
Projeto projeto = new Projeto();
projeto.setImage(input);
List<Projeto> lista = new ArrayList<Projeto>();
lista.add(projeto);
Map parameter = new HashMap();
parameter.put("IMAGE",projeto.getImage());
ClassLoader classLoader = getClass().getClassLoader();
InputStream inputStream = classLoader.getResourceAsStream("report1.jrxml");
JasperReport jasper = JasperCompileManager.compileReport(inputStream);
JasperPrint print = JasperFillManager.fillReport(jasper, parameter, new JRBeanCollectionDataSource(lista));
JasperExportManager.exportReportToPdfFile(print, "RelatorioClientes.pdf");
} catch (JRException e) {
e.printStackTrace();
}
}
/**
* Converte um {@link BufferedImage} em {@link InputStream}
* @param image {@link BufferedImage}
* @return {@link InputStream}
*/
private InputStream toStream(BufferedImage image){
try {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ImageIO.write(image, "png", bos);
byte[] vetor = bos.toByteArray();
InputStream input = new ByteArrayInputStream(vetor);
return input;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
}
This Project class has as an attribute only an inputStream representing the image, and its get/set methods.
In ireport I created an image attribute and in your Expression class I define as Inputstream. and in the image Expression I define the IMAGE parameter I created.
Someone can help me?
thank you
PS: I’m adding prints the way I set up the report in ireport
The problem is in the report. If this is appearing it is because the method
toString
of the image object is being called. Probably you are using a fieldTextField
to try to display the image or selected the wrong type in the image component as it is possible to useInputStream,
File` and some others. Finally, make sure you are actually using an image component and if you have selected the correct type you are passing to it.– utluiz
utluiz, I’m not using a textField field. I added an image field with no selected image, to pass the image through the Parameter. I attached the print the way I configured it in ireport. Thank you for replying!!
– user8078