Inserting image with JasperReport via parameters

2

I'm learning a little about jasper / ireport and I'm having some trouble generating a report with an image.

I followed some tutorials I found on the internet but I'm making some mistake that I can not see. My test report is extremely simple. Contains only one image. I am compiling the report and passing my image by parameter, however when I generate 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 only one inputStream representing the image, and its get / set methods.

In ireport I created an image attribute and in its expression class I define it as InputStream. and in the image expression I define the IMAGE parameter I created.

Can anyone help me?

Thank you

PS: I'm adding prints the way I set up the report in ireport

    
asked by anonymous 11.07.2014 / 16:10

3 answers

2

I've done it and it always works fine.

General lines for such:

  • The field that will display the image in the report should be of type: java.awt.Image

  • Then you only need to send the reference of the same type ( java.awt.Image ).

This has worked perfectly, any question, from a tip.

    
23.07.2014 / 19:38
2

I'll leave some possibilities, your getimage() method will have to return a inputStream , for example:

parameter.put("IMAGE",new FileInputStream("C:/suaPasta/suaImagem.jpg"));

or

parameter.put("IMAGE",new ByteArrayInputStream(Base64.getDecoder().decode(suaImgEmBase64))); 

The error java.io.ByteArrayInputStream@6df0eb9d usually happens if you are passing a parameter in the map of one type and the report is waiting for another one.

    
15.01.2016 / 17:34
1

I went through the same problem, so I solved it like this:

<image hAlign="Right" vAlign="Middle">
    <reportElement style="StyleSeta" x="1236" y="1" width="10" height="16" uuid="5c3ba9a6-9116-47af-b4c8-231df22634e7">
        <property name="com.jaspersoft.studio.unit.x" value="pixel"/>
    </reportElement>
    <imageExpression><![CDATA[$V{caminhoImagem} + "SetaCrescimentoOperacional.png"]]></imageExpression>
</image>

I get added the element image in my report, and in your image expression I pass the image path inside my project, in my case the variable is:

$V{caminhoImagem} = "http://localhost:8080/RelatoriosWEB/

And concatenate with image name "SetaCrescimentoOperacional.png"

Then the PNG image in the path: "http://localhost:8080/RelatoriosWEB/"SetaCrescimentoOperacional.png" will be added to your report.

    
22.05.2017 / 21:16