java缓冲图像导致空白图像

标签 java bufferedimage

我试图从 gif 中提取所有帧并将它们放入一列中,下一帧位于最后一帧下方。因此,人们只需向下滚动一张高图像即可看到 gif。我可以提取所有框架,但当我尝试将其写出来时,我只得到一个黑色 Canvas 。宽度和高度是正确的,并且它说它读取图像是正确的。这里出了什么问题?

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

最佳答案

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

x 坐标为 chunkWidth,这意味着图像的左边缘从 chunkWidth 开始。由于 finalImg 的宽度仅为 chunkWidth,因此您的绘制完全超出了 finalImg 的边界。我怀疑 x 坐标应该是 0

关于java缓冲图像导致空白图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14180789/

相关文章:

java - 从 java 应用程序到 ffmpeg 子进程的管道系列图像

java - 即使在 Springboot 应用程序的过滤器内指定 Access-Control-Allow-Origin header 后,也无法控制跨源请求

java - 如何使用 Java 解析具有不同键和值的 JSON 对象?

java - 字符串的第一个和最后一个字符

java - image.getRaster().getDataBuffer() 返回负值数组

java - 如何将 JLabel 和 JTextField 放在图像之上?

java - 使用正则表达式,如何将属性值从一种模式更改为另一种模式

Java向类添加方法

java - 规避 BufferedImage 的数组大小 > Integer.MAX_VALUE?

java - 如何在java Graphics2d中使用TextLayout在短字符串上进行自动换行?