java 图片旋转保存_Java处理图片旋转、缩放、马赛克

这是一个Java图像处理类,提供了图片缩放、旋转和马赛克化的功能。通过使用AffineTransform进行旋转和缩放操作,以及自定义方法实现马赛克效果,可以对图片进行多种变换并保存结果。
摘要由CSDN通过智能技术生成

importjava.awt.Color;importjava.awt.Graphics;importjava.awt.Graphics2D;importjava.awt.Image;importjava.awt.geom.AffineTransform;importjava.awt.image.AffineTransformOp;importjava.awt.image.BufferedImage;importjava.io.File;importjavax.imageio.ImageIO;/*** 图像处理类.

*

*@authornagsh

**/

public classImageDeal {

String openUrl;//原始图片打开路径

String saveUrl; //新图保存路径

String saveName; //新图名称

String suffix; //新图类型 只支持gif,jpg,png

publicImageDeal(String openUrl, String saveUrl, String saveName,String suffix) {this.openUrl =openUrl;this.saveName =saveName;this.saveUrl =saveUrl;this.suffix =suffix;

}/*** 图片缩放.

*

*@paramwidth

* 需要的宽度

*@paramheight

* 需要的高度

*@throwsException*/

public void zoom(int width, int height) throwsException {double sx = 0.0;double sy = 0.0;

File file= newFile(openUrl);if (!file.isFile()) {throw new Exception("ImageDeal>>>" + file + " 不是一个图片文件!");

}

BufferedImage bi= ImageIO.read(file); //读取该图片//计算x轴y轴缩放比例--如需等比例缩放,在调用之前确保参数width和height是等比例变化的

sx = (double) width /bi.getWidth();

sy= (double) height /bi.getHeight();

AffineTransformOp op= newAffineTransformOp(

AffineTransform.getScaleInstance(sx, sy),null);

File sf= new File(saveUrl, saveName + "." +suffix);

Image zoomImage= op.filter(bi, null);try{

ImageIO.write((BufferedImage) zoomImage, suffix, sf);//保存图片

} catch(Exception e) {

e.printStackTrace();

}

}/*** 旋转

*

*@paramdegree

* 旋转角度

*@throwsException*/

public void spin(int degree) throwsException {int swidth = 0; //旋转后的宽度

int sheight = 0; //旋转后的高度

int x; //原点横坐标

int y; //原点纵坐标

File file= newFile(openUrl);if (!file.isFile()) {throw new Exception("ImageDeal>>>" + file + " 不是一个图片文件!");

}

BufferedImage bi= ImageIO.read(file); //读取该图片//处理角度--确定旋转弧度

degree = degree % 360;if (degree < 0)

degree= 360 + degree;//将角度转换到0-360度之间

double theta = Math.toRadians(degree);//将角度转为弧度//确定旋转后的宽和高

if (degree == 180 || degree == 0 || degree == 360) {

swidth=bi.getWidth();

sheight=bi.getHeight();

}else if (degree == 90 || degree == 270) {

sheight=bi.getWidth();

swidth=bi.getHeight();

}else{

swidth= (int) (Math.sqrt(bi.getWidth() *bi.getWidth()+ bi.getHeight() *bi.getHeight()));

sheight= (int) (Math.sqrt(bi.getWidth() *bi.getWidth()+ bi.getHeight() *bi.getHeight()));

}

x= (swidth / 2) - (bi.getWidth() / 2);//确定原点坐标

y = (sheight / 2) - (bi.getHeight() / 2);

BufferedImage spinImage= newBufferedImage(swidth, sheight,

bi.getType());//设置图片背景颜色

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

gs.setColor(Color.white);

gs.fillRect(0, 0, swidth, sheight);//以给定颜色绘制旋转后图片的背景

AffineTransform at= newAffineTransform();

at.rotate(theta, swidth/ 2, sheight / 2);//旋转图象

at.translate(x, y);

AffineTransformOp op= newAffineTransformOp(at,

AffineTransformOp.TYPE_BICUBIC);

spinImage=op.filter(bi, spinImage);

File sf= new File(saveUrl, saveName + "." +suffix);

ImageIO.write(spinImage, suffix, sf);//保存图片

}/*** 马赛克化.

*@paramsize 马赛克尺寸,即每个矩形的长宽

*@return*@throwsException*/

public boolean mosaic(int size) throwsException {

File file= newFile(openUrl);if (!file.isFile()) {throw new Exception("ImageDeal>>>" + file + " 不是一个图片文件!");

}

BufferedImage bi= ImageIO.read(file); //读取该图片

BufferedImage spinImage = newBufferedImage(bi.getWidth(),

bi.getHeight(), bi.TYPE_INT_RGB);if (bi.getWidth() < size || bi.getHeight() < size || size <= 0) { //马赛克格尺寸太大或太小

return false;

}int xcount = 0; //方向绘制个数

int ycount = 0; //y方向绘制个数

if (bi.getWidth() % size == 0) {

xcount= bi.getWidth() /size;

}else{

xcount= bi.getWidth() / size + 1;

}if (bi.getHeight() % size == 0) {

ycount= bi.getHeight() /size;

}else{

ycount= bi.getHeight() / size + 1;

}int x = 0; //坐标

int y = 0;//绘制马赛克(绘制矩形并填充颜色)

Graphics gs =spinImage.getGraphics();for (int i = 0; i < xcount; i++) {for (int j = 0; j < ycount; j++) {//马赛克矩形格大小

int mwidth =size;int mheight =size;if(i==xcount-1){ //横向最后一个比较特殊,可能不够一个size

mwidth = bi.getWidth()-x;

}if(j == ycount-1){ //同理

mheight =bi.getHeight()-y;

}//矩形颜色取中心像素点RGB值

int centerX =x;int centerY =y;if (mwidth % 2 == 0) {

centerX+= mwidth / 2;

}else{

centerX+= (mwidth - 1) / 2;

}if (mheight % 2 == 0) {

centerY+= mheight / 2;

}else{

centerY+= (mheight - 1) / 2;

}

Color color= newColor(bi.getRGB(centerX, centerY));

gs.setColor(color);

gs.fillRect(x, y, mwidth, mheight);

y= y + size;//计算下一个矩形的y坐标

}

y= 0;//还原y坐标

x = x + size;//计算x坐标

}

gs.dispose();

File sf= new File(saveUrl, saveName + "." +suffix);

ImageIO.write(spinImage, suffix, sf);//保存图片

return true;

}public static void main(String[] args) throwsException {

ImageDeal imageDeal= new ImageDeal("e://1.jpg", "e://", "2", "jpg");//测试缩放

/*imageDeal.zoom(200, 300);*/

//测试旋转

/*imageDeal.spin(90);*/

//测试马赛克

/*imageDeal.mosaic(4);*/}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值