Page 1 of 1

Display Image From SQL Blob Object

PostPosted: Fri Jan 09, 2015 1:14 am
by aduri
I have a Blob object from javaDB and want to display it on a docx page.

he following code snippet was used to convert the blob object to byte array

Code: Select all
HashMap<String, Object> hp = new HashMap<>();

Code: Select all
hp.put("a_picture", resultSet.getBytes("ALUMNUS_PICTURE"));

Code: Select all
byte[] bytes2 = serializeObject(data.get("a_picture"));


this code is to add image to paragraph.
Code: Select all
P paragraph = addImageToParagraph(createInlineImage(wordMLPackage,
                wordMLPackage.getMainDocumentPart(), bytes2, "filename", "alttext", 1, 2));


Code: Select all
private org.docx4j.wml.P addImageToParagraph(Inline inLine) {

        // Now add the anchor in w:p/w:r/w:drawing
        ObjectFactory factory2 = new ObjectFactory();
        org.docx4j.wml.P p = factory.createP();
        org.docx4j.wml.Drawing drawing = factory2.createDrawing();
        R run = factory.createR();
        p.getContent().add(run);
        run.getContent().add(drawing);
        drawing.getAnchorOrInline().add(inLine);

        return p;
    }


this is used to create inline image from the byte array

Code: Select all
    private Inline createInlineImage(WordprocessingMLPackage wordMLPackage2,
            Part sourcePart, byte[] bytes, String filenameHint, String altText,
            int id1, int id2) throws Exception {

        BinaryPartAbstractImage imagePart = BinaryPartAbstractImage.createImagePart(wordMLPackage2,
                sourcePart, bytes);

        return imagePart.createImageInline(filenameHint, altText,
                id1, id2, 800000, 750000, false);

    }


But when i run this code the Netbeans IDE gives an error about the format of the image. the error message is given below.

Code: Select all
org.docx4j.openpackaging.exceptions.Docx4JException: Error checking image format
   at org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage.ensureFormatIsSupported(BinaryPartAbstractImage.java:598)
   at org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage.ensureFormatIsSupported(BinaryPartAbstractImage.java:500)
   at org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage.createImagePart(BinaryPartAbstractImage.java:264)
   at Word.WordDoc.createInlineImage(WordDoc.java:203)
   at Word.WordDoc.createWordDoc(WordDoc.java:103)
   at wa_poly.MyJFrame.jButton3ActionPerformed(MyJFrame.java:1225)
   at wa_poly.MyJFrame.access$400(MyJFrame.java:89)
   at wa_poly.MyJFrame$5.actionPerformed(MyJFrame.java:285)
   at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
   at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2346)
   at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
   at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
   at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
   at java.awt.Component.processMouseEvent(Component.java:6527)
   at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
   at java.awt.Component.processEvent(Component.java:6292)
   at java.awt.Container.processEvent(Container.java:2234)
   at java.awt.Component.dispatchEventImpl(Component.java:4883)
   at java.awt.Container.dispatchEventImpl(Container.java:2292)
   at java.awt.Component.dispatchEvent(Component.java:4705)
   at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4898)
   at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4533)
   at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4462)
   at java.awt.Container.dispatchEventImpl(Container.java:2278)
   at java.awt.Window.dispatchEventImpl(Window.java:2739)
   at java.awt.Component.dispatchEvent(Component.java:4705)
   at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:746)
   at java.awt.EventQueue.access$400(EventQueue.java:97)
   at java.awt.EventQueue$3.run(EventQueue.java:697)
   at java.awt.EventQueue$3.run(EventQueue.java:691)
   at java.security.AccessController.doPrivileged(Native Method)
   at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
   at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:86)
   at java.awt.EventQueue$4.run(EventQueue.java:719)
   at java.awt.EventQueue$4.run(EventQueue.java:717)
   at java.security.AccessController.doPrivileged(Native Method)
   at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
   at java.awt.EventQueue.dispatchEvent(EventQueue.java:716)
   at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
   at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
   at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
   at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
   at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
   at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
Caused by: java.io.IOException: Cannot run program "imconvert": CreateProcess error=2, The system cannot find the file specified
   at java.lang.ProcessBuilder.start(ProcessBuilder.java:1048)
   at java.lang.Runtime.exec(Runtime.java:620)
   at java.lang.Runtime.exec(Runtime.java:450)
   at java.lang.Runtime.exec(Runtime.java:347)
   at org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage.convertToPNG(BinaryPartAbstractImage.java:1279)
   at org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage.ensureFormatIsSupported(BinaryPartAbstractImage.java:582)
   ... 43 more
Caused by: java.io.IOException: CreateProcess error=2, The system cannot find the file specified
   at java.lang.ProcessImpl.create(Native Method)
   at java.lang.ProcessImpl.<init>(ProcessImpl.java:386)
   at java.lang.ProcessImpl.start(ProcessImpl.java:137)
   at java.lang.ProcessBuilder.start(ProcessBuilder.java:1029)
   ... 48 more



any suggestion on the way forward is welcomed.

Re: Display Image From SQL Blob Object

PostPosted: Fri Jan 09, 2015 7:02 am
by jason
What format is your image?

java.io.IOException: Cannot run program "imconvert"


The method convertToPNG is being invoked, to convert image formats which are not supported by Word (eg EPS, PDF),
into ones which are. This requires ImageMagick to be on your system's path (renamed to imconvert); for EPS and PDF images, Ghostscript is also required.

The logic is:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
            if (imagePreloaderFound
                    && (info.getMimeType().equals(ContentTypes.IMAGE_TIFF)
                                        || info.getMimeType().equals(ContentTypes.IMAGE_EMF2) // ImageInfo
                                        || info.getMimeType().equals(ContentTypes.IMAGE_WMF)
                                        || info.getMimeType().equals(ContentTypes.IMAGE_PNG)
                                        || info.getMimeType().equals(ContentTypes.IMAGE_JPEG)
                                        || info.getMimeType().equals(ContentTypes.IMAGE_GIF)
                                        || info.getMimeType().equals(ContentTypes.IMAGE_BMP))) {
                               
                                // If its a format Word supports natively,
                                // do nothing here
                                log.debug(".. supported natively by Word");                                    
                               
            } else if (imageFile != null && bytes != null) {
                               
                                // otherwise (eg if its an EPS or PDF), try to convert it
                                // Although the Word UI suggests you can embed an EPS
                                // directly, Word actually converts it to an EMF;
                                // Word is unable to read a plain EPS image part.
                               
 
Parsed in 0.015 seconds, using GeSHi 1.0.8.4

Re: Display Image From SQL Blob Object

PostPosted: Fri Jan 09, 2015 9:36 am
by aduri
the image is in jpg format.
Note.
when the image is loaded directly from a file , it displays correctly.
for example :
Code: Select all
file = new File("src/resources/images/logo.jpg");
        bytes = convertImageToByteArray(file);


but when stored and retrieved as a blob object, docx4j
complains about the format.

Re: Display Image From SQL Blob Object

PostPosted: Fri Jan 09, 2015 4:11 pm
by jason
Well, docx4j normally wouldn't have a problem loading a jpeg. Maybe yours is base64 encoded, or truncated?

I suggest you compare the size of your blob to the size of your logo.jpg file, then look at the first few bytes of each.