java将图片转成透明背景

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

/**
 * @author zhaojinhui
 * @date 2021/6/10 11:14
 * @apiNote 将图片变成透明背景的
 */
public class ImageUtil {
    /**
     * 传入图片路径将图片变更为透明背景然后返回 inputStream
     * @param url
     * @return
     */
    public static InputStream toTransparentBackground(String url){
        try {
            BufferedImage bufferedImage = ImageIO.read(new File(url));
            int alpha = 255;
            String removeRgb = null;
            // 遍历Y轴的像素
            for (int y = bufferedImage.getMinY(); y < bufferedImage.getHeight(); y++) {
                // 遍历X轴的像素
                for (int x = bufferedImage.getMinX(); x < bufferedImage.getWidth(); x++) {
                    int rgb = bufferedImage.getRGB(x, y);
                    // 取图片边缘颜色作为对比对象
                    if (y == bufferedImage.getMinY() && x == bufferedImage.getMinX()) {
                        removeRgb = convertRgbStr(rgb);
                    }
                    // 设置为透明背景
                    if (removeRgb.equals(convertRgbStr(rgb))) {
                        alpha = 0;
                    } else {
                        alpha = 255;
                    }
                    rgb = (alpha << 24) | (rgb & 0x00ffffff);
                    bufferedImage.setRGB(x, y, rgb);
                }
            }
            return bufferedImageToInputStream(bufferedImage);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取像素点的 rgb值
     * @param color
     * @return
     */
    public static String convertRgbStr(int color) {
        // 获取color(RGB)中R位
        int red = (color & 0xff0000) >> 16;
        // 获取color(RGB)中G位
        int green = (color & 0x00ff00) >> 8;
        // 获取color(RGB)中B位
        int blue = (color & 0x0000ff);
        return red + "," + green + "," + blue;
    }

    /**
     * 将 bufferedImage 转换成 inputStream
     * @param image
     * @return
     */
    public static InputStream bufferedImageToInputStream(BufferedImage image){
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        try {
            ImageIO.write(image, "png", os);
            InputStream input = new ByteArrayInputStream(os.toByteArray());
            return input;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void main(String[] args) throws IOException {
        //String str = "http://58.57.18.50:4434/fastdfsfile/00/04/wKgE52C4PxuAXvOvAABABLIVDZg24.jpeg";
        String str = "D:/test.png";
        InputStream inputStream = toTransparentBackground(str);
        FileOutputStream fileOutputStream = new FileOutputStream("D:/touming.png");
        int read = 0;
        byte[] data = new byte[1 << 10];
        while((read = inputStream.read(data)) != -1 ){
            fileOutputStream.write(data);
        }
        fileOutputStream.close();

    }
}

参考链接 https://blog.csdn.net/lkh1992/article/details/109010328

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值