java 等比例 改变图片的质量(等大小压缩图片)

 /**
     * @param originalFile  原文件
     * @param resizedFile  压缩目标文件
     * @param quality  压缩质量(越高质量越好)
     * @param scale  缩放比例;  1等大.
     * @throws IOException
     */
    public static void resize(File originalFile, File resizedFile,double scale, float quality) throws IOException {  
        ImageIcon ii = new ImageIcon(originalFile.getCanonicalPath());  
        Image i = ii.getImage();  
        int iWidth = (int) (i.getWidth(null)*scale);  
        int iHeight = (int) (i.getHeight(null)*scale); 
        //在这你可以自定义 返回图片的大小 iWidth iHeight
        Image resizedImage = i.getScaledInstance(iWidth,iHeight, Image.SCALE_SMOOTH);  
        // 获取图片中的所有像素
        Image temp = new ImageIcon(resizedImage).getImage();  
        // 创建缓冲
        BufferedImage bufferedImage = new BufferedImage(temp.getWidth(null),  
                temp.getHeight(null), BufferedImage.TYPE_INT_RGB);  
        // 复制图片到缓冲流中
        Graphics g = bufferedImage.createGraphics();  
        // 清除背景并开始画图
        g.setColor(Color.white);  
        g.fillRect(0, 0, temp.getWidth(null), temp.getHeight(null));  
        g.drawImage(temp, 0, 0, null);  
        g.dispose();
        // 柔和图片.  
        float softenFactor =0.05f;  
        float[] softenArray = { 0, softenFactor, 0, softenFactor,  
                1 - (softenFactor * 4), softenFactor, 0, softenFactor, 0 };  
        Kernel kernel = new Kernel(3, 3, softenArray);  
        ConvolveOp cOp = new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP, null);  
        bufferedImage = cOp.filter(bufferedImage, null);  
        FileOutputStream out = new FileOutputStream(resizedFile);  
        JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);  
        JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bufferedImage);  
        param.setQuality(quality, true);  
        encoder.setJPEGEncodeParam(param);  
        encoder.encode(bufferedImage);
        bufferedImage.flush();
        out.close();
    } 

你可能感兴趣的:(java)