int BufferedImage.getRGB(int x, int y)
用來抓一個區域的pixel value
int [] BufferedImage.getRGB(int startX, int startY, int w, int h,
int[] rgbArray, int offset, int scansize)
sample code:
import java.awt.image;
import javax.imageio;
BufferedImage source = ImageIO.read(inputfile);
int imageWidth = source.getWidth();
int imageHeight = source.getHeight();
int [][] pixelValue = new int[height][width];
for(int i = 0; i < pixelValue.length; i++) {
for(int j = 0; j < pixelValue[i].length; j++) {
pixelValue[i][j] = source.getRGB(j, i);
}
}
Note:
1. 使用BufferedImage.getRGB(int x, int y)讀進來的是pixel value,並不能
直接使用,還要透過下面補充的函數[1]才可以得到RGB value。
2. 影像中的座標值剛好與記憶體中Array存放的位置為transpose,需特別注意。
補充:
1. 將使用BufferedImage.getRGB(int x, int y)讀進來的pixel value轉成RGB值
private int[] getRGBFromPixel(int pixelvalue) {
int[] RGB = new int[4];
/* alpha value denotes the transparent level of pixel */
int alpha = (pixelvalue >> 24) & 0xff;
int red = (pixelvalue >> 16) & 0xff;
int green = (pixelvalue >> 8 ) & 0xff;
int blue = pixelvalue & 0xff;
RGB[0] = alpha;
RGB[1] = red;
RGB[2] = green;
RGB[3] = blue;
return RGB;
}
2. 將RGB value轉成BufferedImage中儲存的pixel value
private int returnToRGB(int R, int G, int B) {
int RGB = 0x00000000;
/* process alpha value */
int alpha = (0xff << 24);
/* process Red value */
int Red = (R & 0xff) << 16;
/* process Green value */
int Green = (G & 0xff) << 8;
/* process Blue value */
int Blue = (B & 0xff);
RGB = alpha | Red | Green | Blue;
return RGB;
}
可以用 void BufferedImage.setRGB(int x, int y, int RGB) 設定RGB值
沒有留言:
張貼留言