java图片局部缩放_JAVA操作图片裁切与缩放的一个工具类

importjava.awt.Color;importjava.awt.Graphics;importjava.awt.Image;importjava.awt.Rectangle;importjava.awt.geom.AffineTransform;importjava.awt.image.AffineTransformOp;importjava.awt.image.BufferedImage;importjava.io.File;importjava.io.IOException;importjavax.imageio.ImageIO;/*** 图片操作的一个工具类

*@authorwuxinwei

**/

public classImageHepler {/*** 按最大宽高来缩放图片(图片自适应最大宽高)

*@parambitmap 原图

*@paramwidth 最大的宽

*@paramheight 最大的高

*@paramtype 图片格式

*@paramtemp 输出的文件

*@return缩放完后图片的宽和高(int[0]为宽,int[1]为高)

*@throwsIOException*/

public static int[] zoom(BufferedImage bitmap, int width, intheight,

String type, File temp)throwsIOException {if (bitmap == null) {return null;

}if (width < 1 || height < 1) {return null;

}

Image itemp= null;float oldWidth =bitmap.getWidth();float oldHeight =bitmap.getHeight();double ratio = (height / oldHeight)

: (width/ oldWidth);//缩放比例

itemp=bitmap.getScaledInstance(width, height,

BufferedImage.SCALE_SMOOTH);

AffineTransformOp op= newAffineTransformOp(

AffineTransform.getScaleInstance(ratio, ratio),null);

itemp= op.filter(bitmap, null);

ImageIO.write((BufferedImage) itemp, type, temp);int[] wh={itemp.getWidth(null),itemp.getHeight(null)};returnwh;

}/*** 绘制缩放图

*

*@paramimg

* 原图

*@paramwidth

* 目标图宽

*@paramheight

* 目标图高

*@return

*/

private static BufferedImage makeThumbnail(Image img, int width, intheight) {

BufferedImage tag= newBufferedImage(width, height,

BufferedImage.TYPE_INT_RGB);

Graphics g=tag.getGraphics();

g.drawImage(img.getScaledInstance(width, height, Image.SCALE_SMOOTH),0, 0, null);

g.dispose();returntag;

}/*** 裁剪图片

*

*@paramimage

* 原图

*@paramsubImageBounds

* 裁剪矩形框

*@paramsubImageFile

* 保存路径

*@throwsIOException*/

private static voidsaveSubImage(BufferedImage image,

Rectangle subImageBounds, File subImageFile)throwsIOException {

String fileName=subImageFile.getName();

String formatName= fileName.substring(fileName.lastIndexOf('.') + 1);

BufferedImage subImage= newBufferedImage(subImageBounds.width,

subImageBounds.height, BufferedImage.TYPE_INT_RGB);

Graphics g=subImage.getGraphics();if (subImageBounds.width >image.getWidth()|| subImageBounds.height >image.getHeight()) {int left =subImageBounds.x;int top =subImageBounds.y;if (image.getWidth()

left= (int) ((subImageBounds.width - image.getWidth()) / 2);if (image.getHeight()

top= (int) ((subImageBounds.height - image.getHeight()) / 2);

g.setColor(Color.white);

g.fillRect(0, 0, subImageBounds.width, subImageBounds.height);

g.drawImage(image, left, top,null);

ImageIO.write(image, formatName, subImageFile);

}else{//BufferedImage subImage =//image.getSubimage(subImageBounds.x,subImageBounds.y,//subImageBounds.width, subImageBounds.height);

g.drawImage(image.getSubimage(subImageBounds.x, subImageBounds.y,

subImageBounds.width, subImageBounds.height),0, 0, null);

}

g.dispose();

ImageIO.write(subImage, formatName, subImageFile);

}/*** 图片缩放裁剪并保存到指定文件

*

*@paramsrcImageFile

* 原图保存路径

*@paramdescDir

* 目标图保存路径

*@paramwidth

* 缩放后图片宽度

*@paramheight

* 缩放后图片高度

*@paramrect

* 裁剪矩形框

*@throwsIOException*/

public static void cut(String srcImageFile, String descDir, intwidth,int height, Rectangle rect) throwsIOException {

Image image= javax.imageio.ImageIO.read(newFile(srcImageFile));

BufferedImage bImage=makeThumbnail(image, width, height);

saveSubImage(bImage, rect,newFile(descDir));

}/*** 图片缩放裁剪并保存到指定文件

*@paramsrcImageFile 原图保存路径

*@paramdescDir 目标图保存路径

*@paramwidth 缩放后图片宽度

*@paramheight 缩放后图片高度

*@paramrect 裁剪矩形框

*@throwsIOException*/

public static void cut(File srcImageFile, File descDir, intwidth,int height, Rectangle rect) throwsIOException {

Image image=javax.imageio.ImageIO.read(srcImageFile);

BufferedImage bImage=makeThumbnail(image, width, height);

saveSubImage(bImage, rect, descDir);

}/*** 裁切文件的指定部分并保存到指定文件

*@paramtFile 要裁切的文件

*@paramsFile 裁切后的文件

*@paramx 裁切的起始X坐标

*@paramy 裁切的起始Y坐标

*@paramw 裁切的宽

*@paramh 裁切的高

*@return是否成功

*@throwsIOException*/

public static boolean cutToFile(File tFile, File sFile, int x, inty,int w, int h) throwsIOException {

BufferedImage tBi=ImageIO.read(tFile);

Rectangle rec= newRectangle(x, y, w, h);

saveSubImage(tBi, rec, sFile);return true;

}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值