如何使用Apache Sanselan库在jpg文件中重写XMP XML?

时间:2012-06-02 19:48:36

标签: java apache-commons jpeg xmp

我想将XMP写入jpg。我使用Apache Sanselan库。在下面的代码中,我试图读取XMP XML并将其再次写入同一文件但得到异常(ImageReadException:Unexpected EOF。)。

File file=new File("./img/file.jpg");//input file
    if(file.canWrite()){
        try {
            String xmpXml = Sanselan.getXmpXml(file);
            JpegXmpRewriter xmpWriter=new JpegXmpRewriter();
            //File newfile = new File("./img/newfile.jpg"); //output file
            //xmpWriter.updateXmpXml(new ByteSourceFile(file), new BufferedOutputStream(new FileOutputStream(newfile)), xmpXml);    
            xmpWriter.updateXmpXml(new ByteSourceFile(file), new BufferedOutputStream(new FileOutputStream(file)), xmpXml);             
        } catch (ImageReadException | IOException | ImageWriteException e) {
            e.printStackTrace();
        }
    }else {
        System.out.println("Can NOT Write");
    } 

如果输入和输出文件不同,则代码可以正常工作。

File newfile = new File("./img/newfile.jpg"); //output file
xmpWriter.updateXmpXml(new ByteSourceFile(file), new BufferedOutputStream(new FileOutputStream(newfile)), xmpXml);

但如果我从同一个文件中读取并写入,我会得到一个例外。

    org.apache.sanselan.ImageReadException: Unexpected EOF.
    at org.apache.sanselan.common.BinaryFileFunctions.readAndVerifyBytes(BinaryFileFunctions.java:129)
    at org.apache.sanselan.formats.jpeg.JpegUtils.traverseJFIF(JpegUtils.java:61)
    at org.apache.sanselan.formats.jpeg.xmp.JpegRewriter.analyzeJFIF(JpegRewriter.java:204)
    at org.apache.sanselan.formats.jpeg.xmp.JpegXmpRewriter.updateXmpXml(JpegXmpRewriter.java:187)
    at Editor$1.mouseClicked(Editor.java:147)
    at java.awt.AWTEventMulticaster.mouseClicked(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$000(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

0 个答案:

没有答案