Java图片旋转

2 篇文章 0 订阅

import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import javax.imageio.ImageIO;

/**
 * 图片旋转工具类
 */
public class ImgTest {

	/**
	 * 
	 * @param bufferedImage 图片
	 * @param angel         旋转角度
	 * @return
	 */
	public static BufferedImage rotateImage(BufferedImage bufferedImage, int angel) {
		if (bufferedImage == null) {
			return null;
		}
		if (angel < 0) {
			// 将负数角度,纠正为正数角度
			angel = angel + 360;
		}
		int imageWidth = bufferedImage.getWidth(null);
		int imageHeight = bufferedImage.getHeight(null);
		// 计算重新绘制图片的尺寸
		Rectangle rectangle = calculatorRotatedSize(new Rectangle(new Dimension(imageWidth, imageHeight)), angel);
		// 获取原始图片的透明度
		int type = bufferedImage.getColorModel().getTransparency();
		BufferedImage newImage = null;
		newImage = new BufferedImage(rectangle.width, rectangle.height, type);
		Graphics2D graphics = newImage.createGraphics();
		// 平移位置
		graphics.translate((rectangle.width - imageWidth) / 2, (rectangle.height - imageHeight) / 2);
		// 旋转角度
		graphics.rotate(Math.toRadians(angel), imageWidth / 2, imageHeight / 2);
		// 绘图
		graphics.drawImage(bufferedImage, null, null);
		return newImage;
	}

	/**
	 * 旋转图片
	 * 
	 * @param image 图片
	 * @param angel 旋转角度
	 * @return
	 */
	public static BufferedImage rotateImage(Image image, int angel) {
		if (image == null) {
			return null;
		}
		if (angel < 0) {
			// 将负数角度,纠正为正数角度
			angel = angel + 360;
		}
		int imageWidth = image.getWidth(null);
		int imageHeight = image.getHeight(null);
		Rectangle rectangle = calculatorRotatedSize(new Rectangle(new Dimension(imageWidth, imageHeight)), angel);
		BufferedImage newImage = null;
		newImage = new BufferedImage(rectangle.width, rectangle.height, BufferedImage.TYPE_INT_RGB);
		Graphics2D graphics = newImage.createGraphics();
		// transform
		graphics.translate((rectangle.width - imageWidth) / 2, (rectangle.height - imageHeight) / 2);
		graphics.rotate(Math.toRadians(angel), imageWidth / 2, imageHeight / 2);
		graphics.drawImage(image, null, null);
		return newImage;
	}

	/**
	 * 计算旋转后的尺寸
	 * 
	 * @param src
	 * @param angel
	 * @return
	 */
	private static Rectangle calculatorRotatedSize(Rectangle src, int angel) {
		if (angel >= 90) {
			if (angel / 90 % 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));
	}

	/**
	 * 获取网络图片流(附加)
	 * 
	 * @param url
	 * @return
	 */
	public static InputStream getImageStream(String url) {
		try {
			HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
			connection.setReadTimeout(5000);
			connection.setConnectTimeout(5000);
			connection.setRequestMethod("GET");
			if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
				InputStream inputStream = connection.getInputStream();
				return inputStream;
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	
	
	public static void main(String[] args) {
		String url = "https://img-home.csdnimg.cn/images/20201124032511.png";
		// 将网络图片转为BufferedImage
		try {
			BufferedImage bufferedImage = ImageIO.read(ImgTest.getImageStream(url));
			
			// 调用图片旋转工具类,旋转图片45度
			BufferedImage rotateImage = ImgTest.rotateImage(bufferedImage, 45);
			// 截取URL中的图片名称和后缀
			String fileName = url.substring(url.lastIndexOf("/") + 1, url.length());
			// 截取图片后缀名(.png),以保持图片格式不变
			String imgSuffix = url.substring(url.lastIndexOf(".") + 1, url.length());
			// 将旋转后的图片保存到D盘根目录下
			File file = new File("D:\\a\\", fileName);
			ImageIO.write(rotateImage, imgSuffix, file);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	

}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值