签名图片去除背景

实现图片的背景虚化

    /**
     * 去除图片背景色,图片透明化
     * @param image 原始图片
     * @param isCompress 是否压缩
     * @param imageWidth 压缩后的图片宽度: isCompress为true时不能为null
     * @param imageHeight 他所后的图片高度: isCompress为true时不能为null
     */
    public static BufferedImage clearImageBackground(BufferedImage image, boolean isCompress, Integer imageWidth, Integer imageHeight) throws IOException {
        BufferedImage bufferedImage = new BufferedImage(IMG_WIDTH, IMG_HEIGHT, BufferedImage.TYPE_INT_ARGB);
        try {
            image = transImageBackgroundColor(image, Color.BLACK, Color.WHITE);
            if(isCompress){
                image = resizeImage(image, imageWidth, imageHeight);
            }
            bufferedImage = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_ARGB);
            int width = image.getWidth();
            int height = image.getHeight();

            for (int x = 0; x < width; x++) {
                for (int y = 0; y < height; y++) {
                    int rgba = image.getRGB(x, y);
                    if (isWhite(rgba)) {
                        bufferedImage.setRGB(x, y, 0x00ffff);
                    } else {
                        bufferedImage.setRGB(x, y, rgba);
                    }
                }
            }

        }catch (Exception e){
            e.printStackTrace();
        }
        return bufferedImage;
    }

    public static boolean isWhite(int rgba) {
        int r = (rgba >> 16) & 0xFF;
        int g = (rgba >> 8) & 0xFF;
        int b = rgba & 0xFF;
        return r > 250 && g > 250 && b > 250;
    }

修改图片背景

    /**
     * 压缩图片
     * @param image 原始图片
     * @param filterColor 过滤的颜色(如:文字黑色不替换,则filterColor为BLACK)
     * @param color 新的背景颜色
     */
    public static BufferedImage transImageBackgroundColor(BufferedImage image,  Color filterColor, Color color) {
        for (int x = image.getMinX(); x < image.getWidth(); x++) {
            for (int y = image.getMinY(); y < image.getHeight(); y++) {
                int rgb = image.getRGB(x, y);
                Color src = new Color(rgb);
                if(filterColor.equals(src)){
                    image.setRGB(x, y, src.getRGB());
                    continue;
                }
                image.setRGB(x, y, color.getRGB());
            }
        }
        return image;
    }

图片压缩

    /**
     * 压缩图片
     * @param image 原始图片
     * @param newWidth 压缩宽度
     * @param newHeight 压缩高度
     */
    public static BufferedImage resizeImage(BufferedImage image, int newWidth, int newHeight) {
        Image imageSrc = image.getScaledInstance(newWidth, newHeight, Image.SCALE_AREA_AVERAGING);
        BufferedImage resizeImage = new BufferedImage(newWidth, newHeight, image.getType());
        Graphics2D g2d = resizeImage.createGraphics();
        g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.drawImage(imageSrc, 0, 0,newWidth, newHeight, null);
        g2d.dispose();
        return resizeImage;
    }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值