前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >java学习笔记(中级篇)—java实现高质量图片压缩

java学习笔记(中级篇)—java实现高质量图片压缩

作者头像
chlinlearn
发布2019-08-06 14:17:10
1.9K0
发布2019-08-06 14:17:10
举报
文章被收录于专栏:java老实人

使用java几十行代码实现一个高质量图片压缩程序,再也不用去自己找网络的压缩程序啦!而且很多网上的工具还有水印或者其他的限制,自己动手写一个简单的应用,是再合适不过了。

一、实现原理

1、声明两个字符串变量,分别是要压缩图片的路径和压缩后图片的存放路径

代码语言:javascript
复制
private String brfore_image_path = "D:\\01.jpg";
private String after_image_path = "D:\\temp";

2、利用字符串的方法lastIndexOf,找到\和.最后出现的位置,目的是匹配到图片文件名。

代码语言:javascript
复制
int begin = brfore_image_path.lastIndexOf("\\");
int end = brfore_image_path.lastIndexOf(".");
String image_name=brfore_image_path.substring(begin+1,end);

3、创建BufferedImage对象来读取需要压缩的图片

4、获取原始图片的一系列参数

代码语言:javascript
复制
int in_width  = bi.getWidth();//图宽
int in_height = bi.getHeight();//图高
int in_minx   = bi.getMinX();//BufferedImage的最小x
int in_miny   = bi.getMinY();//BufferedImage的最小y
int type = bi.getType();//返回图像类型
int out_width = in_width;//要输出图像的宽
int out_height = in_height;//要输出图像的高
int multiple = 1;//系数

5、压缩核心代码,可自己调试找最适合的临界值,我选取的是大于1000000像素点时就压缩一半

代码语言:javascript
复制
while(out_width * out_height > 1000000){
    out_width = out_width/2;
    out_height = out_height/2;
    multiple = multiple * 2;
}   

6、创建新的BufferedImage对象,把新的参数传进去,并根据系数把一个个像素点写进图片。

代码语言:javascript
复制
for(int i=0;i<out_width;i++) {
    for(int j =0;j<out_height;j++) {      intpixel=bi.getRGB(i*multiple+in_minx,j*multiple+in_miny);
 ut_image_martrix.setRGB(i, j, pixel);
    }
}

7、把新的BufferedImage对象写到你要保存压缩图片的地址就好了。

二、完整代码

代码语言:javascript
复制
public class CompressImage {
    private String brfore_image_path = "D:\\01.jpg";
    private String after_image_path = "D:\\temp";
    
    public CompressImage(){
    }

    public void get_image(){
        int begin = brfore_image_path.lastIndexOf("\\");
        int end = brfore_image_path.lastIndexOf(".");
        String image_name = brfore_image_path.substring(begin+1,end);

        File in_file = new File(brfore_image_path);
        BufferedImage bi = null;
        try {
            bi = ImageIO.read(in_file);
        }catch(Exception e) {
            e.printStackTrace();
        }
        int in_width  = bi.getWidth();
        int in_height = bi.getHeight();
        int in_minx   = bi.getMinX();
        int in_miny   = bi.getMinY();
        int type = bi.getType();
        int out_width = in_width;
        int out_height = in_height;
        int multiple = 1;
        
        //具体的值可调
        while(out_width * out_height > 1000000){
            out_width = out_width/2;
            out_height = out_height/2;
            multiple = multiple * 2;
        }   
        BufferedImage out_image_martrix = new            BufferedImage(out_width, out_height, type);
        for(int i=0;i<out_width;i++) {
            for(int j =0;j<out_height;j++) {
                int pixel =bi.getRGB(i*multiple+in_minx, j*multiple+in_miny);
                out_image_martrix.setRGB(i, j, pixel);
            }
        }
        try{
            after_image_path = after_image_path + image_name + ".jpg";
            ImageIO.write(out_image_martrix,"jpg", new File(new_path));
            bi = null;
            out_image_martrix = null;
        }catch(Exception e){
            e.printStackTrace();
        }
    }
    
    //测试代码
    public static void main(String[] args) {
        new CompressImage().get_image();
    }
}

三、总结

代码挺简单的,但是自己动手实现完成一个小功能也不一样哦,而且我觉得压缩的质量还挺高的,所以把自己的实现思路和代码分享出来。有兴趣的童鞋可以自己复制上面的完整代码,只要改成自己的路径就可以运行了。当然啦,几行代码无法媲美专业的压缩工具啦~ 最后,喜欢我文章的小伙伴就关注一下我的公众号吧~

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019-08-05 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一、实现原理
  • 二、完整代码
  • 三、总结
相关产品与服务
文件存储
文件存储(Cloud File Storage,CFS)为您提供安全可靠、可扩展的共享文件存储服务。文件存储可与腾讯云服务器、容器服务、批量计算等服务搭配使用,为多个计算节点提供容量和性能可弹性扩展的高性能共享存储。腾讯云文件存储的管理界面简单、易使用,可实现对现有应用的无缝集成;按实际用量付费,为您节约成本,简化 IT 运维工作。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档