java将一个图片进行旋转

本文介绍了一个使用Java进行图像旋转的方法,包括如何读取图像文件、计算旋转后的新尺寸、执行旋转操作并保存处理后的图像。
	public static void main(String[] arg) { 

				BufferedImage image = null;
			try {
				image = ImageIO.read(new File("d:\\oyyyyx.jpg"));//读取原始图片
			} catch (IOException e2) {
				// TODO Auto-generated catch block
				e2.printStackTrace();
			}
			int iw = image.getWidth();// 原始图象的宽度

			int ih = image.getHeight();// 原始图象的高度

			int w = 0;

			int h = 0;

			int x = 0;

			int y = 0;
			double ang = Math.toRadians(90);// 将角度转为弧度

			*//**
			 * 
			 *确定旋转后的图象的高度和宽度
			 *//*
			w = ih;

			h = iw;

			x = (w / 2) - (iw / 2);// 确定原点坐标

			y = (h / 2) - (ih / 2);

			BufferedImage rotatedImage = new BufferedImage(w, h, image.getType());

			Graphics2D gs = (Graphics2D) rotatedImage.getGraphics();

			rotatedImage = gs.getDeviceConfiguration().createCompatibleImage(w, h,
					Transparency.OPAQUE);

			AffineTransform at = new AffineTransform();

			at.rotate(ang, w / 2, h / 2);// 旋转图象

			at.translate(x, y);

			AffineTransformOp op = new AffineTransformOp(at,
					AffineTransformOp.TYPE_BICUBIC);

			op.filter(image, rotatedImage);

			image = rotatedImage;
			OutputStream byteOut = null;
			try {
				byteOut = new FileOutputStream(new File(new File("d:\\"), "aa.jpg"));//创建输出流,把被旋转后的图片保存为aa.jpg
			} catch (FileNotFoundException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}

			try {

				ImageOutputStream iamgeOut = ImageIO
						.createImageOutputStream(byteOut);

				ImageIO.write(image, "jpg", iamgeOut);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			

		}
}


工具方法:

public static void rotateBgImg(File bgImg,String realPath,String fileName){
			   
			BufferedImage image = null;
			try {
				image = ImageIO.read(bgImg);
			} catch (IOException e2) {
				// TODO Auto-generated catch block
				e2.printStackTrace();
			}
			int iw = image.getWidth();// 原始图象的宽度

			int ih = image.getHeight();// 原始图象的高度

			int w = 0;

			int h = 0;

			int x = 0;

			int y = 0;
			double ang = Math.toRadians(90);// 将角度转为弧度

			/**
			 * 
			 *确定旋转后的图象的高度和宽度
			 */
			w = ih;

			h = iw;

			x = (w / 2) - (iw / 2);// 确定原点坐标

			y = (h / 2) - (ih / 2);

			BufferedImage rotatedImage = new BufferedImage(w, h, image.getType());

			Graphics2D gs = (Graphics2D) rotatedImage.getGraphics();

			rotatedImage = gs.getDeviceConfiguration().createCompatibleImage(w, h,
					Transparency.OPAQUE);

			AffineTransform at = new AffineTransform();

			at.rotate(ang, w / 2, h / 2);// 旋转图象

			at.translate(x, y);

			AffineTransformOp op = new AffineTransformOp(at,
					AffineTransformOp.TYPE_BICUBIC);

			op.filter(image, rotatedImage);

			image = rotatedImage;
			OutputStream byteOut = null;
			try {
				byteOut = new FileOutputStream(new File(realPath, fileName));
			} catch (FileNotFoundException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}

			try {
				ImageOutputStream iamgeOut = ImageIO
						.createImageOutputStream(byteOut);
				System.out.println(fileName.substring(fileName.lastIndexOf(".")));
				ImageIO.write(image, fileName.substring(fileName.lastIndexOf(".")+1), iamgeOut);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值