压缩图片分辨率 ,根据 url 和 根据 file
/**
* <p>Title: thumbnailImage</p>
* <p>Description: 根据图片路径生成缩略图 </p>
* @param imagePath 原图片路径
* @param w 缩略图宽
* @param h 缩略图高
* @param prevfix 生成缩略图的前缀
* @param force 是否强制按照宽高生成缩略图(如果为false,则生成最佳比例缩略图)
*/
public static File thumbnailImage(File imgFile, String format,int w, int h, boolean force){
File file = null;
try {
// ImageIO 支持的图片类型 : [BMP, bmp, jpg, JPG, wbmp, jpeg, png, PNG, JPEG, WBMP, GIF, gif]
// 获取图片后缀
Image img = ImageIO.read(imgFile);
BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
Graphics g = bi.getGraphics();
g.drawImage(img, 0, 0, w, h, Color.LIGHT_GRAY, null);
g.dispose();
ByteArrayOutputStream os = new ByteArrayOutputStream();
ImageIO.write(bi,format,os);
byte [] bytes = os.toByteArray();
BufferedOutputStream bos = null;
FileOutputStream fos = null;
file = new File("temp");
fos = new FileOutputStream(file);
bos = new BufferedOutputStream(fos);
bos.write(bytes);
bos.close();
fos.close();
os.close();
} catch (IOException e) {
}
return file;
}
public static InputStream thumbnailImageByUrl(String url,String format,int w, int h, boolean force){
try {
// ImageIO 支持的图片类型 : [BMP, bmp, jpg, JPG, wbmp, jpeg, png, PNG, JPEG, WBMP, GIF, gif]
// 获取图片后缀
Image img = ImageIO.read(new URL(url));
BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
Graphics g = bi.getGraphics();
g.drawImage(img, 0, 0, w, h, Color.LIGHT_GRAY, null);
g.dispose();
ByteArrayOutputStream os = new ByteArrayOutputStream();
ImageIO.write(bi, format, os);
InputStream input = new ByteArrayInputStream(os.toByteArray());
os.close();
return input;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}