ImageIO.read()始终旋转我上传的图片 [英] ImageIO.read( ) always rotates my uploaded picture
问题描述
我想创建一个允许用户将其图像上传到服务器的Web应用程序。当他们点击发送时,他们的图像将被上传到服务器(多部分)。在保存之前,我想对图像进行一些操作,所以我决定使用..
I want to create a web application that allow users to upload their image to the server. When they click send, their image will be uploaded to the server (multipart). Before saving, I want to make some operation with the image, so I decided to use ..
ImageIO.read(InputStream)
ImageIO.read(InputStream)
获取BufferedImage对象
to get BufferedImage object
这里是代码:
public static BufferedImage getBufferedImageFromMultipartFile(MultipartFile file)
throws APIException
{
BufferedImage bi = null;
try
{
bi = ImageIO.read(file.getInputStream());
}
catch (IOException e)
{
throw new APIException(ErrorCode.SERVER_ERROR, e);
}
return bi;
}
问题是当我尝试上传高度超过宽度的图片时如
3264 x 2448(高x宽),结果总是一个已经旋转的图像(2448 x 3264)。
The problem is when I try to upload a picture that has height more than width such as 3264 x 2448 (height x width), the result always an image that has been rotated (2448 x 3264).
有没有任何解决方案解决这个问题 ?
Is there any solution to solve this problem ?
这是一个错误还是任何已定义的API规范?
Is this a bug or any defined API specification ?
Thx。
PS。对不起我的英文:D
PS. sorry for my english :D
推荐答案
ImageIO.read()无法读取图像的方向移动设备。
ImageIO.read( ) can't read the orientation of the image if it was taken with mobile device.
我使用元数据提取器来读取元数据,我认为这是一个很好的解决方案:
github.com/drewnoakes/metadata-extractor/wiki
I used metadata-extractor to read metadata, i think it's a good solution: github.com/drewnoakes/metadata-extractor/wiki
<dependency>
<groupId>com.drewnoakes</groupId>
<artifactId>metadata-extractor</artifactId>
<version>2.7.2</version>
</dependency>
在exif目录中提交的读取方向:
Read orientation filed in exif directory:
ExifIFD0Directory exifIFD0 = metadata.getDirectory(ExifIFD0Directory.class);
int orientation = exifIFD0.getInt(ExifIFD0Directory.TAG_ORIENTATION);
switch (orientation) {
case 1: // [Exif IFD0] Orientation - Top, left side (Horizontal / normal)
return null;
case 6: // [Exif IFD0] Orientation - Right side, top (Rotate 90 CW)
return Rotation.CW_90;
case 3: // [Exif IFD0] Orientation - Bottom, right side (Rotate 180)
return Rotation.CW_180;
case 8: // [Exif IFD0] Orientation - Left side, bottom (Rotate 270 CW)
return Rotation.CW_270;
}
(旋转是org.imgscalr.Scalr框架中的一个类我使用ti旋转图像)。
(Rotation is a class from the org.imgscalr.Scalr framework I use ti rotate image).
这篇关于ImageIO.read()始终旋转我上传的图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!