【169】JAVA8利用隔行隔列删除元素来有损压缩图片

本文的有损压缩算法,是每隔一行或者一列,就删去一行或一列的像素。如下图所示,下图中红线表示要删除的行和列的元素。

在这里插入图片描述
下面直接放代码:

常量类 Constant.java

package zhangchao.common;

/**
 * 
 * 常量类
 * @author 张超
 * 
 */
public class Constant {

    public static final String INPUT_DIR_PATH =  "D:\\yourzcinputdir\\";
    public static final String OUTPUT_DIR_PATH = "D:\\yourzcoutputdir\\";
}

Main.java

package zhangchao;

import zhangchao.common.Constant;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;


/**
 * 含有 main 方法
 * @author zhangchao
 *
 */
public class Main {

    /**
     * 有损压缩图片
     * @param file 原文件
     * @param outputFile 输出的压缩后的文件
     * @return 压缩后的文件
     */
    public static File one(final File file, final File outputFile, final String formatName) {
        try {
            // 原图片
            BufferedImage image = ImageIO.read(file);
            int width = image.getWidth();
            int height = image.getHeight();

            // 压缩后图片的宽度
            int widthNew;
            if (width % 2 == 0) {
                widthNew = width / 2;
            } else {
                widthNew = width / 2 + 1;
            }
            // 压缩后图片的高度
            int heightNew;
            if (height % 2 == 0) {
                heightNew = height / 2;
            } else {
                heightNew = height / 2 + 1;
            }

            // 压缩后的图片
            BufferedImage imageNew = new BufferedImage(widthNew, heightNew, image.getType());

            // 从0开始计算行数和列数。把同时位于偶数行和偶数列的元素复制到新的压缩后的图片中。
            for (int x = 0; x < width; x+=2) {
                for (int y = 0; y < height; y+=2) {
                    int rgb = image.getRGB(x, y);
                    int x1 = x/2;
                    int y1 = y/2;
                    imageNew.setRGB(x1, y1, rgb);
                }
            }

            ImageIO.write(imageNew, formatName, outputFile);
            return outputFile;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void main(String[] args) {
        long t1 = System.currentTimeMillis();
        File dir = new File(Constant.INPUT_DIR_PATH);
        File[] files = dir.listFiles();
        for (File file : files) {
            // 如果图片小于200K就不压缩
            if (file.length() < 200 * 1024) {
                try {
                    Files.copy(Paths.get(file.getAbsolutePath()), Paths.get(Constant.OUTPUT_DIR_PATH + file.getName()));
                } catch (IOException e) {
                    e.printStackTrace();
                }
                continue;
            }
            File result = file;
            while(null != result && result.length() >= 200 * 1024) {
                result = one(result, new File( Constant.OUTPUT_DIR_PATH+ file.getName()), "jpg");
            }
        }
        long t2 = System.currentTimeMillis();
        System.out.println("time=" + (t2 - t1));
    }
}

// 14654
// 10909
// 张

Main类的作用:循环遍历输入目录中的每个文件,如果小于200K就不压缩,直接复制到输出目录;如果大于等于200K,就进行反复压缩,直到低于200K为止。上面代码中one方法每次压缩,图片体积大约会变成原来的25%

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值