Java图片操作

[size=small][color=blue]1、图片压缩[/color][/size]


//图片压缩
public static void imageCompress(String path,String newPath,String fileName,String toFileName,float scale,int width,int height){
imageCompress(path,newPath, fileName, toFileName, scale, 0.75f, width, height);
}
/**
* 描述:
* @param path 需要压缩的图片路径
* @param fileName 要压缩的图片名称
* @param toFileName 压缩后的图片名称
* @param scale 压缩比例 不能大于1,默认0.5
* @param quality 压缩品质介于0.1~1.0之间
* @param width 压缩后的图片的宽度
* @param height 压缩后的图片的高度
* 返回值:void
*/
private static void imageCompress(String path,String newPath, String fileName,String toFileName,float scale,float quality,int width,int height){
FileOutputStream out = null;
try {
Image image = javax.imageio.ImageIO.read(new File(path +"/"+ fileName));
int imageWidth = image.getWidth(null);
int imageHeight = image.getHeight(null);
if(scale >=1.0) scale = 0.99f;//默认压缩比为0.5,压缩比越大,对内存要去越高,可能导致内存溢出
//按比例计算出来的压缩比
float realscale = getRatio(imageWidth,imageHeight,width,height);
float finalScale = Math.min(scale, realscale);//取压缩比最小的进行压缩
imageWidth = (int)(finalScale*imageWidth);
imageHeight = (int)(finalScale*imageHeight);

image = image.getScaledInstance(imageWidth, imageHeight, Image.SCALE_AREA_AVERAGING);
BufferedImage mBufferedImage = new BufferedImage(imageWidth, imageHeight,BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = mBufferedImage.createGraphics();

g2.drawImage(image, 0, 0,imageWidth, imageHeight, Color.white,null);
g2.dispose();

float[] kernelData2 = {-0.125f, -0.125f, -0.125f,-0.125f,2, -0.125f,-0.125f,-0.125f, -0.125f };
Kernel kernel = new Kernel(3, 3, kernelData2);
ConvolveOp cOp = new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP, null);
mBufferedImage = cOp.filter(mBufferedImage, null);

out = new FileOutputStream(newPath+"/" + toFileName);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(mBufferedImage);
param.setQuality(quality, true);//默认0.75
encoder.setJPEGEncodeParam(param);
encoder.encode(mBufferedImage);
}catch (FileNotFoundException fnf){
fnf.printStackTrace();
}catch (IOException ioe){
ioe.printStackTrace();
}catch(Exception ex){
ex.printStackTrace();
}finally{
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

private static float getRatio(int width,int height,int maxWidth,int maxHeight){
float Ratio = 1.0f;
float widthRatio ;
float heightRatio ;
widthRatio = (float)maxWidth/width;
heightRatio = (float)maxHeight/height;
if(widthRatio<1.0 || heightRatio<1.0){
Ratio = widthRatio<=heightRatio?widthRatio:heightRatio;
}
return Ratio;
}

/**
* 动态获取压缩比列根据宽度
* @param map
* @return
*/
public static float getCompressSize(float width){
float result = 0.5f;
result = 595/width;
result = Float.parseFloat(StringUtil.formatNumber("#.##", result));
return result;
}

/**
* 计算图片尺寸大小等信息:w宽、h高、s大小。异常时返回null。
*
* @param imgpath 图片路径
* @return 图片信息map
*/
public static Map getImgInfo(InputStream fis) throws Exception {
Map map = new HashMap();
BufferedImage buff = ImageIO.read(fis);
map.put("w", new Long(buff.getWidth()* 1L)); //宽度
//map.put("h", new Long(buff.getHeight()* 1L)); //高度
//map.put("s", new Long(imgfile.length())); //获取图片大小
fis.close();
return map;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值