zoukankan      html  css  js  c++  java
  • java图片高质量缩放类

    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Image;
    import java.awt.image.BufferedImage;
    import java.awt.image.ConvolveOp;
    import java.awt.image.Kernel;
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;

    import javax.swing.ImageIcon;

    import com.sun.image.codec.jpeg.JPEGCodec;
    import com.sun.image.codec.jpeg.JPEGEncodeParam;
    import com.sun.image.codec.jpeg.JPEGImageEncoder;

    @SuppressWarnings("restriction")
    public class PictrueScaleUtils {
    /**
    * jpg图片的缩放
    * @param originalFile 原图片路径
    * @param resizedFile 缩放后图片的路径
    * @param newWidth 缩放后图片的宽度
    * @param newHeight 缩放后图片的高度【当isScale为false时newHeight不起作用】
    * @param isScale 是否按比例缩放
    * @param quality 缩放质量
    * @throws IOException
    */
    public static void jpgScalePicture(File originalFile, File resizedFile,
    int newWidth, int newHeight, Boolean isScale,
    float quality) throws IOException {
    if (quality > 1) {
    throw new IllegalArgumentException("图片质量是0和1之间");
    }

    ImageIcon ii = new ImageIcon(originalFile.getCanonicalPath());
    Image i = ii.getImage();
    Image resizedImage = null;

    int iWidth = i.getWidth(null);
    int iHeight = i.getHeight(null);

    if(isScale){
    if (iWidth > iHeight) {
    resizedImage = i.getScaledInstance(newWidth, (newWidth * iHeight)/ iWidth, Image.SCALE_SMOOTH);
    } else {
    resizedImage = i.getScaledInstance((newWidth * iWidth) / iHeight, newWidth, Image.SCALE_SMOOTH);
    }
    } else {
    resizedImage = i.getScaledInstance(newWidth, newHeight, Image.SCALE_SMOOTH);
    }

    // 1这个代码确保图像中的所有像素的加载。
    Image temp = new ImageIcon(resizedImage).getImage();

    // 2创建缓冲的图像。
    BufferedImage bufferedImage = new BufferedImage(temp.getWidth(null), temp.getHeight(null), BufferedImage.TYPE_INT_RGB);

    // 3将图像复制到缓冲的图像。
    Graphics g = bufferedImage.createGraphics();

    // 4背景清晰的图像和画。
    g.setColor(Color.white);
    g.fillRect(0, 0, temp.getWidth(null), temp.getHeight(null));
    g.drawImage(temp, 0, 0, null);
    g.dispose();

    // 4软化。
    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);

    // 6写的JPEG文件。
    FileOutputStream out = new FileOutputStream(resizedFile);

    // 7作为一个JPEG编码图像数据流
    @SuppressWarnings("restriction")
    JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);

    @SuppressWarnings("restriction")
    JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bufferedImage);

    param.setQuality(quality, true);

    encoder.setJPEGEncodeParam(param);
    encoder.encode(bufferedImage);
    }

    /**************************************************************************************************************************/
    public static void main(String[] args) throws IOException {
    File originalImage = new File("E:/aaa/png - 副本.png");
    File resizedFile = new File("E:/aaa/png - 副本aaaaaaaaaaaaaa.png");
    jpgScalePicture(originalImage, resizedFile, 200, 200, true, 1f);

    System.err.println("转化成功''''''''''''''''''");
    }
    }

    Java中的类JPEGImageEncoder,JPEGCodec和JPEGEncodeParam的导入问题

    java的图片压缩,需要用这三个类,默认这三个类在eclipse是导不进来,直接报错的。

    import com.sun.image.codec.jpeg.JPEGCodec;
    import com.sun.image.codec.jpeg.JPEGEncodeParam;
    import com.sun.image.codec.jpeg.JPEGImageEncoder;

    解决办法:
    Eclipse默认把这些受访问限制的API设成了ERROR。只要把Windows-Preferences-Java-Complicer-Errors/Warnings里面的Deprecated and restricted API中的Forbidden references(access rules)选为Warning就可以编译通过。

  • 相关阅读:
    php Composer 报ssl证书错误
    centos svn服务器安装
    nginx配置 php 单入口
    centos nginx 安装
    Zend Studio的配置和使用
    不要让别人笑你不能成为程序员
    10个用于Web开发的最好 Python 框架
    分析和解析PHP代码的7大工具
    php模拟用户自动在qq空间发表文章的方法
    PHP中实现MySQL嵌套事务的两种解决方案
  • 原文地址:https://www.cnblogs.com/tancp/p/3664199.html
Copyright © 2011-2022 走看看