我編寫了一些類似於繪畫的東西。我有JPanel,我可以借鑑它。我只使用黑線。我想將它轉換爲二進制數組,其中1是像素爲黑色時,0爲白色時(背景)。這是可能的?這個怎麼做?將JPanel轉換爲二進制數組
2
A
回答
2
簡而言之,創建一個尺寸與您的JPanel和paint the panel相同的BufferedImage
。然後,您可以遍歷圖像柵格以獲取與黑色和白色相對應的像素顏色值序列。例如
// Paint the JPanel to a BufferedImage.
Dimension size = jpanel.getSize();
int imageType = BufferedImage.TYPE_INT_ARGB;
BufferedImage image = BufferedImage(size.width, size.height, imageType);
Graphics2D g2d = image.createGraphics();
jpanel.paint(g2);
// Now iterate the image in row-major order to test its pixel colors.
for (int y=0; y<size.height; y++) {
for (int x=0; ix<size.width; x++) {
int pixel = image.getRGB(x, y);
if (pixel == 0xFF000000) {
// Black (assuming no transparency).
} else if (pixel == 0xFFFFFFFF) {
// White (assuming no transparency).
} else {
// Some other color...
}
}
}
+0
參見[ComponentImageCapture.java] (http://stackoverflow.com/questions/5853879/java-swing-obtain-image-of-jframe/5853992#5853992)。 – 2011-12-15 19:35:35
相關問題
- 1. 將二進制數組轉換爲int16
- 2. 將二進制向量轉換爲十進制數組
- 3. 使用數組將十進制轉換爲二進制
- 4. 將圖像轉換爲二進制數組和二進制數組轉換爲java中的圖像
- 5. 將二進制數字轉換爲二進制數字
- 6. 將二進制轉換爲十進制
- 7. 將八進制轉換爲二進制
- 8. 將十進制轉換爲二進制
- 9. 將RGBA numpy數組轉換爲二進制2d numpy數組
- 10. 將十進制數位數組轉換爲二進制數位數組
- 11. 將基數10轉換爲二進制
- 12. python十進制數組轉換爲二進制數efficienty
- 13. 將兩個整數轉換爲二進制數組?
- 14. Prolog - 將二進制函數轉換爲n進制函數
- 15. 如何將二進制數(32位)轉換爲十進制數?
- 16. 將十進制數字轉換爲6位二進制數字
- 17. 如何將二進制數字轉換爲八進制數字?
- 18. 將十六進制數組轉換爲二進制然後轉換爲十進制
- 19. 將十六進制轉換爲二進制,然後轉換爲十進制
- 20. 如何將數字(十進制)轉換爲二進制(二進制)數字和從二進制到十進制?
- 21. 如何將二進制文件轉換爲字節數組?
- 22. 如何將二進制位轉換爲數組
- 23. 在java中將二進制文件轉換爲字節數組?
- 24. Java初學者:將圖像轉換爲二進制數組
- 25. Cython將二進制字符串快速轉換爲int數組
- 26. 在PHP中將二進制字節數組轉換爲圖像
- 27. 使用HTML5將圖像轉換爲二進制數組(blob)
- 28. 將二進制字符串表示轉換爲字節數組
- 29. 將位圖轉換爲二進制數組性能
- 30. 將1和0的數組轉換爲二進制變量
http://stackoverflow.com/questions/113897/how-do-i-get-the-image-paint-paintcomponent-generates應該幫助你起步 – Robin 2011-12-15 19:11:10