0
我正在尋找更快的方式來創建大型(12291x12291)BufferedImages。我使用Graphics2D g2d = image.createGraphics()來繪製它,將它保存到一個文件,然後使用g.drawImage(image,0,0,null);加載它。我正在保存該文件,以便稍後可以加載特定圖像。截至目前,創建圖像需要大約45秒到一分鐘的時間。有什麼方法可以更快地創建這些圖像?創建大型BufferedImages的更快方法
我正在創建這些圖像中的兩個。
public void drawTiles() {
Graphics2D g2d = tiles1.createGraphics();
left = 0;
top = 0;
//The drawing takes around 10 to 20 seconds
Colors c = new Colors();
for (double[] row : data) {
for (double d : row) {
int v = (int) (20 - d/500);
if (v < 0) {
v = 0;
} else if (v > 20) {
v = 20;
}
color = v;
g2d.setColor(c.colors[color]);
g2d.fillRect(left, top, sizeX, sizeY);
left += sizeX;
if (left == size * sizeX) {
left = 0;
top += sizeY;
}
}
}
g2d.dispose();
//The creation of the file takes little time for an image of this size
try {
File tiles1file = new File("path/to/map" + id + ".bmp");
ImageIO.write(tiles1, "bmp", tiles1file);
} catch (Exception ex) {
ex.printStackTrace();
}
id++;
}
確實花費很長的「創建圖像」部分時間?你能顯示你的代碼嗎? –
與從頭開始在內存中完全創建圖像相比,壓縮和解壓縮PNG(使用DEFLATE)的速度並不快。 – Nayuki
但是,如何嘗試BMP文件?它們是未壓縮的,主要受磁盤速度(〜100 MB/s)的限制。您的12291×12291圖像大約需要450 MB。 – Nayuki