Insert image in Ireport

Asked

Viewed 1,505 times

0

I’m trying to insert an image into Ireport 3.7.5. When I drag the Image field to the page and place the static url with the image address, it works, but when I put it on Image Expression that way :

System.getProperty("user.dir") + "\\fotos" + "\\"+$F{FOTO}

The above code generate the same address I used in the static url that works

But it makes a mistake:

Error filling print... Byte data not found at Location : C: Program Files (x86) Jaspersoft iReport-3.7.5 photos clob46: 'ICONE3.png' net.sf.jasperreports.engine.Jrexception: Byte data not found at Location : C: Program Files (x86) Jaspersoft iReport-3.7.5 photos clob46: 'ICONE3.png'

[EDIT 1]

My method was like this:

public void relatorio ( String codigo ){
      String arquivo = null;

       try{

           conn = ConnectionFactory.getConnection();

            arquivo = System.getProperty("user.dir") + "/src/relatorio/rel_cadastro.jrxml";
            //System.out.println("Caminho do arquivo jrml: "+arquivo);
           JasperDesign design = JRXmlLoader.load(arquivo);
       //    InputStream path1 = classLoader.getResourceAsStream("/images/logo.jpg");
           String foto = System.getProperty("user.dir") + "\\fotos\\ICONE3.png"; 
           InputStream path = ClassLoader.getSystemResourceAsStream( foto );


           JasperReport jr = JasperCompileManager.compileReport( design );

           HashMap valores = new HashMap();
           valores.put("CODIGO", codigo );
           valores.put("imagem", path);

           JasperPrint impressao = JasperFillManager.fillReport(jr,valores,conn);

           JasperViewer jrViewer = new JasperViewer(impressao, false);

           jrViewer.setVisible(true);
           jrViewer.setDefaultCloseOperation(JasperViewer.DISPOSE_ON_CLOSE);


       }
       catch(JRException e){
           System.out.println("Erro no relatorio: \n"+e.getMessage());
       }
   }

1 answer

1


Ireport

Changes the Image field type of the report to java.io.InputStream. Creates a parameter variable $P{imagem} of the kind InputStream in Ireport.

JAVA:

InputStream imgPath =  new FileInputStream("C://images//sua_imagem.jpg");

HashMap<String, Object> params = new HashMap<String, Object>();  
params.put("imagem",imgPath );

You can recover the yes from your classpath :

String path  = System.getProperty("user.dir") + "\\fotos\\ICONE3.png" );

Or yes in Web application :

    HttpServletRequest request;
    ServletContext context = request.getServletContext(); 
    InputStream path = context.getResourceAsStream("/images/logo.jpg")

If all you want is an Inputstream you can create using new FileInputStream(path), or getResourceAsStream().

Change that passage:

 String foto = System.getProperty("user.dir") + "\\fotos\\ICONE3.png"; 
 InputStream path = ClassLoader.getSystemResourceAsStream( foto );

To:

 String foto = System.getProperty("user.dir") + "\\fotos\\ICONE3.png"; 
 InputStream path =  new FileInputStream(foto);

So it worked in your Sso because it’s an application DESKTOP and the image is more external with inputStream would work also:

HashMap<String, Object> params = new HashMap<String, Object>();  
params.put("imagem", System.getProperty("user.dir") + "\\fotos\\ICONE3.png" );

Jasper.jrxml

Try this on field you want to print only conditionally ... No "*Print When Expression*" only display when ($P{imagem} != null).

  • If you can edit the answer so I can mark as solved

  • Edit as ? Yes I can!

  • Instead of putting Inputstream, just put the System.getProperty("user.dir") + "\\fotos\\ICONE3.png" ) as you did

  • 1

    I have already explained why in your case it worked!!

Browser other questions tagged

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