Converting transparent gif / png to jpeg using java

前端 未结 7 828
南笙
南笙 2020-11-29 02:27

I\'d like to convert gif images to jpeg using Java. It works great for most images, but I have a simple transparent gif image:

Input gif image http://img292.imagesha

7条回答
  •  情歌与酒
    2020-11-29 02:55

    As already mentioned in the UPDATE of the question I've implemented a simpler way of replacing transparent pixels with predefined color:

    public static BufferedImage fillTransparentPixels( BufferedImage image, 
                                                       Color fillColor ) {
        int w = image.getWidth();
        int h = image.getHeight();
        BufferedImage image2 = new BufferedImage(w, h, 
            BufferedImage.TYPE_INT_RGB);
        Graphics2D g = image2.createGraphics();
        g.setColor(fillColor);
        g.fillRect(0,0,w,h);
        g.drawRenderedImage(image, null);
        g.dispose();
        return image2;
    }
    

    and I call this method before jpeg conversion in this way:

    if( inputImage.getColorModel().getTransparency() != Transparency.OPAQUE) {
        inputImage = fillTransparentPixels(inputImage, Color.WHITE);
    }
    

提交回复
热议问题