我试图让方法在不到50毫秒内完成,但我似乎无法弄清楚如何提高方法的整体速度。我正在为像素使用一个对象,因为我需要能够在压缩数据时检查null。
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;
}
答案 0 :(得分:4)
不要为Pixel使用类。你将建造数十万甚至数百万。