Every once in a while I start a new computer vision project with Android. And I am always facing the same question: “What do I need again to retrieve a camera image ready for processing?”. While there are great tutorials around I just want a downloadable project with a minimum amount of code – not taking pictures, not setting resolutions, just the continuous retrieval of incoming camera frames.
So here they are – two different “Hello World” for computer vision. I will show you some excerpts from the code and then provide a download link for each project.
Pure Android API
The main problem to solve is how to store the camera image into a processable image format – in this case the android.graphics.Bitmap
.
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
if(camera != null) {
camera.release();
camera = null;
}
camera = Camera.open();
try {
camera.setPreviewDisplay(holder);
} catch (IOException e) {
e.printStackTrace();
}
camera.setPreviewCallback(new PreviewCallback() {
public void onPreviewFrame(byte[] data, Camera camera) {
System.out.println("Frame received!"+data.length);
Size size = camera.getParameters().getPreviewSize();
/*
* Directly constructing a bitmap from the data would be possible if the preview format
* had been set to RGB (params.setPreviewFormat() ) but some devices only support YUV.
* So we have to stick with it and convert the format
*/
int[] rgbData = convertYUV420_NV21toRGB8888(data, size.width, size.height);
Bitmap bitmap = Bitmap.createBitmap(rgbData, size.width, size.height, Bitmap.Config.ARGB_8888);
/*
* TODO: now process the bitmap
*/
}
});
camera.startPreview();
}
Notice the function convertYUV420_NV21toRGB8888() which is needed since the internal representation of camera frames does not match any supported Bitmap format.
Using OpenCV
This is even more straight-forward. We just use OpenCV’s JavaCameraView. If you are new to Android+OpenCV, here is a good tutorial for you.
cameraView = (CameraBridgeViewBase) findViewById(R.id.cameraView);
cameraView.setCvCameraViewListener(this);