Java如何实现证件照换底色| 背景换色

简介

本文主要讲解如何用java代码实现任何图片的底片换色,本文以为证件照片为例,用java代码生成多个底色不一样的图片。代码仅仅用到java包下的类,不需要借助外部包,用到的包有javax.imageiojava.awtjava.io 等。

教程

目标图片

实现思路

图片是由一个个像素块组成的,每个像素块对应一个RGB颜色值。将照片加载到内存,转换成一个二维的RGB矩阵,想办法识别到边缘的背景色,遍历二维矩阵,将跟背景色相同的颜色值替换为目标颜色值(如:蓝色背景 更换为 白色背景)。

代码实现


import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;

/**
 * @author  tarzan
 */
public class ImageBgChange {



    public static void main(String[] args) {
            //白色
            changeBgColor("E:\\screenshot\\certificate.png",Color.WHITE,45);
            //黑色
            changeBgColor("E:\\screenshot\\certificate.png",Color.BLACK,45);
            //绿色
            changeBgColor("E:\\screenshot\\certificate.png",Color.GREEN,45);
            //蓝色
            changeBgColor("E:\\screenshot\\certificate.png",Color.BLUE,45);
            //黄色
            changeBgColor("E:\\screenshot\\certificate.png",Color.YELLOW,45);
            //红色
            changeBgColor("E:\\screenshot\\certificate.png",Color.RED,45);
            //黑色透明
            changeBgColor("E:\\screenshot\\certificate.png",new Color(0,0,0,0),45);
            //白色透明
            changeBgColor("E:\\screenshot\\certificate.png",new Color(255,255,255,0),45);
    }


    /**
     * 方法描述: 改变背景色
     *
     * @param path 图片路径
     * @param color 要变成的颜色
     * @param range 颜色误差范围(取50左右最佳)
     * @date 2023年01月13日 17:48:52
     */
    private static void changeBgColor(String path,Color color,int range) {
        try {
            BufferedImage image = ImageIO.read(new File(path));
            int RGB=image.getRGB(image.getMinX(), image.getMinY());
            // 遍历Y轴的像素
            for (int y = image.getMinY(); y < image.getHeight(); y++) {
                // 遍历X轴的像素
                for (int x = image.getMinX(); x < image.getWidth(); x++) {
                    int rgb = image.getRGB(x, y);
                    int r = (rgb & 0xff0000) >>16;
                    int g = (rgb & 0xff00) >> 8;
                    int b = (rgb & 0xff);
                    int R = (RGB & 0xff0000) >>16;
                    int G = (RGB & 0xff00) >> 8;
                    int B = (RGB & 0xff);
                //颜色误差范围
               if(Math.abs(R-r) < range && Math.abs(G-g) < range && Math.abs(B-b) < range ) {
                        image.setRGB(x,y,color.getRGB());
                    }
                }
            }
            String suffix=path.substring(path.lastIndexOf("."));
            String filePath =path.replace(suffix,"_"+color.getRGB()+suffix);
            ImageIO.write(image, "png", new File(filePath));
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    /**
     * 方法描述: argb 转rgb
     *
     * @param color
     * @return {@link String}
     */
    public static String toRGB(int color) {
        int red = (color & 0xff0000) >> 16;// 获取color(RGB)中R位
        int green = (color & 0x00ff00) >> 8;// 获取color(RGB)中G位
        int blue = (color & 0x0000ff);// 获取color(RGB)中B位
        return red + "," + green + "," + blue;
    }

    /**
     * 方法描述: rgb 转argb
     *
     * @param color
     * @return {@link String}
     */
    private static int toARGB(String color){
        String[] rgb=color.split(",");
        int r= Integer.parseInt(rgb[0]);
        int g= Integer.parseInt(rgb[0]);
        int b= Integer.parseInt(rgb[0]);
        int argb = 255 << 24;//设置A(透明度)的值 左移24位是放到最前面
        argb += (r << 16);
        argb += (g << 8);
        argb += b;
        return argb;
    }

}

这个是java代码实现,在idea等编辑器里,运行主方法就行,记得在代码里修改你的照片文件路径。

效果展示

由于透明色,不是在专用的查看上看的话,是没办法显示的,图片中分别有一个是白色透明和黑色透明。

注:未经本人同意,禁止以为个人名义或公司转载到其他平台。

猜你喜欢

转载自blog.csdn.net/weixin_40986713/article/details/128677906
今日推荐