java - 为什么 Google Appengine 的图像 API 提供的图像方向不正确?
问题描述
如果您使用 AppEngine图像 API创建图像, 则输出图像没有 Exif。如果源图像在 Exif 中设置了方向标志,则不会将其保留到输出图像中,因此会在用户看来是旋转的。
有没有办法告诉将ImagesServiceFactory
Exif 传递给输出图像?
解决方案
到目前为止,我发现的最好方法是使用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();
这应该足以让你继续前进。可能的 Exif 方向值(如果存在)是:
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 解析步骤。欢迎更好的解决方案:)
推荐阅读
- python - Python是否在每个步骤中计算列表理解条件?
- python - 如果 K 相同,为什么 Kmeans 会产生相同的质心
- php - 未找到列:1054 'where 子句'中的未知列 'MONTH(date)'
- java - FnProject java 函数,其依赖项托管在私有存储库上
- python - 在 Visual Studio (VS) 代码中调试 Python/C++ 的混合代码
- excel - Excel 插件:单击指向不同域的链接有效,但使用 JS 更改位置无效
- python - 步骤功能:批处理总是在异常抛出时返回 State.TaskFailed 错误
- javascript - 如何从 Crome Network 获取数据
- r - 如何创建一个组合不同变量 (R) 的所有值的数据框?
- python - 找到从 A 到 B 并经过 C 的圆形路径