Jump to content
We've recently updated our Privacy Statement, available here ×

How to show blob object image using ireport tool


venujasper

Recommended Posts

Hi ,

 

       I am using ireport tool in my application.There is a requirement to show the image on report.

       I have a blob object field in database to store image.I need to show the blob object field as an image in the report.

       Have any idea? Thanks in advance.

 

Regards,

Kitti

Link to comment
Share on other sites

  • Replies 6
  • Created
  • Last Reply

Top Posters In This Topic

Thanks for reply,

 

I am using mysql database.I have used java.io.InputStream, it is successfully compiled,but when i am running it ,showing error message as "Error displaying report page see the console for details".

 

I was not able see errors on the console. Can i get any images such as .gif, jpg, jpeg etc.

 

Link to comment
Share on other sites

Setting your field and image expression to "java.io.InputStream" will work fine for you as long as the blob contains a GIF, JPEG, or PNG (a binary image).  However, if you have PDF documents stored in the blob data - which is pretty common these days - then it won't work for you, you will get an Error exporting print...Image read failed.  Although the report is produced, there is nothing where the image should appear.

What you have to do is continue to read the blob into a java.io.InputStream field, but then create a scriptlet to utilize a java package to convert it from PDF to InputStream.  I did this using the Apache PDFBox open-source package.  Also, if the PDF is multi-pages, then each page needs to become a separate image - thus you need the image display to actually be an embedded subreport driven off a JRDataSource object containing the list of images. 

Below is the code from the scriptlet.  I hope this helps.

James

Code:
public class ImageScriptlet extends JRDefaultScriptlet{    public ImageScriptlet()    {    }    public void afterDetailEval()        throws JRScriptletException    {        String contentType = ( String ) getFieldValue( "CONTENT_TYPE" );        InputStream docStream = ( InputStream ) getFieldValue( "BLOB_DATA" );        if ( null != contentType )        {            ImageDataSource ds = null;            List images = new ArrayList();                        if ( contentType.equalsIgnoreCase( "application/pdf" ) )            {                try                {                    PDDocument document = PDDocument.load( docStream );                    List pages = document.getDocumentCatalog().getAllPages();                        for ( int i = 0; i < pages.size(); i++ )                    {                        PDPage page = ( PDPage ) pages.get( i );                        images.add( getInputStream( page ) );                    }                        document.close();                }                catch ( IOException e )                {                    System.out.println( "IOException during afterDetailEval in ImageScriptlet" );                    System.out.println( e.getMessage() );                                    }            }            else            {                images.add( docStream );            }            ds = new ImageDataSource( images );            setVariableValue( "BLOB_DATA_SOURCE", ds );        }    }
Link to comment
Share on other sites

  • 4 months later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...