Insert image in Ireport

0

I'm trying to insert an image in 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 in Image Expression like this:

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

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

But it gives an error:

  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 looks 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());
       }
   }
    
asked by anonymous 22.01.2018 / 21:29

1 answer

1

IReport

Change the type of the Report Image field to java.io.InputStream. Creates a parameter variable $P{imagem} of type 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 retrieve the sim from your classpath:

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

Or sim 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 this section:

 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 fine on your case because it is an application DESKTOP and the image is outside more with inputStream would work too:

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

jasper.jrxml

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

    
23.01.2018 / 13:58