java 实现 前台H5图片上传后 旋转图片保存


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;
import org.springframework.mock.web.MockMultipartFile;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.HashMap;
import java.util.Map;

/**
 * 图片工具类
 */
public class ImageUtil {


	/**
	 * 获取exif信息
	 * @param inputStream
	 * @return
	 */
	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信息,将旋转角度信息拿到
	 * @param metadata
	 * @return
	 */
	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;
	}

	/**
	 * 获得图片的宽
	 * @param is
	 * @return
	 */
	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;
	}


	/**
	 *
	 * @param file 要旋转的图片
	 * @param dest 旋转完成保存的图片
	 * @param angle 旋转角度  1 : 90  2:180  3:270
	 */
	public static void imageRotation(File file, File dest, int angle){
		String ex = file.getName().substring(file.getName().lastIndexOf("."));
		//图片翻转90
		try {
			InputStream inputStream= new FileInputStream(file);
			// MultipartFile 类型可以获取到长宽
			BufferedImage bf = ImageUtil.getBufferedImg(ImageIO.read(inputStream), ImageUtil.getWidth(inputStream), ImageUtil.getHeight(inputStream), angle);
			// file 文件 获取不到长宽
//			BufferedImage bf = ImageUtil.getBufferedImg(ImageIO.read(inputStream), 10000, 10000, angle);
			ImageIO.write(bf, ex.substring(1), dest);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		File file = new File("D://smcftp/1.jpg");
		File dest = new File("D://smcftp/2.jpg");
		imageRotation(file,dest,1);
	}

}

github : https://github.com/Zhao2018Mr/ImageRotation

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值