JAVA处理图片旋转

iPhone竖向拍摄照片旋转问题

  1. 导入jar包
		<!-- https://mvnrepository.com/artifact/cn.t/media-util -->
        <dependency>
            <groupId>cn.t</groupId>
            <artifactId>media-util</artifactId>
            <version>1.0.7-RELEASE</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.drewnoakes/metadata-extractor -->
        <dependency>
            <groupId>com.drewnoakes</groupId>
            <artifactId>metadata-extractor</artifactId>
            <version>2.14.0</version>
        </dependency>
  1. 工具类
package com.mieasy.admin.util;

import com.drew.imaging.jpeg.JpegMetadataReader;
import com.drew.imaging.jpeg.JpegProcessingException;
import com.drew.metadata.Directory;
import com.drew.metadata.Metadata;
import com.drew.metadata.MetadataException;
import com.drew.metadata.exif.ExifDirectoryBase;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * @author victor
 * @create 2020-09-29 下午5:23
 */
public class ImageUtil {

	public static int getImgWidth(File file) {
		InputStream is = null;
		BufferedImage src = null;
		int ret = -1;
		try {
			is = new FileInputStream(file);
			src = javax.imageio.ImageIO.read(is);
			ret = src.getWidth(null); // 得到源图宽
			is.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ret;
	}

	public static int getImgHeight(File file) {
		InputStream is = null;
		BufferedImage src = null;
		int ret = -1;
		try {
			is = new FileInputStream(file);
			src = javax.imageio.ImageIO.read(is);
			ret = src.getHeight(null); // 得到源图高
			is.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ret;
	}

	/**
	 * 获取图片正确显示需要旋转的角度(顺时针)
	 * @return
	 */
	public static int getRotateAngleForPhoto(String filePath){

		File file = new File(filePath);
		int angle = 0;
		Metadata metadata;
		try {
			metadata = JpegMetadataReader.readMetadata(file);
			Directory directory = metadata.getFirstDirectoryOfType(ExifDirectoryBase.class);
			if(directory.containsTag(ExifDirectoryBase.TAG_ORIENTATION)){

				// Exif信息中方向  
				int orientation = directory.getInt(ExifDirectoryBase.TAG_ORIENTATION);

				// 原图片的方向信息
				if(6 == orientation ){
					//6旋转90
					angle = 90;
				}else if( 3 == orientation){
					//3旋转180
					angle = 180;
				}else if( 8 == orientation){
					//8旋转90
					angle = 270;
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		} catch (JpegProcessingException e) {
			e.printStackTrace();
		} catch (MetadataException e) {
			e.printStackTrace();
		}
		return angle;
	}

	/**
	 * 旋转手机照片
	 * @return
	 */
	public static String rotatePhonePhoto(String fullPath, int angel){

		BufferedImage src;
		try {
			src = ImageIO.read(new File(fullPath));

			int src_width = src.getWidth(null);
			int src_height = src.getHeight(null);

			Rectangle rect_des = calcRotatedSize(new Rectangle(new Dimension(src_width, src_height)), angel);
			BufferedImage res = new BufferedImage(rect_des.width,rect_des.height,BufferedImage.TYPE_INT_RGB);
			Graphics2D g2 = res.createGraphics();
			g2.translate((rect_des.width - src_width) / 2,
					(rect_des.height - src_height) / 2);
			g2.rotate(Math.toRadians(angel), src_width / 2, src_height / 2);
			g2.drawImage(src, null, null);
			ImageIO.write(res, "jpg", new File(fullPath));
		} catch (IOException e) {
			e.printStackTrace();
		}
		return fullPath;
	}

	/**
	 * 计算旋转参数
	 */
	public static Rectangle calcRotatedSize(Rectangle src, int angel) {
		// if angel is greater than 90 degree,we need to do some conversion.
		if (angel > 90) {
			if (angel / 9 % 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));
	}

	public static void main(String[] args) {
		String path = "/home/victor/Desktop/香-塗香粉(木盒連繩結袋裝).JPG";
		int rotateAngleForPhoto = ImageUtil.getRotateAngleForPhoto(path);
		File file1 = new File(path);
		int imgWidth = ImageUtil.getImgWidth(file1);
		int imgHeight = ImageUtil.getImgHeight(file1);

		System.err.println(rotateAngleForPhoto);
		System.err.print(imgWidth + "+");
		System.err.println(imgHeight);

		String s = ImageUtil.rotatePhonePhoto(path, rotateAngleForPhoto);
		File file = new File(s);
		int imgWidth1 = ImageUtil.getImgWidth(file);
		int imgHeight1 = ImageUtil.getImgHeight(file);
		System.err.println(s);
		System.err.print(imgWidth1 + "+");
		System.err.println(imgHeight1);
	}
}

参考:https://blog.51cto.com/thatway/1627283
   https://blog.csdn.net/u010361786/article/details/80511073

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值