简体   繁体   中英

Graphics.drawImage leaves BufferedImage empty

I'm trying to extract 10 px square sections of a BufferedImage and add them to a new BufferedImage, very similar to the jumble example shown in this drawImage tutorial . However, my BufferedImage appears to remain empty after my drawImage call. If I call drawString, I see the String is drawn normally.

In the documentation for Graphics.drawImage , there is the statement

This method returns immediately in all cases, even if the image area to be drawn has not yet been scaled, dithered, and converted for the current output device. If the current output representation is not yet complete then drawImage returns false. As more of the image becomes available, the process that loads the image notifies the specified image observer.

Do I need an ImageObserver to wait for the output? If so what implementing class should I use? If not, what is the correct solution?

MCVE

import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public class TestImageScale {

    public static void main(String[] args) throws IOException {
        BufferedImage img = ImageIO.read(new File(
                "example.jpeg"));

        //Randomly generate some coordinates to grab pixels from
        int minDim = Math.min(img.getHeight(), img.getWidth()) - 10;
        int[][] coordMatch = new int[5][3];
        for (int i = 0; i < 5; i++) {
            coordMatch[i][0] = (int) Math.floor(Math.random() * minDim + 5);
            coordMatch[i][1] = (int) Math.floor(Math.random() * minDim + 5);
            coordMatch[i][2] = 5;
        }

        BufferedImage simImg = new BufferedImage(10, 10 * 5, BufferedImage.TYPE_INT_ARGB);
        Graphics g = simImg.getGraphics();

        for (int i = 0; i < 5; i++) {
            int x = coordMatch[i][0];
            int y = coordMatch[i][1];
            int r = coordMatch[i][2];
            //Print statement to show that we are in the for loop
            System.out.println(String.format("%s,%s,%s", x, y, r));
            //g.drawImage should write the pixels from img to simImg
            g.drawImage(img, x - r, y - r, x + r, y + r, 0, i * 10, 10, i * 10 + 10, null);
        }
        //I draw the string "hello" on simImg to show that g is working
        g.drawString("hello", 0, 10);

        ImageIO.write(simImg, "png", new File("output.png"));
    }


}

In a test run, I get the lines

322,228,5
118,186,5
285,351,5
21,213,5
144,48,5

printed to the console and the saved file looks like

输出示例显示打印字符串“ hello”的开始

This output shows that the for loop is entered and that the Graphics object g is connected to the correct BufferedImage . But the pixels from img are not being copied to simImg .

As requested, an example image for testing, from Matlab's demo images. But really, this has behaved this way with every image that I have tested. example.png

I think you have some of the arguments to drawImage in the wrong order.

The Javadoc for the drawImage method you are using mentions that the coordinates of the destination rectangle come before the coordinates of the source rectangle. It looks to me as if you're calling this method with the source coordinates first and the destination coordinates second.

Instead of

        g.drawImage(img, x - r, y - r, x + r, y + r, 0, i * 10, 10, i * 10 + 10, null);

try

        g.drawImage(img, 0, i * 10, 10, i * 10 + 10, x - r, y - r, x + r, y + r, null);

I don't have your test image, so I used another image I instead. After making this modification and running your class, I got an output image that wasn't entirely blank and looked to have copied pixels from my sample image.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM