开发者

Fast gathering image on screen and reading the pixels

开发者 https://www.devze.com 2023-03-21 17:23 出处:网络
I\'m trying to get a small section of image on the screen and开发者_Go百科 read any pixel to compare the other pixels.The code to get screen image is:

I'm trying to get a small section of image on the screen and开发者_Go百科 read any pixel to compare the other pixels.The code to get screen image is:

Rectangle captureSize = new Rectangle(x, y, height, width);
BufferedImage image = robot.createScreenCapture(captureSize);

And, to read pixel by pixel I used

  for (int y = 0; y < image.getHeight(); y = y + 1) {
        for (int x = 0; x < image.getWidth(); x = x + 1) {
            color = image.getRGB(x, y);
            // Some methods etc
        {
{

However, when I ran it I was shocked. Because createScreenCapture took about 40 ms and using getRGB of each pixel took around 350 ms which is very inefficient to create an application for 60 fps. By the way, my image is 800x400 pixels size. I didn't try

rgbArray = image.getRGB(startX, startY, w, h, rgbArray,offset, scansize) ;

method because I don't know how efficient it is and to reorder my code would be a bit difficult. So, any help would be appreciated.


Use

rgbArray = image.getRGB(startX, startY, w, h, rgbArray,offset, scansize) ;

It will be much faster to read the pixel values from the array than to do the method call to get each pixel value, and the single call to getRGB to fetch the array is not slow.

0

精彩评论

暂无评论...
验证码 换一张
取 消