小编典典

从imageReader写回修改后的图像

java

我从动画gif中读取了分离的图像,我只需要对其进行更改,frames[i]然后将其设置回文件,并与编写者一起另存为新文件。

    File file = new File("gif1.gif");
    ImageReader reader = ImageIO.getImageReadersBySuffix("GIF").next();
    ImageInputStream in = ImageIO.createImageInputStream(file);
    reader.setInput(in);

    File output = new File("k.gif");

    ImageWriter writer = ImageIO.getImageWritersBySuffix("GIF").next();
    ImageOutputStream out = ImageIO.createImageOutputStream(file);
    writer.setOutput(out);

    int numImages = reader.getNumImages(true); 
    BufferedImage[] frames = new BufferedImage[pocetObrazku];

    for (int i = 0; i < numImages; i++)
    {

        BufferedImage image = reader.read(i);
        frames[i] = reader.read(i);        
    }

我会很高兴为您提供任何帮助。


阅读 225

收藏
2020-11-30

共1个答案

小编典典

Tanks @StanislavL
在评论中提供了链接(https://community.oracle.com/thread/1264385),这确实很有帮助。有我的解决方案:

File file = new File("gif1.gif");
ImageReader reader = ImageIO.getImageReadersBySuffix("GIF").next();
ImageInputStream in = ImageIO.createImageInputStream(file);
reader.setInput(in);

int numImages = reader.getNumImages(true); 
BufferedImage[] frames = new BufferedImage[pocetObrazku];

File fileOut = new File("k.gif");
fileOut.createNewFile();
ImageWriter iw = ImageIO.getImageWritersByFormatName("gif").next();

ImageOutputStream ios = ImageIO.createImageOutputStream(fileOut);
iw.setOutput(ios);
iw.prepareWriteSequence(null);

for (int i = 0; i < numImages ; i++) {
    BufferedImage image = reader.read(i);
    frames[i] = reader.read(i);

    IIOMetadata metadata = reader.getImageMetadata(i);    
    IIOImage ii = new IIOImage(image, null, metadata);      
    iw.writeToSequence(ii, (ImageWriteParam) null);    
}
2020-11-30