照片的裁剪和压缩

写入图片文件:

URL url = new URL(photo_path);
URLConnection con = url.openConnection();
String urlPath = con.getURL().getFile();
String fileName = urlPath.substring(urlPath.lastIndexOf("/") + 1);
String filePath = "";
String path = "";
if (fileName != null) {
byte[] buffer = new byte[4 * 1024];
int read;
filePath = this.getClass().getResource("/").getPath() + "upload";
path = filePath + "/" + fileName;
File fileFolder = new File(filePath);
if (!fileFolder.exists()) {
fileFolder.mkdirs();
}
InputStream in = con.getInputStream();
FileOutputStream os = new FileOutputStream(path);
while ((read = in.read(buffer)) > 0) {
os.write(buffer, 0, read);
}
os.close();
in.close();
}

BufferedImage src = InputImage(path);
disposeImage(src, path, Integer.parseInt(photoWidth), Integer.parseInt(photoHeight));
// 将次文件文件进行裁剪
File outfile = new File(path);
cutPhoto(path, outfile, Integer.valueOf(x), Integer.valueOf(y), Integer.valueOf(width),
Integer.valueOf(height));
// 再将裁剪的图片压缩成100*100
BufferedImage srcnew = InputImage(path);
disposeImage(srcnew, path, 100, 100);




读取文件方法:

private BufferedImage InputImage(String srcImgPath) {
BufferedImage srcImage = null;
try {
FileInputStream in = new FileInputStream(srcImgPath);
srcImage = javax.imageio.ImageIO.read(in);
} catch (IOException e) {

e.printStackTrace();
}
return srcImage;
}


压缩图片:

/**
* * 处理图片 * * @param src * @param outImgPath * @param new_w * @param new_h
*/
private void disposeImage(BufferedImage src, String outImgPath, int new_w, int new_h) {
// 得到图片
int old_w = src.getWidth();
// 得到源图宽
int old_h = src.getHeight();
// 得到源图长
BufferedImage newImg = null;
// 判断输入图片的类型
switch (src.getType()) {
case 13:
// png,gifnewImg = new BufferedImage(new_w, new_h,
// BufferedImage.TYPE_4BYTE_ABGR);
break;
default:
newImg = new BufferedImage(new_w, new_h, BufferedImage.TYPE_INT_RGB);
break;
}
Graphics2D g = newImg.createGraphics();
// 从原图上取颜色绘制新图
g.drawImage(src, 0, 0, old_w, old_h, null);
g.dispose();
// 根据图片尺寸压缩比得到新图的尺寸
newImg.getGraphics().drawImage(src.getScaledInstance(new_w, new_h, Image.SCALE_SMOOTH), 0, 0, null);
// 调用方法输出图片文件
OutImage(outImgPath, newImg);
}




/**
* * 将图片文件输出到指定的路径,并可设定压缩质量 * * @param outImgPath * @param newImg * @param
* per
*/
private static void OutImage(String outImgPath, BufferedImage newImg) {
// 判断输出的文件夹路径是否存在,不存在则创建
File file = new File(outImgPath);
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
} // 输出到文件流
try {
String format = outImgPath.substring(outImgPath.lastIndexOf(".") + 1);
ImageIO.write(newImg, format, new File(outImgPath));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}


图片裁剪:

/*
* 图片裁剪
*/
private File cutPhoto(String path, File outfile, int x, int y, int width, int height) {
File srcfile = new File(path);
FileInputStream is = null;
ImageInputStream iis = null;
try {
is = new FileInputStream(srcfile);
Iterator<ImageReader> it = ImageIO.getImageReadersByFormatName(path.substring(path.lastIndexOf(".") + 1));
ImageReader reader = it.next();
iis = ImageIO.createImageInputStream(is);
reader.setInput(iis, true);
ImageReadParam param = reader.getDefaultReadParam();
Rectangle rect = new Rectangle(x, y, width, height);
param.setSourceRegion(rect);
BufferedImage bi = reader.read(0, param);
ImageIO.write(bi, path.substring(path.lastIndexOf(".") + 1), outfile);
} catch (Exception e) {
logger.error("裁剪图片出错!", e);
e.printStackTrace();
}
return outfile;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值