吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 6355|回复: 20
收起左侧

[Java 转载] 纯java代码实现去水印功能

  [复制链接]
llsydn 发表于 2019-12-30 11:57
public class WatermarkUtils {
    private static List<File> fileList = new ArrayList<>();

    public static void main(String[] args) {
        //convertAllImages("F:\\before.png", "F:\\after.png");
        fileList.add(new File("F:\\before.png"));
        convertAllImages(fileList);
    }

    /**
     * 去除文件列表里图片的水印并替换
     *
     * @Param fileList 文件列表
     */
    public static void convertAllImages(List<File> fileList) {
        try {
            for (File file : fileList) {
                if (!file.getName().endsWith("png") && !file.getName().endsWith("jpg")) {
                    continue;
                }
                BufferedImage bi = ImageIO.read(file); //ImageIO流读取像素块
                if (bi != null) {
                    removeWatermark(bi);
                    String formatName = file.getName().substring(file.getName().lastIndexOf(".") + 1);//生成的图片格式
                    ImageIO.write(bi, formatName, file);//ImageIO流生成的处理图替换原图片
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 从源目录获取图片处理后导出到目标目录
     *
     * @param dir     源目录
     * @param saveDir 目标目录
     */
    private static void convertAllImages(String dir, String saveDir) {
        File dirFile = new File(dir);
        File saveDirFile = new File(saveDir);
        dir = dirFile.getAbsolutePath();
        saveDir = saveDirFile.getAbsolutePath();
        loadImages(new File(dir));
        for (File file : fileList) {
            String filePath = file.getAbsolutePath();
            String dstPath = saveDir + filePath.substring(filePath.indexOf(dir) + dir.length());
            replace(file.getAbsolutePath(), dstPath);
        }
    }

    /**
     * 加载图片
     */
    private static void loadImages(File f) {
        if (f != null) {
            if (f.isDirectory()) {
                File[] fileArray = f.listFiles();
                if (fileArray != null) {
                    for (File file : fileArray) {
                        loadImages(file); //递归调用
                    }
                }
            } else {
                String name = f.getName();
                if (name.endsWith("png") || name.endsWith("jpg")) {
                    fileList.add(f);
                }
            }
        }
    }

    /**
     * 生成源图片的处理图
     *
     * @param srcFile 源图片路径
     * @param dstFile 目标图片路径
     */
    private static void replace(String srcFile, String dstFile) {
        try {
            URL http;
            if (srcFile.trim().startsWith("https")) {
                http = new URL(srcFile);
                HttpsURLConnection conn = (HttpsURLConnection) http.openConnection();
                conn.setRequestMethod("GET");
            } else if (srcFile.trim().startsWith("http")) {
                http = new URL(srcFile);
                HttpURLConnection conn = (HttpURLConnection) http.openConnection();
                conn.setRequestMethod("GET");
            } else {
                http = new File(srcFile).toURI().toURL();
            }
            BufferedImage bi = ImageIO.read(http.openStream());
            if (bi != null) {
                removeWatermark(bi);
                exportImage(bi, srcFile, dstFile);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * ImageIO流生成源图片的处理图
     *
     * @param bi       ImageIO
     * @param fileName 源图片带后缀的文件名
     * @param dstFile  目标图片路径
     */
    private static void exportImage(BufferedImage bi, String fileName, String dstFile) {
        try {
            String type = fileName.substring(fileName.lastIndexOf(".") + 1);
            Iterator<ImageWriter> it = ImageIO.getImageWritersByFormatName(type);
            ImageWriter writer = it.next();
            File f = new File(dstFile);
            ImageOutputStream ios = ImageIO.createImageOutputStream(f);
            writer.setOutput(ios);
            writer.write(bi);
            bi.flush();
            ios.flush();
            ios.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 去除水印
     */
    private static void removeWatermark(BufferedImage bi) {
        Color wColor = new Color(254, 254, 254);
        Color hColor = new Color(197, 196, 191);
        //白底水印
        for (int i = 0; i < bi.getWidth(); i++) {
            for (int j = 0; j < bi.getHeight(); j++) {
                int color = bi.getRGB(i, j);
                Color oriColor = new Color(color);
                int red = oriColor.getRed();
                int greed = oriColor.getGreen();
                int blue = oriColor.getBlue();
                if (red == 254 && greed == 254 && blue == 254) {
                    continue;
                }
                if (red > 220 && greed > 180 && blue > 80) {
                    bi.setRGB(i, j, wColor.getRGB());
                }
                if (red <= 240 && greed >= 200 && blue >= 150) {
                    bi.setRGB(i, j, wColor.getRGB());
                }
            }
        }
    }
}

转换后的图片

转换后的图片

转换前图片

转换前图片

免费评分

参与人数 4吾爱币 +7 热心值 +4 收起 理由
I_Break + 1 + 1 用心讨论,共获提升!
苏紫方璇 + 5 + 1 感谢发布原创作品,吾爱破解论坛因你更精彩!
shdm520 + 1 + 1 谢谢@Thanks!
阿豪0719 + 1 只能处理特定颜色的图片啊..京东上随便找了两个商品就不行...我在下边上图.

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

头像被屏蔽
你的小宝贝 发表于 2019-12-31 16:13
提示: 作者被禁止或删除 内容自动屏蔽
chen180 发表于 2019-12-31 11:51
阿豪0719 发表于 2019-12-30 14:59
两个处理过后的图片...第二张几乎看不到效果..第一张...额寄几看吧

看样子是不怎么好用。。。对于纯色背景的效果可能还好点..
Aleshaaaa 发表于 2019-12-30 13:04
Dreamfly疯子 发表于 2019-12-30 13:07
能做成去水印的软件么
小丑地带 发表于 2019-12-30 13:12
这个算法不行哦
bachelor66 发表于 2019-12-30 13:13
单色的?                           
byyc 发表于 2019-12-30 13:19
研究一下。。。
919359733 发表于 2019-12-30 13:25
我只会加水印
云驿站 发表于 2019-12-30 13:32
大佬666效果不错啊
阿豪0719 发表于 2019-12-30 14:46
考下来试试看 先谢了
阿豪0719 发表于 2019-12-30 14:59
两个处理过后的图片...第二张几乎看不到效果..第一张...额寄几看吧
shuiyin2.jpg
shuiyin1.jpg
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-9-9 11:56

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表