user1832787
user1832787

Reputation: 69

java buffered image resulting in blank image

I am trying to extract all the frames out of a gif and put them in a column with the next frame under the last. So a person can just scroll down a tall image and see the gif. I can extract all the frames but when I try to write it out I get just a black canvas. The width and height are correct and its saying it read the images right. Whats going wrong here?

    String img = "test.gif"; //original gif
    String[] temp = img.split(".gif");
    String base = temp[0];

    try {
        ImageReader reader = ImageIO.getImageReadersBySuffix("GIF").next();
        ImageInputStream in = ImageIO.createImageInputStream(new File(img));
        reader.setInput(in);

        int rows = reader.getNumImages(true);  // How many images there will be 
        int cols = 1;
        int chunks = rows;
        int chunkWidth, chunkHeight;
        int type;

        type = reader.read(0).getType(); //Get single frame file type
        chunkWidth = reader.read(0).getWidth(); //Get single frame width
        chunkHeight = reader.read(0).getHeight();  //Get single frame height

        //Initializing the final image
        BufferedImage finalImg = new BufferedImage(chunkWidth, chunkHeight * rows, type);

        for (int i = 0, count = reader.getNumImages(true); i < count; i++) {
            BufferedImage image = reader.read(i); //read next frame from gif
            finalImg.createGraphics().drawImage(image, chunkWidth, chunkHeight * i, null); //append new image to new file
        }

        System.out.println("Image concatenated.....");
        ImageIO.write(finalImg, "png", new File("finalImg1234555.png")); //final png with all gif images in it

    } catch (IOException ex) {
        Logger.getLogger(Gifextract.class.getName()).log(Level.SEVERE, null, ex);
    }

Upvotes: 2

Views: 1396

Answers (1)

Mike
Mike

Reputation: 2434

finalImg.createGraphics().drawImage(image, chunkWidth, chunkHeight * i, null);

The x-coordinate is chunkWidth, which means the left edge of the image starts at chunkWidth. Since finalImg only has a width of chunkWidth you are drawing completely outside of finalImg's bounds. I suspect the x-coordinate is supposed to be 0.

Upvotes: 3

Related Questions