java缓冲图像导致空白图像

时间:2013-01-06 09:20:03

标签: java bufferedimage

我正在尝试从gif中提取所有帧,并将它们放在一个列中,下一帧位于最后一帧之下。所以一个人可以向下滚动一个高大的图像,然后看到gif。我可以提取所有帧但是当我尝试写出来时,我只得到一块黑色画布。宽度和高度是正确的,它说它正确读取图像。这里出了什么问题?

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

1 个答案:

答案 0 :(得分:3)

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

x坐标为chunkWidth,表示图像的左边缘从chunkWidth开始。由于finalImg的宽度只有chunkWidth,因此您完全在finalImg的范围之外绘制。我怀疑x坐标应该是0