问题描述
我想创建一个 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)
获取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 as3264 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 ?
谢谢.
附注.对不起我的英语 :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;
}
(Rotation 是 org.imgscalr.Scalr 框架中的一个类,我使用 ti 旋转图像).
(Rotation is a class from the org.imgscalr.Scalr framework I use ti rotate image).
这篇关于ImageIO.read() 总是旋转我上传的图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!