具体也就是怎么用 getRGB(int startX, int startY, int w, int h, int[] rgbArray, int offset, int scansize)
这个方法 最好给个例子 而且最好是 存到Byte数组中
注:api我已经看过了 希望能得到一个用这个函数的例子
上述代码是
1,读取一个bmp文件,把bmp的所有像素用rgbArray存储起来,
2,然后取其中一个像素点(x0,y0),把它构造成一个Color对象,
3,构造一个类型一样的BufferedImage imgOut,把像素矩阵rgbArray写到BufferedImage,
4,把imgOut写入文件
这个Color对象有getRed,getBlue,getBlack方法,可以分别获取这个像素在三个颜色分量上的灰度值。
[code]
public static void main(String[] args) {
OutputStream output = null;
try {
// read bmp
BufferedImage img = ImageIO.read(new File("F:/temp/1.bmp"));
int imageType = img.getType();
int w = img.getWidth();
int h = img.getHeight();
int startX = 0;
int startY = 0;
int offset = 0;
int scansize = w;
// rgb的数组
int[] rgbArray = new int[offset + (h - startY) * scansize
+ (w - startX)];
img.getRGB(startX, startY, w, h, rgbArray, offset, scansize);
int x0 = w / 2;
int y0 = h / 2;
int rgb = rgbArray[offset + (y0 - startY) * scansize
+ (x0 - startX)];
Color c = new Color(rgb);
System.out.println("中间像素点的rgb:" + c);
// create and save to bmp
File out = new File("F:/temp/2.bmp");
if (!out.exists())
out.createNewFile();
output = new FileOutputStream(out);
BufferedImage imgOut = new BufferedImage(w, h, imageType);
imgOut.setRGB(startX, startY, w, h, rgbArray, offset, scansize);
ImageIO.write(imgOut, "bmp", output);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (output != null)
try {
output.close();
} catch (IOException e) {
}
}
}
[/code]
int的rgb(4 byte)共有四个信息,rgb的格式是0xFFFFFFFF,分别表示 alpha 信息:R分量:G分量:B分量,各占8bit。所以你所谓的byte数组不知道是什么情况,一个byte是无法存储一个像素的。