使用背景透明度将JPG转换为PNG

时间:2015-11-07 04:33:59

标签: java image bufferedimage

JPG转换为PNG 背景透明度图片时,我不想在转换后的图片上使用虚线边框

我的原始图像(JPG) My Original Image

我的转换图像(PNG)

enter image description here

我的源代码是

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背景透明度图像

1 个答案:

答案 0 :(得分:7)

通常,我们提取的颜色与颜色完全相同或几乎相等,需要一个阈值。

这是您的代码,几乎没有修改:

// 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或任何简单的图像编辑器)。

enter image description here