java 图片 大小_java改变图片文件尺寸

packagecom.jeecg.util;importjava.awt.Graphics;importjava.awt.image.BufferedImage;importjava.io.File;importjava.io.FileNotFoundException;importjava.io.FileOutputStream;importjava.io.IOException;importjava.io.OutputStream;importjava.text.SimpleDateFormat;importjava.util.Date;importjavax.imageio.ImageIO;public classImgCrop {/*** 改变图片的大小到宽为size,然后高随着宽等比例变化

*@paramis 上传的图片的输入流

*@paramos 改变了图片的大小后,把图片的流输出到目标OutputStream

*@paramsize 新图片的宽

*@paramformat 新图片的格式

*@throwsIOException*/

public static OutputStream resizeImage(BufferedImage bufferedimage,int widht,int height,OutputStream os, String format) throwsIOException {

BufferedImage image= newBufferedImage(widht, height, BufferedImage.TYPE_INT_BGR);

Graphics graphics=image.createGraphics();

graphics.drawImage(bufferedimage,0, 0, widht, height, null);

ImageIO.write(image, format, os);

os.flush();

os.close();returnos;

}/*** * 裁剪图片方法 *

*@parambufferedImage 图像源 *

*@paramstartX 裁剪开始x坐标 *

*@paramstartY 裁剪开始y坐标 *

*@paramendX 裁剪结束x坐标 *

*@paramendY 裁剪结束y坐标 *

*@return

*/

public static BufferedImage cropImage(BufferedImage Image, int startX, int startY, int endX, intendY) {int width =Image.getWidth();int height =Image.getHeight();if (startX == -1) {

startX= 0;

}if (startY == -1) {

startY= 0;

}if (endX == -1) {

endX= width - 1;

}if (endY == -1) {

endY= height - 1;

}

BufferedImage result= new BufferedImage(endX - startX, endY - startY, 4);for (int x = startX; x < endX; ++x) {for (int y = startY; y < endY; ++y) {int rgb =Image.getRGB(x, y);

result.setRGB(x- startX, y -startY, rgb);

}

}returnresult;

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

totalImg(300,400);

}/*** 所有图片调用总方法*/

public static void totalImg(Integer wid,Integer hei) throwsIOException, FileNotFoundException {

File file1= new File("D:/image/");if (!file1.exists()) {

file1.mkdirs();

}

File file2= new File("D:/img/");if (!file2.exists()) {

file2.mkdirs();

}

File file3= new File("D:/imgCache/");if (!file3.exists()) {

file3.mkdirs();

}

File file4= new File("D:/imgCopy/");if (!file4.exists()) {

file4.mkdirs();

}//原始图片路径

String imagePath = "D:/image";

File imageFile= newFile(imagePath);

File[] imageFiles=imageFile.listFiles();for(File fimage : imageFiles) {

BufferedImage bufferedimage=ImageIO.read(fimage);int width =bufferedimage.getWidth();int height =bufferedimage.getHeight();

OutputStream os= new FileOutputStream(new File("D:/img/"+fimage.getName()));

resizeImage(bufferedimage,width,height,os,"jpg");

os.flush();

os.close();

}//过程图片路径

String path = "D:/img";

dealImg(path,wid,hei,1);

String cachePath= "D:/imgCache";

File file= newFile(cachePath);

File[] files=file.listFiles();while(files.length>0) {

dealImg(cachePath,wid,hei,2);

file= newFile(path);

files=file.listFiles();

}

}/*** 处理图片

*@parampath 文件夹路径*/

private static voiddealImg(String path,Integer wid,Integer hei,Integer type) {try{

File file= newFile(path);

File[] files=file.listFiles();

SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");

Date beginDate= newDate();

System.out.println(path+"begin:"+sdf.format(beginDate));for(File f : files) {

BufferedImage bufferedimage=ImageIO.read(f);

Double width= (double) bufferedimage.getWidth();

Double height= (double) bufferedimage.getHeight();

Double w= 0.00;

Double h= 0.00;if (type==1) {if (width

width=width*1.1;

height=height*1.1;

}

OutputStream os= new FileOutputStream(new File("D:/imgCache/"+f.getName()));

resizeImage(bufferedimage,width.intValue(),height.intValue(),os,"jpg");

f.delete();

os.flush();

os.close();continue;

}else if (width>wid&&height>hei) {while (width>wid&&height>hei) {

width=width*0.9;

height=height*0.9;if (width

w=wid.doubleValue();

h= ((wid-width)/wid)*height+height;

}else{

h=hei.doubleValue();

w= ((hei-height)/hei)*width+width;

}

}else{

w=width;

h=height;

}

}

width=w;

height=h;

OutputStream os= new FileOutputStream(new File("D:/imgCache/"+f.getName()));

resizeImage(bufferedimage,width.intValue(),height.intValue(),os,"jpg");

f.delete();

os.flush();

os.close();continue;

}

}//目标将图片裁剪成 宽900,高1200

if (width >wid) {/*开始x坐标 开始y坐标 结束x坐标 结束y坐标*/bufferedimage=cropImage(bufferedimage,(int) ((width - wid) / 2),0,(int) (width - (width-wid) / 2),height.intValue());if (height >hei) {

bufferedimage=cropImage(bufferedimage,0,(int) ((height - hei) / 2),wid,(int) (height - (height - hei) / 2) );

}

}else{if (height >hei) {

bufferedimage=cropImage(bufferedimage,0,(int) ((height - hei) / 2),width.intValue(),(int) (height - (height - hei) / 2) );

}

}

OutputStream os= new FileOutputStream(new File("D:/imgCopy/"+f.getName()));

ImageIO.write(bufferedimage,"jpg", os); //输出裁剪图片

f.delete();

os.flush();

os.close();

}

Date endDate= newDate();

System.out.println(path+"end:"+sdf.format(endDate));

}catch(Exception e) {//TODO Auto-generated catch block

e.printStackTrace();

}

}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值