1、图片下载/***
* 下载网络图片到本地
*
* @param urlString
* @param filename
* @throws Exception
*/
public String downloadImg(String urlString, String filePath) throws Exception {
String filename;
OutputStream os = null;
InputStream is = null;
try {
// 构造URL
URL url = new URL(urlString);
filename = url.getPath().substring(url.getPath().lastIndexOf("/") + 1);
// 打开连接
URLConnection con = url.openConnection();
// 输入流
is = con.getInputStream();
// 8K的数据缓冲
byte[] bs = new byte[1024*8];
// 读取到的数据长度
int len;
// 输出的文件流
os = new FileOutputStream(filePath);
// 开始读取
while ((len = is.read(bs)) != -1) {
os.write(bs, 0, len);
}
os.flush();
return filename;
} catch (Exception e) {
throw new Exception(e);
}finally{
// 完毕,关闭所有链接
StreamUtil.close(os);
StreamUtil.close(is);
}
}
2、图片备份/**
* 图片复制
* @param sourceDir 源文件目录
* @param targetDir 目标文件目录
*/
public void copyImage(String sourceDir,String targetDir) {
try{
File file=new File(sourceDir);
Image image =ImageIO.read(file);
int width=image.getWidth(null);
int height=image.getHeight(null);
BufferedImage imageTag=new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
imageTag.getGraphics().drawImage(image,0,0,width,height,null);
FileOutputStream out=new FileOutputStream(targetDir);
JPEGImageEncoder encoder=JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(imageTag);
param.setQuality(0.95f, true);
encoder.encode(imageTag, param);
out.close();
}catch (IOException e) {
e.printStackTrace();
}
}
3、图片加水印/**注意:坐标从右下角开始,可以根据画图软件确定坐标
* 方法功能:图片加水印
*/
public Boolean pressWaterImg(String pressImg, String targetImg, int x, int y) {
boolean ret = false;
OutputStream out = null;
try {
File dirFile = new File(targetImg);
boolean bFile = dirFile.exists();
if (!bFile) {
return ret;
}
// 目标文件
File _file = new File(targetImg);
Image src = ImageIO.read(_file);
int wideth = src.getWidth(null);
int height = src.getHeight(null);
BufferedImage image = new BufferedImage(wideth, height, BufferedImage.TYPE_INT_RGB);
Graphics g = image.createGraphics();
g.drawImage(src, 0, 0, wideth, height, null);
// 水印文件
File _filebiao = new File(pressImg);
Image src_biao = ImageIO.read(_filebiao);
int wideth_biao = src_biao.getWidth(null);
int height_biao = src_biao.getHeight(null);
x=wideth-x-wideth_biao;//默认从右下角坐标开始计算偏移量
y=height-y-height_biao;
g.drawImage(src_biao, x, y, wideth_biao, height_biao, null);
// 水印文件结束
g.dispose();
out = new FileOutputStream(targetImg);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(image);
param.setQuality(0.95f, true);
encoder.encode(image,param);
ret = true;
} catch (Exception e) {
e.printStackTrace();
}finally{
StreamUtil.close(out);
}
return ret;
}
4、/**
* 方法功能:压缩图片大小为固定大小
*
* @param srcImgPath
* 源文件的路径(带名称的)
* @param targetPath
* 目标文件的路径
* @param imgName
* 目标文件的名称
*/
public Boolean compressImg(String srcImgPath, String targetPath, String targetName) {
boolean ret = false;
FileOutputStream out = null;// 文件输出流
File targetFile = null; // 目标文件夹对象
File srcFile = null; // 原文件夹对象
String imgName = ""; // 原文件夹的名字
try {
/*
* 判断目标路径目录是否存在,不存在,做新建
*/
targetFile = new File(targetPath);
boolean bFile = targetFile.exists();
if (!bFile) {
bFile = targetFile.mkdirs();
if (!bFile) {
return ret;
}
}
/*
* 判断源路径目录是否存在,不存在,错误返回
*/
srcFile = new File(srcImgPath);
bFile = srcFile.exists();
if (!bFile) {
return ret;
}
imgName = targetName.substring(0, targetName.lastIndexOf("."));
// 获取config配置文件中的图片尺寸字符串
String imageSize = getImageSizeConfig("goodsImgSize");
if (imageSize != null && !imageSize.equals("")) {
String[] imageSizeAryC = imageSize.split(",");
for (int i = 0; i < imageSizeAryC.length; i++) {
String imageSizeI=imageSizeAryC[i];
String[] imageSizeAry = imageSizeI.split(":");
/* ============= 图片处理 ================== */
// 建立输出流
out = new FileOutputStream(targetPath + separator + imgName + "_" + imageSizeAry[0] + ".jpg");
out.flush();
// 依照固定大小画图区域
// width = 426;
// height = 284;
resize(srcImgPath, Integer.parseInt(imageSizeAry[0]), Integer.parseInt(imageSizeAry[1]), false, out);
out.close();
}
ret = true;
}
} catch (Exception e) {
e.printStackTrace();
}
return ret;
}
5、/**
* 方法功能:处理图片等比例压缩
*
* @param filePath
* 图片路径
* @param height
* 高度
* @param width
* 宽度
* @param blean
* 比例不对时是否需要补白
* @author liu_huibin 20110114
*/
public void resize(String filePath, int width, int height, boolean blean, FileOutputStream out) {
try {
double ratio = 0.0; // 缩放比例
BufferedImage tag1 = null;// 生成指定大小的图片域
File f = new File(filePath);
BufferedImage bi = ImageIO.read(f);
if (bi==null||"".equals(bi)) {
throw new OpenException(ErrorCode.IMAGE_FORMAT_BAD,"商品主图格式错误");
}
Image itemp = bi.getScaledInstance(width, height, bi.SCALE_SMOOTH);
// 计算比例
if ((bi.getHeight() > height) && (bi.getWidth() > width)) {
double a =(double)bi.getHeight() / height;
double b=(double)bi.getWidth() / width;
if (a>=b) {
ratio = (new Integer(height)).doubleValue() / bi.getHeight();
}else {
ratio = (new Integer(width)).doubleValue() / bi.getWidth();
}
AffineTransformOp op = new AffineTransformOp(AffineTransform.getScaleInstance(ratio, ratio), null);
itemp = op.filter(bi, null);
}else if ((bi.getHeight() >= height) || (bi.getWidth() >= width)) {
if (bi.getHeight() > bi.getWidth()) {
ratio = (new Integer(height)).doubleValue() / bi.getHeight();
}else{
ratio = (new Integer(width)).doubleValue() / bi.getWidth();
}
AffineTransformOp op = new AffineTransformOp(AffineTransform.getScaleInstance(ratio, ratio), null);
itemp = op.filter(bi, null);
}
// 转换图片时转化,使图片不再失真
ResampleOp resampleOp = new ResampleOp(itemp.getWidth(null), itemp.getHeight(null));
BufferedImage rescaledTomato = resampleOp.filter(bi, null);
// 依照固定大小画图区域
tag1 = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);//画幕注意,为要得到的大小如:426*284
Graphics2D graphics2D = tag1.createGraphics(); //压缩图片不是800*800是456*245,这样压缩需要空白
graphics2D.setBackground(Color.WHITE);//补白或其他颜色
graphics2D.clearRect(0, 0, width, height); //画幕大小清理
graphics2D.drawImage(rescaledTomato, 0, 0, rescaledTomato.getWidth(), rescaledTomato.getHeight(), null);
// 画图操作
// JPEGCodec.createJPEGEncoder(out).encode(tag1);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(tag1);
param.setQuality(0.95f, true);
encoder.encode(tag1, param);
} catch (IOException e) {
e.printStackTrace();
}
}
* 下载网络图片到本地
*
* @param urlString
* @param filename
* @throws Exception
*/
public String downloadImg(String urlString, String filePath) throws Exception {
String filename;
OutputStream os = null;
InputStream is = null;
try {
// 构造URL
URL url = new URL(urlString);
filename = url.getPath().substring(url.getPath().lastIndexOf("/") + 1);
// 打开连接
URLConnection con = url.openConnection();
// 输入流
is = con.getInputStream();
// 8K的数据缓冲
byte[] bs = new byte[1024*8];
// 读取到的数据长度
int len;
// 输出的文件流
os = new FileOutputStream(filePath);
// 开始读取
while ((len = is.read(bs)) != -1) {
os.write(bs, 0, len);
}
os.flush();
return filename;
} catch (Exception e) {
throw new Exception(e);
}finally{
// 完毕,关闭所有链接
StreamUtil.close(os);
StreamUtil.close(is);
}
}
2、图片备份/**
* 图片复制
* @param sourceDir 源文件目录
* @param targetDir 目标文件目录
*/
public void copyImage(String sourceDir,String targetDir) {
try{
File file=new File(sourceDir);
Image image =ImageIO.read(file);
int width=image.getWidth(null);
int height=image.getHeight(null);
BufferedImage imageTag=new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
imageTag.getGraphics().drawImage(image,0,0,width,height,null);
FileOutputStream out=new FileOutputStream(targetDir);
JPEGImageEncoder encoder=JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(imageTag);
param.setQuality(0.95f, true);
encoder.encode(imageTag, param);
out.close();
}catch (IOException e) {
e.printStackTrace();
}
}
3、图片加水印/**注意:坐标从右下角开始,可以根据画图软件确定坐标
* 方法功能:图片加水印
*/
public Boolean pressWaterImg(String pressImg, String targetImg, int x, int y) {
boolean ret = false;
OutputStream out = null;
try {
File dirFile = new File(targetImg);
boolean bFile = dirFile.exists();
if (!bFile) {
return ret;
}
// 目标文件
File _file = new File(targetImg);
Image src = ImageIO.read(_file);
int wideth = src.getWidth(null);
int height = src.getHeight(null);
BufferedImage image = new BufferedImage(wideth, height, BufferedImage.TYPE_INT_RGB);
Graphics g = image.createGraphics();
g.drawImage(src, 0, 0, wideth, height, null);
// 水印文件
File _filebiao = new File(pressImg);
Image src_biao = ImageIO.read(_filebiao);
int wideth_biao = src_biao.getWidth(null);
int height_biao = src_biao.getHeight(null);
x=wideth-x-wideth_biao;//默认从右下角坐标开始计算偏移量
y=height-y-height_biao;
g.drawImage(src_biao, x, y, wideth_biao, height_biao, null);
// 水印文件结束
g.dispose();
out = new FileOutputStream(targetImg);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(image);
param.setQuality(0.95f, true);
encoder.encode(image,param);
ret = true;
} catch (Exception e) {
e.printStackTrace();
}finally{
StreamUtil.close(out);
}
return ret;
}
4、/**
* 方法功能:压缩图片大小为固定大小
*
* @param srcImgPath
* 源文件的路径(带名称的)
* @param targetPath
* 目标文件的路径
* @param imgName
* 目标文件的名称
*/
public Boolean compressImg(String srcImgPath, String targetPath, String targetName) {
boolean ret = false;
FileOutputStream out = null;// 文件输出流
File targetFile = null; // 目标文件夹对象
File srcFile = null; // 原文件夹对象
String imgName = ""; // 原文件夹的名字
try {
/*
* 判断目标路径目录是否存在,不存在,做新建
*/
targetFile = new File(targetPath);
boolean bFile = targetFile.exists();
if (!bFile) {
bFile = targetFile.mkdirs();
if (!bFile) {
return ret;
}
}
/*
* 判断源路径目录是否存在,不存在,错误返回
*/
srcFile = new File(srcImgPath);
bFile = srcFile.exists();
if (!bFile) {
return ret;
}
imgName = targetName.substring(0, targetName.lastIndexOf("."));
// 获取config配置文件中的图片尺寸字符串
String imageSize = getImageSizeConfig("goodsImgSize");
if (imageSize != null && !imageSize.equals("")) {
String[] imageSizeAryC = imageSize.split(",");
for (int i = 0; i < imageSizeAryC.length; i++) {
String imageSizeI=imageSizeAryC[i];
String[] imageSizeAry = imageSizeI.split(":");
/* ============= 图片处理 ================== */
// 建立输出流
out = new FileOutputStream(targetPath + separator + imgName + "_" + imageSizeAry[0] + ".jpg");
out.flush();
// 依照固定大小画图区域
// width = 426;
// height = 284;
resize(srcImgPath, Integer.parseInt(imageSizeAry[0]), Integer.parseInt(imageSizeAry[1]), false, out);
out.close();
}
ret = true;
}
} catch (Exception e) {
e.printStackTrace();
}
return ret;
}
5、/**
* 方法功能:处理图片等比例压缩
*
* @param filePath
* 图片路径
* @param height
* 高度
* @param width
* 宽度
* @param blean
* 比例不对时是否需要补白
* @author liu_huibin 20110114
*/
public void resize(String filePath, int width, int height, boolean blean, FileOutputStream out) {
try {
double ratio = 0.0; // 缩放比例
BufferedImage tag1 = null;// 生成指定大小的图片域
File f = new File(filePath);
BufferedImage bi = ImageIO.read(f);
if (bi==null||"".equals(bi)) {
throw new OpenException(ErrorCode.IMAGE_FORMAT_BAD,"商品主图格式错误");
}
Image itemp = bi.getScaledInstance(width, height, bi.SCALE_SMOOTH);
// 计算比例
if ((bi.getHeight() > height) && (bi.getWidth() > width)) {
double a =(double)bi.getHeight() / height;
double b=(double)bi.getWidth() / width;
if (a>=b) {
ratio = (new Integer(height)).doubleValue() / bi.getHeight();
}else {
ratio = (new Integer(width)).doubleValue() / bi.getWidth();
}
AffineTransformOp op = new AffineTransformOp(AffineTransform.getScaleInstance(ratio, ratio), null);
itemp = op.filter(bi, null);
}else if ((bi.getHeight() >= height) || (bi.getWidth() >= width)) {
if (bi.getHeight() > bi.getWidth()) {
ratio = (new Integer(height)).doubleValue() / bi.getHeight();
}else{
ratio = (new Integer(width)).doubleValue() / bi.getWidth();
}
AffineTransformOp op = new AffineTransformOp(AffineTransform.getScaleInstance(ratio, ratio), null);
itemp = op.filter(bi, null);
}
// 转换图片时转化,使图片不再失真
ResampleOp resampleOp = new ResampleOp(itemp.getWidth(null), itemp.getHeight(null));
BufferedImage rescaledTomato = resampleOp.filter(bi, null);
// 依照固定大小画图区域
tag1 = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);//画幕注意,为要得到的大小如:426*284
Graphics2D graphics2D = tag1.createGraphics(); //压缩图片不是800*800是456*245,这样压缩需要空白
graphics2D.setBackground(Color.WHITE);//补白或其他颜色
graphics2D.clearRect(0, 0, width, height); //画幕大小清理
graphics2D.drawImage(rescaledTomato, 0, 0, rescaledTomato.getWidth(), rescaledTomato.getHeight(), null);
// 画图操作
// JPEGCodec.createJPEGEncoder(out).encode(tag1);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(tag1);
param.setQuality(0.95f, true);
encoder.encode(tag1, param);
} catch (IOException e) {
e.printStackTrace();
}
}