如果使用AppEngine Images API创建图像,则输出图像没有Exif。如果源图像在Exif中设置了方向标记,则该标记不会保留在输出图像中,因此会向用户显示为旋转状态。
是否可以告诉ImagesServiceFactory
将Exif传递到输出图像?
答案 0 :(得分:0)
到目前为止,我发现最好的方法是简单地使用a java Exif parser读取Orientation标志,然后对生成的图像进行旋转变换,以使像素实际旋转。这是一个简化的示例:
byte[] sourceImage = yourCode();
int orientation = 1;
try {
Metadata metadata = ImageMetadataReader.readMetadata(new ByteArrayInputStream(sourceImage));
orientation = metadata.getFirstDirectoryOfType(ExifIFD0Directory.class).getInt(ExifIFD0Directory.TAG_ORIENTATION);
} catch (Throwable t) {
log.log(Level.INFO, "Failed to extract orientation", t);
}
Image destinationImage = ImagesServiceFactory.makeImage(sourceImage);
Transform rotate;
switch (orientation) {
case (EXIF_ORIENTATION_90): {
rotate = ImagesServiceFactory.makeRotate(90);
break;
}
case (EXIF_ORIENTATION_180): {
rotate = ImagesServiceFactory.makeRotate(180);
break;
}
case (EXIF_ORIENTATION_270): {
rotate = ImagesServiceFactory.makeRotate(270);
break;
}
default:
rotate = ImagesServiceFactory.makeRotate(0); // anything else, no rotate
}
destinationImage = ImagesServiceFactory.getImagesService().applyTransform(rotate, destinationImage);
final byte[] destinationImageData = destinationImage.getImageData();
那应该足以使您前进。 Possible Exif orientation values(如果有)是:
1 = Horizontal (normal)
2 = Mirror horizontal
3 = Rotate 180
4 = Mirror vertical
5 = Mirror horizontal and rotate 270 CW
6 = Rotate 90 CW
7 = Mirror horizontal and rotate 90 CW
8 = Rotate 270 CW
我希望不必这样做,因为它需要不必要的转换以及额外的Exif解析步骤。更好的解决方案欢迎您:)