grabFrame() to return a string of pixel information?
Hi everyone,
Basically what im looking for is creating a 2d array of values(but id be happy with a string) corrosponding to each of the 320*240 pixels of my camera. At the moment i can get back an awt image using:
Buffer buf = frameGrabber.grabFrame();
Image img = (new BufferToImage((VideoFormat) buf.getFormat()).createImage(buf));
I would of thought that i should meerly use the toString() method. But if i toString() buf i get:
javax.media.Buffer@22c95b
and if i toString() img i get:
BufferedImage@22c95b: type = 1 DirectColorModel: rmask=ff0000 gmask=ff00 bmask=ff amask=0 IntegerInterleavedRaster: width = 320 height = 240 #Bands = 3 xOff = 0 yOff = 0 dataOffset[0] 0
If anyone could help, and point me in the direction of the right method to use, i would be greatly appreciative.
Thanks.
[856 byte] By [
domdomdoma] at [2007-10-3 10:41:12]

You will need integer values from an image to do anything meaningful with the data. Something like this -
int redValues = new int[imageWidth * imageHeight];
int greenValues = new int[imageWidth * imageHeight];
int blueValues = new int[imageWidth * imageHeight];
int alphaValues = new int[imageWidth * imageHeight];
int counter = 0;
public void grabEveryPixelInThe Image(Image img, int x, int y, int w, int h) {
int[] pixels = new int[w * h];
PixelGrabber pg = new PixelGrabber(img, x, y, w, h, pixels, 0, w);
try {
pg.grabPixels();
} catch (InterruptedException e) {
System.err.println("interrupted waiting for pixels!");
return;
}
if ((pg.getStatus() & ImageObserver.ABORT) != 0) {
System.err.println("image fetch aborted or errored");
return;
}
for (int j = 0; j < h; j++) {
for (int i = 0; i < w; i++) {
doOnePixel(pixels[j * w + i]);
}
}
}
public void doOnePixel( int pixel) {
int alpha = (pixel >> 24) & 0xff;
int red= (pixel >> 16) & 0xff;
int green = (pixel >> 8) & 0xff;
int blue = (pixel) & 0xff;
redValues[ counter] = red ;
greenValues [ counter] = green ;
blueValues [ counter] = blue ;
AlphaValues [ counter ] = alpha ;
counter++;
}
regards
hi, just thought id let ya know what ive done....
Instead of converting the buffer to an image, ive skipped that completely and gone with just reading the buffer data.
byte[] outputData
outputData = (byte[]) buf.getData();
seems to work. although if anyone could let me know if this method is faster or slower than converting it to an image and then getting the pixel information. Please let me know, Thanks :P