package com.jeecg.util;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.imageio.ImageIO;
public class ImgCrop {
/**
* 改变图片的大小到宽为size,然后高随着宽等比例变化
* @param is 上传的图片的输入流
* @param os 改变了图片的大小后,把图片的流输出到目标OutputStream
* @param size 新图片的宽
* @param format 新图片的格式
* @throws IOException
*/
public static OutputStream resizeImage(BufferedImage bufferedimage,int widht,int height,OutputStream os, String format) throws IOException {
BufferedImage image = new BufferedImage(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();
return os;
}
/**
* * 裁剪图片方法 *
* @param bufferedImage 图像源 *
* @param startX 裁剪开始x坐标 *
* @param startY 裁剪开始y坐标 *
* @param endX 裁剪结束x坐标 *
* @param endY 裁剪结束y坐标 *
* @return
*/
public static BufferedImage cropImage(BufferedImage Image, int startX, int startY, int endX, int endY) {
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);
}
}
return result;
}
public static void main(String[] args) throws IOException {
totalImg(300,400);
}
/**
* 所有图片调用总方法
*/
public static void totalImg(Integer wid,Integer hei) throws IOException, 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 = new File(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 = new File(cachePath);
File[] files = file.listFiles();
while(files.length>0) {
dealImg(cachePath,wid,hei,2);
file = new File(path);
files = file.listFiles();
}
}
/**
* 处理图片
* @param path 文件夹路径
*/
private static void dealImg(String path,Integer wid,Integer hei,Integer type) {
try {
File file = new File(path);
File[] files = file.listFiles();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
Date beginDate = new Date();
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<wid||height<hei) {
while (width<wid||height<hei) {
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<wid||height<hei) {
if (width<=height) {
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 = new Date();
System.out.println(path+"end:"+sdf.format(endDate));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}