因此,我正在创建一个程序,根据显示器上给定的 x,y 范围计算纯黑色像素。唯一的问题是下面提供的这个方法需要很长时间才能运行,而在java中,它可以在近乎瞬间运行。有人可以帮我解决这个问题吗?
CPP代码
bool hasSufficientBlackPixels(){
int num = 0;
int r,g,b;
HDC hdc = GetDC(NULL);
COLORREF color;
for(int x = 416; x<2049; x+=2) {
for(int y = 342; y<973; y+=2) {
color = GetPixel(hdc, x, y);
r = (color>>16) & 0xFF;
g = (color>>8) & 0xFF;
b = color & 0xFF;
// cout<<"r, g , b: (" << r << ", " << g << ", "<<b << ")\n";
if(r+g+b==0) { // is black pixel
num++;
}
}
}
cout<<num;
return num <= 100000 && num >= 8000;
}
Java代码
public boolean isSufficientBlackPixels() throws AWTException, IOException {
Robot robot = new Robot();
BufferedImage screenshot = robot.createScreenCapture(new Rectangle(x1, y1, x2 - x1, y2 - y1));
int blackPixelCount = 0;
for (int y = 0; y < screenshot.getHeight(); y += 2) {
for (int x = 0; x < screenshot.getWidth(); x += 2) {
int rgb = screenshot.getRGB(x, y);
int red = (rgb >> 16) & 0xFF;
int green = (rgb >> 8) & 0xFF;
int blue = rgb & 0xFF;
if (red == 0 && green == 0 && blue == 0) {
blackPixelCount++;
}
}
}
return blackPixelCount <= 100000 && blackPixelCount >= 8000;
}
我的 CPP 代码需要一种高效的速度方法
您可能想看看 OpenCv。 您可以轻松计算零的数量: https://stackoverflow.com/a/28981342/11167943