Inserting image with Jasperreport via parameters

Asked

Viewed 6,235 times

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

Parameter

Campo Image

  • 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 field TextField to try to display the image or selected the wrong type in the image component as it is possible to use InputStream, 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, 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!!

4 answers

3

I’ve done it and it always works in a good way.

Broad lines for this:

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

    Tipo da Field

    inserir a descrição da imagem aqui

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

This has worked perfectly, any doubt, gives a hint.

2

I’ll leave some possibilities, your method getimage() 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 mistake java.io.ByteArrayInputStream@6df0eb9d usually happens if you are passing a parameter in the map of one kind and in the report are expecting another.

1

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

<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 got added the image element in my report, and in your image Expression I pass the image path within my project, in my case the variable is:

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

And concateno with the name of the image "SetaCrescimentoOperacional.png"

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

0

if it is a java project, it is possible to pass only the image path inside your Resources that it also finds when compiling your jrxml.

Examples:

String logoPath = getClass().getResource("/images/logo.jpg");
params.put("logo", logoPath);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.