图片旋转工具类(分为:顺时针旋转90度、逆时针旋转90度、旋转180度、水平旋转、垂直旋转)

package com.chenb.test;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

//图片旋转工具类
public class ImageUtils2 {
	
	//顺时针旋转90度(通过交换图像的整数像素RGB 值)
	public static BufferedImage rotateClockwise90(BufferedImage bi) {
		int width = bi.getWidth();
		int height = bi.getHeight();
		BufferedImage bufferedImage = new BufferedImage(height, width, bi.getType());
		for (int i = 0; i < width; i++)
			for (int j = 0; j < height; j++)
				bufferedImage.setRGB(height - 1 - j, width - 1 - i, bi.getRGB(i, j));
		return bufferedImage;
	}

	//逆时针旋转90度(通过交换图像的整数像素RGB 值)
	public static BufferedImage rotateCounterclockwise90(BufferedImage bi) {
		int width = bi.getWidth();
		int height = bi.getHeight();
		BufferedImage bufferedImage = new BufferedImage(height, width, bi.getType());
		for (int i = 0; i < width; i++)
			for (int j = 0; j < height; j++)
				bufferedImage.setRGB(j, i, bi.getRGB(i, j));
		return bufferedImage;
	}
	
	//旋转180度(通过交换图像的整数像素RGB 值)
		public static BufferedImage rotate180(BufferedImage bi) {
			int width = bi.getWidth();
			int height = bi.getHeight();
			BufferedImage bufferedImage = new BufferedImage(width,height,bi.getType());
			for (int i = 0; i < width; i++)
				for (int j = 0; j < height; j++)
					bufferedImage.setRGB( width - i-1,height-j-1, bi.getRGB(i, j));
			return bufferedImage;
		}
		
	//水平翻转
		public static BufferedImage rotateHorizon(BufferedImage bi) {
			int width = bi.getWidth();
			int height = bi.getHeight();
			BufferedImage bufferedImage = new BufferedImage(width,height,bi.getType());
			for (int i = 0; i < width; i++)
				for (int j = 0; j < height; j++)
					bufferedImage.setRGB( width - i-1,j, bi.getRGB(i, j));
			return bufferedImage;
		}
	//垂直翻转
		public static BufferedImage rotateVertical(BufferedImage bi) {
			int width = bi.getWidth();
			int height = bi.getHeight();
			BufferedImage bufferedImage = new BufferedImage(width,height,bi.getType());
			for (int i = 0; i < width; i++)
				for (int j = 0; j < height; j++)
					bufferedImage.setRGB(i,height-1-j, bi.getRGB(i, j));
			return bufferedImage;
		}
		
		//main方法,用于测试
	public static void main(String[] args) throws IOException {
		 // 测试来源图片  
        String pathname = System.getProperty("user.home") + File.separator + "Desktop" + File.separator + "testmm.jpg";  
        File file = new File(pathname);  
        // 测试生成图片  
        String testPath = System.getProperty("user.home") + File.separator + "Desktop" + File.separator + "test.jpg";  
        File outFile = new File(testPath);  
        
        //顺时针旋转90度测试
        BufferedImage image = ImageIO.read(file);  
        image=rotateClockwise90(image);
        //ImageIO.write(image, "png", outFile);  
       
        //逆时针旋转90度测试
        BufferedImage image2 = ImageIO.read(file);  
        image2=rotateCounterclockwise90(image2);
        //ImageIO.write(image2, "png", outFile); 
        
        //旋转180度测试
        BufferedImage image3 = ImageIO.read(file);  
        image3=rotate180(image3);
        //ImageIO.write(image3, "png", outFile);  
       
        //水平旋转测试
        BufferedImage image4 = ImageIO.read(file);  
        image4=rotateHorizon(image4);
        //ImageIO.write(image4, "png", outFile);
        
        //垂直旋转测试
        BufferedImage image5 = ImageIO.read(file);  
        image5=rotateVertical(image5);
        ImageIO.write(image5, "png", outFile);
	}
}


原图:



顺时针旋转90度后:



逆时针旋转90度后:



旋转180度后:



水平旋转后:



垂直旋转后:



评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值