• java后台解决上传图片翻转90的问题,有demo,经过测试可用


    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

  • 相关阅读:
    对于服务器AdminServer, 与计算机Machine-0相关联的节点管理器无法访问
    C语言面试题目之指针和数组
    Go数据类型之基本数据类型
    【转载】虚拟地址与虚拟内存的理解
    const变量可以修改么?
    【转载】内联函数 —— C 中关键字 inline 用法解析
    【转载】抓包工具tcpdump用法说明
    【转载】网络编程面试题
    [leetcode]颠倒整数
    [leetcode]反转字符串
  • 原文地址:https://www.cnblogs.com/youxiu326/p/10540766.html
Copyright © 2020-2023  润新知