Hri*_*tov 1 java optimization bufferedimage
我有以下Java代码:
public static BufferedImage createImage(byte[] data, int width, int height)
{
BufferedImage res = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);
byte[] rdata = ((DataBufferByte)res.getRaster().getDataBuffer()).getData();
for (int y = 0; y < height; y++) {
int yi = y * width;
for (int x = 0; x < width; x++) {
rdata[yi] = data[yi];
yi++;
}
}
return res;
}
Run Code Online (Sandbox Code Playgroud)
有更快的方法吗?
在C++中,我会使用memcpy,但在Java中?
或者也许可以直接用传递的数据初始化结果图像?
好吧,要快速复制数组,您可以使用System.arraycopy:
System.arraycopy(data, 0, rdata, 0, height * width);
Run Code Online (Sandbox Code Playgroud)
我不知道如何BufferedImage开始初始化,我担心.
你有没有尝试过:
res.getRaster().setDataElements(0, 0, width, height, data);
Run Code Online (Sandbox Code Playgroud)
?