我试图让该方法在 50 毫秒内完成,但我似乎不知道如何提高该方法的整体速度。我使用像素对象是因为在压缩数据时需要能够检查空值。
public static Frame getFrame(Dimension d, Robot r, Rectangle s, Resolution rs)
{
int w = d.width;
int h = d.height;
BufferedImage b = r.createScreenCapture(s);
Pixel[] pixels = new Pixel[w * h];
for(int i = 0; i < w; i++)
{
for(int j = 0; j < h; j++)
{
pixels[j * w + i] = new Pixel(b.getRGB(i, j));
}
}
return new Frame(rs, pixels, true);
}
这是 Pixel 类的构造函数
public Pixel(int c)
{
if((c & 0xFF) == 0xA || (c & 0xFF) == 0xD)
c++;
if((c & 0xFF00) == 0xA00 || (c & 0xFF00) == 0xD00)
c += 0x100;
if((c & 0xFF0000) == 0xA0000 || (c & 0xFF0000) == 0xD0000)
c += 0x10000;
if((c & 0xFF000000) == 0xA000000 || (c & 0xFF000000) == 0xD000000)
c += 0x1000000;
color = c;
}
这是 Frame 类的构造函数
public Frame(Resolution res, Pixel[] pix, boolean ignoreCheck)
{
if(!ignoreCheck)
{
if(pix.length < res.getTotalPixels())
throw new NotEnoughPixelsException(res.getTotalPixels() - pix.length);
else if(pix.length > res.getTotalPixels())
throw new TooManyPixelsException(pix.length - res.getTotalPixels());
}
resolution = res;
pixels = pix;
}
最佳答案
不要使用 Pixel 类。您将 build 数十万个,甚至数百万个。
关于java - 提高java方法的效率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20693481/