2016-10-25 33 views
3

Apache Commons Imagingを使用してEXIFデータをTIFFイメージに書き込むにはどうすればよいですか?Apache Commons Imagingを使用してEXIFデータをTIFFファイルに書き込む

File img = new File("pic.tif"); 
File dst = new File("out.tif"); 
try (FileOutputStream fos = new FileOutputStream(dst); 
    OutputStream os = new BufferedOutputStream(fos)) { 

    TiffOutputSet outputSet = null; 

    final ImageMetadata metadata = Imaging.getMetadata(img); 
    final TiffImageMetadata tiffMetadata = (TiffImageMetadata) metadata; 
    outputSet = tiffMetadata.getOutputSet(); 

    if (null == outputSet) { 
     outputSet = new TiffOutputSet(); 
    } 

    // New York City 
    final double longitude = -74.0; 
    final double latitude = 40 + 43/60.0; 
    outputSet.setGPSInDegrees(longitude, latitude); 

    new ExifRewriter().updateExifMetadataLossless(img, os, outputSet); 
} 

が、私はこのエラーを得た:

Exception in thread "main" org.apache.commons.imaging.ImageReadException: Not a Valid JPEG File: doesn't begin with 0xffd8 
    at org.apache.commons.imaging.common.BinaryFunctions.readAndVerifyBytes(BinaryFunctions.java:134) 
    at org.apache.commons.imaging.formats.jpeg.JpegUtils.traverseJFIF(JpegUtils.java:56) 
    at org.apache.commons.imaging.formats.jpeg.exif.ExifRewriter.analyzeJFIF(ExifRewriter.java:186) 
    at org.apache.commons.imaging.formats.jpeg.exif.ExifRewriter.updateExifMetadataLossless(ExifRewriter.java:376) 
    at org.apache.commons.imaging.formats.jpeg.exif.ExifRewriter.updateExifMetadataLossless(ExifRewriter.java:298) 
    at Test.main(Test.java:94) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:498) 
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147) 

ExifRewriterクラスはTIFFをサポートしていないことを示していると思われる

これは私が試した何ですか?しかし、どのクラスを使うべきですか?

+0

私は同じ質問があるので質問をアップしてください。 – Omnipresent

答えて

2

ExifRewriterはorg.apache.commons.imaging.formats.jpegパッケージのツールであるため、TIFF形式では機能しません。あなたはその後、上書きしたい場合があります

BufferedImage img = Imaging.getBufferedImage(f); 
byte[] imageBytes = Imaging.writeImageToBytes(img, ImageFormats.TIFF, new HashMap<>()); 

File ex = new File(FileUtils.getBaseFileName(f) + "_exif." + FileUtils.getExtension(f)); 
try(FileOutputStream fos = new FileOutputStream(ex); 
    OutputStream os = new BufferedOutputStream(fos)) { 
    new TiffImageWriterLossless(imageBytes).write(os, outputSet); 
} 

:TIFFファイルにEXIFとタグを書き込むためには

、あなたがそれを読んでする必要があなたのOutputSet用に作成されたタグとそれを再書き込みexif-edのオリジナルファイル

Files.delete(Paths.get(f.toURI())); 
Files.move(Paths.get(ex.toURI()), Paths.get(f.toURI())); 
関連する問題