Java 使用Apache Commons Imaging将EXIF数据写入TIFF文件
如何使用Apache Commons Imaging将EXIF数据写入TIFF图像 这就是我所尝试的:Java 使用Apache Commons Imaging将EXIF数据写入TIFF文件,java,image-processing,metadata,exif,apache-commons-imaging,Java,Image Processing,Metadata,Exif,Apache Commons Imaging,如何使用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 outp
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?但是我应该使用哪个类呢?ExifRewriter是org.apache.commons.imaging.formats.jpeg
包的工具,因此它不适用于TIFF格式
要将EXIF和标记写入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);
}
这也可能有帮助
BufferedImage bufferedImage = Imaging.getBufferedImage(out.toByteArray());
File imageFile = new File(outputFileName);
final Map<String, Object> optionalParams = new HashMap<String, Object>();
TiffOutputSet tiffExif = new TiffOutputSet();
tiffExif.addRootDirectory().add(MicrosoftTagConstants.EXIF_TAG_XPTITLE, "Title");
optionalParams.put("EXIF", tiffExif);
Imaging.writeImage(bufferedImage, imageFile, ImageFormats.TIFF, optionalParams);
BufferedImage BufferedImage=Imaging.getBufferedImage(out.toByteArray());
文件imageFile=新文件(outputFileName);
final Map optionalParams=新HashMap();
TiffOutputSet tiffExif=新的TiffOutputSet();
tiffExif.addRootDirectory().add(MicrosoftTagConstants.EXIF_TAG_XPTITLE,“Title”);
optionalParams.put(“EXIF”,tiffExif);
Imaging.writeImage(BuffereImage、imageFile、ImageFormats.TIFF、optionalParams);
投票表决该问题,因为我有相同的问题。
BufferedImage bufferedImage = Imaging.getBufferedImage(out.toByteArray());
File imageFile = new File(outputFileName);
final Map<String, Object> optionalParams = new HashMap<String, Object>();
TiffOutputSet tiffExif = new TiffOutputSet();
tiffExif.addRootDirectory().add(MicrosoftTagConstants.EXIF_TAG_XPTITLE, "Title");
optionalParams.put("EXIF", tiffExif);
Imaging.writeImage(bufferedImage, imageFile, ImageFormats.TIFF, optionalParams);