将 JPG 转换为具有背景透明度的 PNG

Convert JPG to PNG with background transparency

JPG 转换为 PNG 背景透明图像时,我不希望转换后的图像出现虚线边框

我的原图 (JPG)

我的转换图像 (PNG)

我的源代码是

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.awt.image.FilteredImageSource;
import java.awt.image.ImageFilter;
import java.awt.image.ImageProducer;
import java.awt.image.RGBImageFilter;
import java.io.File;

import javax.imageio.ImageIO;

public class DemoTransparent {

    public static void main(String[] args) throws Exception {

        URL url = new URL("http://i.stack.imgur.com/jEqbx.jpg");
        Image  image1 = ImageIO.read(url);
        BufferedImage source = (BufferedImage) image1;
        int color = source.getRGB(0, 0);
        Image image = makeColorTransparent(source, new Color(color));
        BufferedImage transparent = imageToBufferedImage(image);
        File out = new File("D:\Demo.png");
        ImageIO.write(transparent, "PNG", out);

    }

    private static BufferedImage imageToBufferedImage(Image image) {

        BufferedImage bufferedImage = new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_4BYTE_ABGR_PRE);

        Graphics2D g2 = bufferedImage.createGraphics();
        g2.drawImage(image, 0, 0, null);
        g2.dispose();
        return bufferedImage;

    }

    public static Image makeColorTransparent(BufferedImage im, final Color color) {
        ImageFilter filter = new RGBImageFilter() {

            public int markerRGB = color.getRGB() | 0xFF000000;
            public final int filterRGB(int x, int y, int rgb) {
                if ((rgb | 0xFF000000) == markerRGB) {
                    return 0x00FFFFFF & rgb;
                } else {
                    return rgb;
                }
            }


        };
        ImageProducer ip = new FilteredImageSource(im.getSource(), filter);
        return Toolkit.getDefaultToolkit().createImage(ip);
    }

}

我只想要png背景透明图片

通常我们提取与颜色完全相等或接近等于颜色的颜色是采用阈值。

这是您稍作修改的代码:

// Take another parameter i.e. threshold
public static Image makeColorTransparent(BufferedImage im, final Color color, float threshold) {
    ImageFilter filter = new RGBImageFilter() {
        public float markerAlpha = color.getRGB() | 0xFF000000;
        public final int filterRGB(int x, int y, int rgb) {
            int currentAlpha = rgb | 0xFF000000;           // just to make it clear, stored the value in new variable
            float diff = Math.abs((currentAlpha - markerAlpha) / markerAlpha);  // Now get the difference
            if (diff <= threshold) {                      // Then compare that threshold value
                return 0x00FFFFFF & rgb;
            } else {
                return rgb;
            }
        }
    };
    ImageProducer ip = new FilteredImageSource(im.getSource(), filter);
    return Toolkit.getDefaultToolkit().createImage(ip);
}

然后调用函数 makeColorTransparent(image, color, 0.05f);
但是你的形象有问题。椭圆角上的像素颜色与椭圆内部(右下角)像素的颜色完全相同。所以算法也在移除那些像素。

很遗憾,您不能包含这些像素。我试着用不同的阈值来做。但是要么包含角,要么删除椭圆形的某些像素。您可以为此做的是使用 0.05f 作为阈值,然后手动绘制您想要的区域(使用 photoshop 或任何简单的图像编辑器)。