java後臺解決上傳圖片翻轉90的問題,有demo,經過測試可用
阿新 • • 發佈:2018-11-05
1.需要加入 依賴
metadata-extractor.jar
依賴如下
<dependencies> <!-- Extracts Exif, IPTC, XMP, ICC and other metadata from image and video files(從影象和視訊檔案中提取EXIF、IPTC、XMP、ICC和其他元資料) --> <dependency> <groupId>com.drewnoakes</groupId> <artifactId>metadata-extractor</artifactId> <version>2.11.0</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies>
2.主要看工具類,ImageUtil,因為是mvc上傳圖片,所以之前引數我參考別人程式碼 改了一下.
package com.huarui.util; import java.awt.Dimension; import java.awt.Graphics2D; import java.awt.Rectangle; import java.awt.RenderingHints; import java.awt.image.BufferedImage; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.util.HashMap; import java.util.Map; import javax.imageio.ImageIO; import com.drew.imaging.ImageMetadataReader; import com.drew.imaging.ImageProcessingException; import com.drew.metadata.Directory; import com.drew.metadata.Metadata; import com.drew.metadata.Tag; public class ImageUtil { public static Map<String, Object> getExif(InputStream inputStream) { Map<String, Object> map = new HashMap<String, Object>(); try { Metadata metadata = ImageMetadataReader.readMetadata(inputStream); map = printExif(metadata); } catch (ImageProcessingException e) { System.out.println(e.getMessage()); } catch (IOException e) { System.out.println(e.getMessage()); } return map; } // 獲取exif資訊,將旋轉角度資訊拿到 private static Map<String, Object> printExif(Metadata metadata) { Map<String, Object> map = new HashMap<String, Object>(); String tagName = null; String desc = null; for (Directory directory : metadata.getDirectories()) { for (Tag tag : directory.getTags()) { tagName = tag.getTagName(); desc = tag.getDescription(); if (tagName.equals("Orientation")) { map.put("Orientation", desc); } } } return map; } public static int getAngle(Map<String, Object> map) { int ro = 0; if(map.get("Orientation")!=null) { String ori = map.get("Orientation").toString(); if (ori.indexOf("90") >= 0) { ro = 1; } else if (ori.indexOf("180") >= 0) { ro = 2; } else if (ori.indexOf("270") >= 0) { ro = 3; } } return ro; } public static BufferedImage getBufferedImg(BufferedImage src,int width, int height, int ro) { int angle = (int) (90 * ro); int type = src.getColorModel().getTransparency(); int wid = width; int hei = height; if (ro % 2 != 0) { int temp = width; width = height; height = temp; } Rectangle re = new Rectangle(new Dimension(width, height)); BufferedImage BfImg = null; BfImg = new BufferedImage(re.width, re.height, type); Graphics2D g2 = BfImg.createGraphics(); g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); g2.rotate(Math.toRadians(angle), re.width / 2,re.height / 2); g2.drawImage(src, (re.width - wid) / 2, (re.height - hei) / 2, null); g2.dispose(); return BfImg; } //獲得圖片的高 public static int getHeight(InputStream is) { BufferedImage src = null; int height = -1; try { src = ImageIO.read(is); height = src.getHeight(); } catch (Exception e) { System.out.println(e.getMessage()); } return height; } //獲得圖片的寬 public static int getWidth(InputStream is) { BufferedImage src = null; int width = -1; try { src = ImageIO.read(is); width = src.getWidth(); } catch (Exception e) { System.out.println(e.getMessage()); } return width; } }
3.Ctroller
package com.huarui.action; import com.huarui.util.ImageUtil; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.multipart.MultipartFile; import javax.imageio.ImageIO; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; @Controller public class UploadController { @RequestMapping("/upload") public @ResponseBody String fileUpload(MultipartFile file) throws IOException { File localFile = new File("e://a.jpg"); String ex = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".")); //圖片翻轉90 int angle = ImageUtil.getAngle(ImageUtil.getExif(file.getInputStream())); BufferedImage bf =ImageUtil.getBufferedImg(ImageIO.read(file.getInputStream()), ImageUtil.getWidth(file.getInputStream()), ImageUtil.getHeight(file.getInputStream()), angle); ImageIO.write(bf, ex.substring(1), localFile); //度問題 return localFile.getAbsolutePath(); } @RequestMapping("/") public String index(){ return "test"; } }
參考程式碼->https://blog.csdn.net/sum__mer/article/details/50847584
親測有效,附上鍊接->
https://github.com/youxiu326/sb_image_extractor.git