3

Apache Commons Imaging을 사용하여 TIFF 이미지에 EXIF ​​데이터를 쓰려면 어떻게해야합니까?Apache Commons Imaging을 사용하여 TIFF 파일에 EXIF ​​데이터 쓰기

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