标签:获取 依赖 lse math mat finally info next 中心
在pom.xml
中引入需要的依赖内容:
<dependency> <groupId>com.drewnoakes</groupId> <artifactId>metadata-extractor</artifactId> <version>2.7.0</version> </dependency>
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 }
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; }
注:需将图片正常保存到本地,然后使用 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);
IOS设备上传图片,使用ImageIO.write 图片翻转纠正(JAVA)
标签:获取 依赖 lse math mat finally info next 中心
原文地址:https://www.cnblogs.com/wdnnccey/p/12640580.html