So I found a surprisingly simple method that appears to work. The
method below converts JPEGs, as captured by the camera, and converts
them to RGB888. I've run these through BitmapToMat and back again, as
well as successfully running them through a CascadeClassifier:
/** Takes a JPEG captured by the device camera and converts it to
RGB888 format */
private Bitmap JPEGtoRGB888(Bitmap img)
{
int numPixels = img.getWidth()* img.getHeight();
int[] pixels = new int[numPixels];
//Get JPEG pixels. Each int is the color values for one
pixel.
img.getPixels(pixels, 0, img.getWidth(), 0, 0, img.getWidth(),
img.getHeight());
//Create a Bitmap of the appropriate format.
Bitmap result = Bitmap.createBitmap(img.getWidth(),
img.getHeight(), Config.ARGB_8888);
//Set RGB pixels.
result.setPixels(pixels, 0, result.getWidth(), 0, 0,
result.getWidth(), result.getHeight());
return result;
}
Usage:
Be sure that your camera format is set to JPEG:
Camera.Parameters parameters = myCamera.getParameters();
//RGB888 not natively supported by the camera
parameters.setPictureFormat(ImageFormat.JPEG);
myCamera.setParameters(parameters);
In your JPEG camera callback:
PictureCallback handleJpeg = new PictureCallback() {
public void onPictureTaken(byte[] _data, Camera _camera) {
Bitmap bmp=BitmapFactory.decodeByteArray(_data,0,_data.length);
bmp = JPEGtoRGB888(bmp);
...
}
}
Like I said, this seems to work for me. It's matching just fine
through CascadeClassifiers can be run through the Mat conversion
methods.
For some reason I'm having trouble marking up the resulting Mats with
Core.rectangle, but I'm 80% confident that that's unrelated.
I hope this helps!
-Aaron