如何在 Java 中有效地從 BufferedImage 中提取像素資料作為整數數組?
如何從 Java 中的圖像中提取像素資料作為整數數組
BufferedImage 提供了各種處理像素資料的方法。然而,其中一些方法對於有效存取像素資訊可能不是最佳的。在Java 中擷取像素資料有兩種主要方法:
-
使用BufferedImage 的getRGB() 方法:
此方法以整數形式擷取像素顏色,結合alpha、紅色、綠色和藍色值。雖然方便,但這種方法速度較慢,因為它需要解碼顏色資訊並將其重新排列到單獨的通道中。
-
直接存取像素數組:
要直接存取像素數組,可以使用以下程式碼:
byte[] pixels = ((DataBufferByte) bufferedImage.getRaster().getDataBuffer()).getData();
登入後複製此方法提供原始存取每個像素的紅色、綠色和藍色值。如果有 Alpha 通道,它也會包含在位元組數組中。雖然此方法需要更複雜的索引計算,但它比使用 getRGB() 快得多。
效能比較
基準測試顯示出顯著差異兩種方法之間的處理時間。從 getRGB() 切換到直接數組存取使得處理大圖像時的速度提高了 90% 以上。以下是用於比較效能的範例程式碼:
import java.awt.image.BufferedImage; import java.awt.image.DataBufferByte; import java.io.IOException; public class PerformanceComparison { public static void main(String[] args) throws IOException { BufferedImage image = ImageIO.read(PerformanceComparison.class.getResource("large_image.jpg")); // Using getRGB() long startTime = System.nanoTime(); int[][] resultRGB = convertTo2DUsingGetRGB(image); long endTime = System.nanoTime(); System.out.println("getRGB(): " + toString(endTime - startTime)); // Using direct array access startTime = System.nanoTime(); int[][] resultArray = convertTo2DWithoutUsingGetRGB(image); endTime = System.nanoTime(); System.out.println("Direct Array Access: " + toString(endTime - startTime)); } private static int[][] convertTo2DUsingGetRGB(BufferedImage image) { int width = image.getWidth(); int height = image.getHeight(); int[][] result = new int[height][width]; for (int row = 0; row < height; row++) { for (int col = 0; col < width; col++) { result[row][col] = image.getRGB(col, row); } } return result; } private static int[][] convertTo2DWithoutUsingGetRGB(BufferedImage image) { final byte[] pixels = ((DataBufferByte) image.getRaster().getDataBuffer()).getData(); final int width = image.getWidth(); final int height = image.getHeight(); int[][] result = new int[height][width]; int index = 0; for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { int alpha = pixels[index++] & 0xFF; int blue = pixels[index++] & 0xFF; int green = pixels[index++] & 0xFF; int red = pixels[index++] & 0xFF; result[y][x] = (alpha << 24) | (blue << 16) | (green << 8) | red; } } return result; } private static String toString(long nanos) { return String.format("%d min %d s %d ms", nanos / 60000000000L, (nanos % 60000000000L) / 1000000000L, (nanos % 1000000000L) / 1000000L); } }
請記住,提取像素資料的最佳方法取決於您的特定需求以及您正在使用的影像的大小和複雜性。
以上是如何在 Java 中有效地從 BufferedImage 中提取像素資料作為整數數組?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

公司安全軟件導致部分應用無法正常運行的排查與解決方法許多公司為了保障內部網絡安全,會部署安全軟件。 ...

將姓名轉換為數字以實現排序的解決方案在許多應用場景中,用戶可能需要在群組中進行排序,尤其是在一個用...

系統對接中的字段映射處理在進行系統對接時,常常會遇到一個棘手的問題:如何將A系統的接口字段有效地映�...

在使用IntelliJIDEAUltimate版本啟動Spring...

Java對象與數組的轉換:深入探討強制類型轉換的風險與正確方法很多Java初學者會遇到將一個對象轉換成數組的�...

在使用MyBatis-Plus或其他ORM框架進行數據庫操作時,經常需要根據實體類的屬性名構造查詢條件。如果每次都手動...

電商平台SKU和SPU表設計詳解本文將探討電商平台中SKU和SPU的數據庫設計問題,特別是如何處理用戶自定義銷售屬...

Redis緩存方案如何實現產品排行榜列表的需求?在開發過程中,我們常常需要處理排行榜的需求,例如展示一個�...
