我有一個簡單的服務器端代碼,接收一個字節數組表示一個JPEG格式的圖像,並返回圖像的尺寸。字節數組緩存圖像轉換慢
public String processImage(byte[] data) {
long startTime = System.currentTimeMillis();
ByteArrayInputStream stream = new ByteArrayInputStream(data);
BufferedImage bufferedImage;
bufferedImage = ImageIO.read(stream);
int height = bufferedImage.getHeight();
int width = bufferedImage.getWidth();
long endTime = System.currentTimeMillis();
return "height="+height+" | width="+width+" | elapsed="+(endTime-startTime);
}
它的工作原理,但問題是它是無法接受的緩慢。對於100KB的圖片,需要6秒。對於900KB的圖像,需要30秒。這是預期的嗎?有沒有辦法使字節數組bufferedImage轉換更快?
僅供參考,抓高度/寬度並不是我打算做的唯一事情。我最終想要處理bufferedImage。所以獲得高度/寬度只是一個示例代碼。
謝謝,我還沒有嘗試過,但我試圖在一個孤立的環境中運行相同的代碼,它只花了20毫秒200KB。所以這個問題看起來不像是代碼。上述設置是做什麼的? – baekacaek
@baekacaek:它將圖形卡排除在等式之外。如果您從未計劃在顯示器上顯示BufferedImage,則無需浪費時間將其加載到顯卡的RAM中。我在本地嘗試了你的代碼片段,上面的命令行選項將測試圖像上的運行時間從大約250ms降低到大約140ms。因人而異。 – Asaph