Chinaunix首页 | 论坛 | 博客
  • 博客访问: 546436
  • 博文数量: 298
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 3077
  • 用 户 组: 普通用户
  • 注册时间: 2019-06-17 10:57
文章分类

全部博文(298)

文章存档

2022年(96)

2021年(201)

2019年(1)

我的朋友

分类: Java

2021-05-21 14:43:24

一张原始图像(1920x1080),如果每个像素32bit表示(RGBA),那么,图像需要的内存大小1920x1080x4 = 8294400 Byte
那图像为何可以压缩呢?因为它有很多冗余信息。

比如说,第一行像素基本都一样,假设亮度值Y是这么存的[105比如说,第一行像素基本都一样,假设亮度值Y是这么存的[105 105 105…….105],如果共100个像素,那需要1Byte*100。
最简单的压缩:[105, 100],表示接下来100个像素的亮度都是105,那么只要2个字节,就能表示整行数据了

1、方式一

(1)、写工具类

点击(此处)折叠或打开

  1. import java.awt.Color;
  2. import java.awt.Graphics;
  3. import java.awt.Image;
  4. import java.awt.image.BufferedImage;
  5. import java.awt.image.ConvolveOp;
  6. import java.awt.image.Kernel;
  7. import java.io.File;
  8. import java.io.FileOutputStream;
  9. import java.io.IOException;
  10.  
  11. import javax.swing.ImageIcon;
  12.  
  13. import com.sun.image.codec.jpeg.JPEGCodec;
  14. import com.sun.image.codec.jpeg.JPEGEncodeParam;
  15. import com.sun.image.codec.jpeg.JPEGImageEncoder;
  16.  
  17. //java项目www fhadmin org
  18. public class imagesFiler {
  19.     /**
  20.      * 缩放图片(压缩图片质量,改变图片尺寸)
  21.      * 若原图宽度小于新宽度,则宽度不变!
  22.      * @param originalFile 原图片路径地址
  23.      * @param resizedFile 压缩后输出路径地址
  24.      * @param maxWidth 最大宽度
  25.      * @param maxHeight 最大高度
  26.      * @param newWidth 新的宽度
  27.      * @param quality 图片质量参数 0.7f 相当于70%质量
  28.      */
  29.     public static void imageResize(File originalFile, File resizedFile,
  30.                               int maxWidth,int maxHeight, float quality) throws IOException {
  31.  
  32.         if (quality > 1) {
  33.             throw new IllegalArgumentException(
  34.                     "图片质量需设置在0.1-1范围");
  35.         }
  36.  
  37.         ImageIcon ii = new ImageIcon(originalFile.getCanonicalPath());
  38.         Image i = ii.getImage();
  39.         Image resizedImage = null;
  40.  
  41.         int iWidth = i.getWidth(null);
  42.         int iHeight = i.getHeight(null);
  43.  
  44.         int newWidth = maxWidth;
  45.         if(iWidth < maxWidth){
  46.             newWidth = iWidth;
  47.         }
  48.  
  49.  
  50.         if (iWidth >= iHeight) {
  51.             resizedImage = i.getScaledInstance(newWidth, (newWidth * iHeight)
  52.                     / iWidth, Image.SCALE_SMOOTH);
  53.         }
  54.   
  55.  
  56.         int newHeight = maxHeight;
  57.         if(iHeight < maxHeight){
  58.             newHeight = iHeight;
  59.         }
  60.  
  61.         if(resizedImage==null && iHeight >= iWidth){
  62.             resizedImage = i.getScaledInstance((newHeight * iWidth) / iHeight,
  63.                     newHeight, Image.SCALE_SMOOTH);
  64.         }
  65.  
  66.         //此代码确保加载图像中的所有像素
  67.         Image temp = new ImageIcon(resizedImage).getImage();
  68.  
  69.         //创建缓冲图像
  70.         BufferedImage bufferedImage = new BufferedImage(temp.getWidth(null),
  71.                 temp.getHeight(null), BufferedImage.TYPE_INT_RGB);
  72.  
  73.         //将图像复制到缓冲图像
  74.         Graphics g = bufferedImage.createGraphics();
  75.  
  76.        //清除背景并绘制图像。
  77.         g.setColor(Color.white);
  78.         g.fillRect(0, 0, temp.getWidth(null), temp.getHeight(null));
  79.         g.drawImage(temp, 0, 0, null);
  80.         g.dispose();
  81.  
  82.         float softenFactor = 0.05f;
  83.         float[] softenArray = { 0, softenFactor, 0, softenFactor,
  84.                 1 - (softenFactor * 4), softenFactor, 0, softenFactor, 0 };
  85.         Kernel kernel = new Kernel(3, 3, softenArray);
  86.         ConvolveOp cOp = new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP, null);
  87.         bufferedImage = cOp.filter(bufferedImage, null);
  88.  
  89.         //将jpeg写入文件
  90.         FileOutputStream out = new FileOutputStream(resizedFile);
  91.  
  92.          //将图像编码为jpeg数据流
  93.         JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
  94.  
  95.         JPEGEncodeParam param = encoder
  96.                 .getDefaultJPEGEncodeParam(bufferedImage);
  97.  
  98.         param.setQuality(quality, true);
  99.  
  100.         encoder.setJPEGEncodeParam(param);
  101.         encoder.encode(bufferedImage);
  102.     }
  103.    
  104. }

(2)、测试

点击(此处)折叠或打开

  1. //java项目www fhadmin org
  2. public class demo {
  3.     public static void main(String[] args) throws Exception{
  4.     //需要压缩的图片地址 aaa.jpg为需要压缩的图片
  5.     File customaryFile = new File("");
  6.     //压缩过后输出的路径地址 ddd.jpg 可进行设置为任意名称
  7.     File compressAfter = new File("");
  8.         imagesFiler.imageResize(customaryFile,compressAfter,1200,2500,0.8f);
  9.     }
  10. }



阅读(1022) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~