java image zoom_java图片基本操作-缩放,旋转,镜像,拼接

这篇博客介绍了如何使用Java处理图像,包括图像的缩放、旋转、镜像翻转以及图像的拼接操作。通过示例代码详细展示了每个操作的实现步骤,为Java开发者提供了实用的图像处理方法。
摘要由CSDN通过智能技术生成

/*** 缩放*/

public static void zoomByScale(BufferedImage bufImage, double scale) throwsIOException {//获取缩放后的长和宽

int _width = (int) (scale *bufImage.getWidth());int _height = (int) (scale *bufImage.getHeight());//获取缩放后的Image对象

Image _img =bufImage.getScaledInstance(_width, _height, Image.SCALE_DEFAULT);//新建一个和Image对象相同大小的画布

BufferedImage image = newBufferedImage(_width, _height, BufferedImage.TYPE_INT_RGB);//获取画笔

Graphics2D graphics =image.createGraphics();//将Image对象画在画布上,最后一个参数,ImageObserver:接收有关 Image 信息通知的异步更新接口,没用到直接传空

graphics.drawImage(_img, 0, 0, null);//释放资源

graphics.dispose();//使用ImageIO的方法进行输出,记得关闭资源

OutputStream out = new FileOutputStream("/Users/sunxianyan/Downloads/缩放.jpg");

ImageIO.write(image,"jpeg", out);

out.close();

}/*** 旋转*/

public static void rotate(BufferedImage bufImage, int degree) throwsIOException {int w = bufImage.getWidth();//得到图片宽度。

int h = bufImage.getHeight();//得到图片高度。

int swidth = 0; //旋转后的宽度

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

int x;//原点横坐标

int y;//原点纵坐标

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

degree= 360+degree;

}double theta =Math.toRadians(degree);if (degree == 180|| degree ==0||degree==360){

sheight=bufImage.getWidth();

swidth=bufImage.getHeight();

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

sheight=bufImage.getWidth();

swidth=bufImage.getHeight();

}else{

swidth= (int) (Math.sqrt(w * w + h *h));

sheight= (int) (Math.sqrt(w * w + h *h));

}

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

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

BufferedImage spinImage= newBufferedImage(swidth, sheight,bufImage.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(bufImage, spinImage);

ImageIO.write(spinImage,"jpeg", new File("/Users/sunxianyan/Downloads/旋转.jpg"));

}/*** 裁剪*/

public static void clip(BufferedImage bufImage) throwsIOException {

ImageIO.write(bufImage.getSubimage(0, 0, 100, 100), "JPEG", new File("/Users/sunxianyan/Downloads/裁剪.jpg"));

}/*** 镜像*/

public static void mirror(BufferedImage bufImage) throwsIOException {//获取图片的宽高

final int width =bufImage.getWidth();final int height =bufImage.getHeight();//读取出图片的所有像素

int[] rgbs = bufImage.getRGB(0, 0, width, height, null, 0, width);//对图片的像素矩阵进行水平镜像

for (int row = 0; row < height; row++) {for (int col = 0; col < width / 2; col++) {int temp = rgbs[row * width +col];

rgbs[row* width + col] = rgbs[row * width + (width - 1 -col)];

rgbs[row* width + (width - 1 - col)] =temp;

}

}//把水平镜像后的像素矩阵设置回 bufImage

bufImage.setRGB(0, 0, width, height, rgbs, 0, width);

ImageIO.write(bufImage,"JPEG", new File("/Users/sunxianyan/Downloads/镜像.jpg"));

}/*** 拼接*/

public static void add() throwsIOException {

String fileUrl= "https://static.cdn.longmaosoft.com/00039447dd4fa068e1835148c3d5431e.png";

String filepath= "/Users/sunxianyan/Downloads/223233fmbmgbxrmfwmi3mw.jpg";

BufferedImage bufImage= ImageIO.read(newURL(fileUrl));

BufferedImage addImage= ImageIO.read(newFile(filepath));

Graphics2D g2d=(Graphics2D) bufImage.getGraphics().create();//绘制图片(如果宽高传的不是图片原本的宽高, 则图片将会适当缩放绘制)

g2d.drawImage(addImage, 50, 50, addImage.getWidth(), addImage.getHeight(),null);

g2d.dispose();

ImageIO.write(bufImage,"jpeg", new File("/Users/sunxianyan/Downloads/拼接.jpg"));

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值