开发者

how to convert a gif image to jpg?

like this gif image

it has transparent background,

when i using ImageIO.write(image,"jpg", file) to save,it's be broken

the broken result is here

how to fix it problem? thank you

my code:

import java.awt.image.BufferedI开发者_开发知识库mage;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
import junit.framework.TestCase;

public class ImageResize1  extends TestCase{

    public void testT1() throws IOException{
        URL url=new URL("http://ec.europa.eu/culture/media/programme/images/logos/01_tr_media_col/01_tr_media_col_gif.gif");
        BufferedImage image=ImageIO.read(url);
        File file=new File("C:/temp/java/t7.jpg");
        ImageIO.write(image,"jpg", file);
    }
}

i used:

for(int x = 0; x < scaled.getWidth(); x++) {
    for(int y = 0; y < scaled.getHeight(); y++) {
        int rgb = scaled.getRGB(x, y);
        int alpha = (rgb >> 24) & 0xff;
        if(alpha != 255) {
            scaled.setRGB(x, y,-1); //set white
        }
    }
}

check it from here

it's not right,result is here


AreaAveragingScaleFilter scaleFilter =
    new AreaAveragingScaleFilter(
                Math.round(originalWidth / factorX),
                Math.round(originalHeight / factorY));
ImageProducer producer = new FilteredImageSource(original.getSource(), scaleFilter);
ImageGenerator generator = new ImageGenerator();
producer.startProduction(generator);
BufferedImage scaled = generator.getImage();

for(int x = 0; x < scaled.getWidth(); x++) {
    for(int y = 0; y < scaled.getHeight(); y++) {
        int rgb = scaled.getRGB(x, y);
        int alpha = (rgb >> 24) & 0xff;
        if(alpha != 255) {
            scaled.setRGB(x, y,-1); //set white
        }
    }
}


JPEGImageWriteParam param = new JPEGImageWriteParam(null);
param.setCompressionMode(JPEGImageWriteParam.MODE_EXPLICIT);
param.setCompressionQuality((float) 0.85);
java.util.Iterator<ImageWriter> it = ImageIO.getImageWritersBySuffix("jpg");
ImageWriter writer = it.next();
dest.getParentFile().mkdirs();
writer.setOutput(new FileImageOutputStream(dest));
writer.write(null, new IIOImage(scaled, null, null), param);
writer.dispose();   

Check it from here


JPG doesn't support transparency, so you can't.

Try using PNG, TIFF or GIF instead.


I can't comment on the code as I'm not a Java guru, but...

The JPEG format doesn't support transparent backgrounds, so if you're expecting that to be retained, it won't happen.

(not sure why you're doing the conversion, but if you need a high-colour format that does support transparency, use PNG instead)


Converting from one format to another is just the beginning :) if it was me I'll use ImageMagik by passing the command to OS and let the ImageMagick take care of the rest

ImageMagick homepage

Beside converting you can resize, add text and almost do all the photoshop tricks on your newly created image and it is easy.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜