Insert image in Ireport


  • QA Engineer

    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 like this:

    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());
       }
    

    }



  • IReport

    Change Report Image Field Type to java.io.InputStream. Creates a parameter variable $P{imagem} type InputStream 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 the 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 this 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 out in your pit because it is an application DESKTOP and the image is external more with inputStream would be right 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 show when ($P{imagem} != null).




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2