gpt4 book ai didi

java - 如何将 gif 图像转换为 jpg?

转载 作者:行者123 更新时间:2023-11-29 04:00:52 29 4
gpt4 key购买 nike

像这样 gif image

背景透明

当我使用 ImageIO.write(image,"jpg", file) 保存时,它被破坏了

损坏的结果是here

如何解决这个问题?谢谢

我的代码:

import java.awt.image.BufferedImage;
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);
}
}

我用过:

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
}
}
}

here 查看

不对,结果是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();

here查看

关于java - 如何将 gif 图像转换为 jpg?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3717052/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com