在使用ios設備時,上傳圖片,發現圖片旋轉了90度。
正確:

錯誤:

1. 引入pom
在pom.xml
中引入需要的依賴內容:
<dependency> <groupId>com.drewnoakes</groupId> <artifactId>metadata-extractor</artifactId> <version>2.7.0</version> </dependency>
2. 計算圖片翻轉到正常顯示需旋轉的角度數
1 /** 2 * 計算圖片翻轉到正常顯示需旋轉角度 3 */ 4 public static int getRotateAngle(File file) { 5 6 int angel = 0; 7 Metadata metadata = null; 8 try { 9 metadata = ImageMetadataReader.readMetadata(file); 10 int orientation = 0; 11 Iterable<Directory> iterable = metadata.getDirectories(); 12 13 for (Iterator<Directory> iter = iterable.iterator(); iter.hasNext(); ) { 14 Directory dr = iter.next(); 15 if (dr.getString(ExifIFD0Directory.TAG_ORIENTATION) != null) { 16 orientation = dr.getInt(ExifIFD0Directory.TAG_ORIENTATION); 17 } 18 Collection<Tag> tags = dr.getTags(); 19 for (Tag tag : tags) { 20 System.out.println(tag.getTagName() + ": " + tag.getDescription()); 21 } 22 } 23 System.out.println("orientation::::" + orientation); 24 if (orientation == 0 || orientation == 1) { 25 angel = 360; 26 } else if (orientation == 3) { 27 angel = 180; 28 } else if (orientation == 6) { 29 angel = 90; 30 } else if (orientation == 8) { 31 angel = 270; 32 } 33 } catch (Exception e) { 34 e.printStackTrace(); 35 } 36 return angel; 37 }
3. 旋轉圖片-指定度數
1 /** 2 * 旋轉圖片 3 */ 4 public static BufferedImage rotateImage(BufferedImage bufferedImage, int angel) { 5 if (bufferedImage == null) { 6 return null; 7 } 8 if (angel < 0) { 9 // 將負數角度,糾正為正數角度 10 angel = angel + 360; 11 } 12 int imageWidth = bufferedImage.getWidth(null); 13 int imageHeight = bufferedImage.getHeight(null); 14 // 計算重新繪制圖片的尺寸 15 Rectangle rectangle = calculatorRotatedSize(new Rectangle(new Dimension(imageWidth, imageHeight)), angel); 16 // 獲取原始圖片的透明度 17 int type = bufferedImage.getColorModel().getTransparency(); 18 BufferedImage newImage = null; 19 newImage = new BufferedImage(rectangle.width, rectangle.height, type); 20 Graphics2D graphics = newImage.createGraphics(); 21 // 平移位置 22 graphics.translate((rectangle.width - imageWidth) / 2, (rectangle.height - imageHeight) / 2); 23 // 旋轉角度 24 graphics.rotate(Math.toRadians(angel), imageWidth / 2, imageHeight / 2); 25 // 繪圖 26 graphics.drawImage(bufferedImage, null, null); 27 return newImage; 28 }
將圖片保存到本地:
private InputStream savePic(InputStream inputStream, String path) { OutputStream os = null; try { // 2、保存到臨時文件 // 1K的數據緩沖 byte[] bs = new byte[1024]; // 讀取到的數據長度 int len; // 輸出的文件流保存到本地文件 os = new FileOutputStream(path); // 開始讀取 while ((len = inputStream.read(bs)) != -1) { os.write(bs, 0, len); } InputStream input = new FileInputStream(path); return input; } catch (Exception e) { e.printStackTrace(); } finally { // 完畢,關閉所有鏈接 try { os.close(); inputStream.close(); } catch (IOException e) { e.printStackTrace(); } } return null; }
/** * 計算旋轉后的尺寸 * * @param src * @param angel * @return */ private static Rectangle calculatorRotatedSize(Rectangle src, int angel) { if (angel >= 90) { if (angel / 90 % 2 == 1) { int temp = src.height; src.height = src.width; src.width = temp; } angel = angel % 90; } double r = Math.sqrt(src.height * src.height + src.width * src.width) / 2; double len = 2 * Math.sin(Math.toRadians(angel) / 2) * r; double angel_alpha = (Math.PI - Math.toRadians(angel)) / 2; double angel_dalta_width = Math.atan((double) src.height / src.width); double angel_dalta_height = Math.atan((double) src.width / src.height); int len_dalta_width = (int) (len * Math.cos(Math.PI - angel_alpha - angel_dalta_width)); int len_dalta_height = (int) (len * Math.cos(Math.PI - angel_alpha - angel_dalta_height)); int des_width = src.width + len_dalta_width * 2; int des_height = src.height + len_dalta_height * 2; return new java.awt.Rectangle(new Dimension(des_width, des_height)); }
注:需將圖片正常保存到本地,然后使用 getRotateAngle 方法獲取需要旋轉到角度
//先把圖片存到本地
InputStream inputStream = savePic(file.getInputStream(),"/test/a.jpg");
//獲取本地圖片需要旋轉到度數 int rotateAngle = getRotateAngle(new file("/test/a.jpg"));
//讀取BuffereadImage. BufferedImage bi = ImageIO.read(inputStream); //如果需要旋轉 if (rotateAngle != 0) {
//旋轉 bi = rotateImage(bi, rotateAngle); }
//最后使用
ImageIO.write(bi, "jpg", file);