Write back modified image from imageReader

前端 未结 1 467
日久生厌
日久生厌 2021-01-24 15:33

I read separated images from animated gif and I need just make change with them in frames[i] and then set them back to file and save with writer as new file.

相关标签:
1条回答
  • 2021-01-24 16:00

    Tanks @StanislavL for link (https://community.oracle.com/thread/1264385) in comment, it was really helpful. There is my solution:

    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);    
    }
    
    0 讨论(0)
提交回复
热议问题