I am trying to develop an image recognition android application... I am using the customized camera using surface view.... As Herein android to capture images ... I want to
I assume that you have added opencv library to your project successfully.
Here is a sample code using OpenCV4android.
public class SampleCameraFrameAccessActivity extends Activity implements CvCameraViewListener2, OnTouchListener{
private static final String TAG = "SampleCameraFrameAccessActivity";
protected CameraBridgeViewBase cameraPreview;
protected Mat mRgba;
protected BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) {
@Override
public void onManagerConnected(int status) {
switch (status) {
case LoaderCallbackInterface.SUCCESS:
{
Log.i(TAG, "OpenCV loaded successfully");
// mOpenCvCameraView.enableView();
// mOpenCvCameraView.setOnTouchListener(ColorRegionDetectionActivity.this);
cameraPreview.enableView();
} break;
default:
{
super.onManagerConnected(status);
} break;
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.camera_sample_layout);
cameraPreview = (CameraBridgeViewBase) findViewById(R.id.sample_test_camera_view);
cameraPreview.setCvCameraViewListener(this);
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
if(cameraPreview != null){
cameraPreview.disableView();
}
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_3, this, mLoaderCallback);
}
@Override
public void onCameraViewStarted(int width, int height) {
// TODO Auto-generated method stub
mRgba = new Mat(height, width, CvType.CV_8UC4);
}
@Override
public void onCameraViewStopped() {
// TODO Auto-generated method stub
mRgba.release();
}
@Override
public Mat onCameraFrame(CvCameraViewFrame inputFrame) {
// TODO Auto-generated method stub
mRgba = inputFrame.rgba();
return mRgba;
}
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
return false;
}
}
And the XML Layout file is :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/sample_test_layout" >
<org.opencv.android.JavaCameraView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/sample_test_camera_view" />
</RelativeLayout>
In onCameraFrame method you can access every frame from the frame buffer of the camera. if you want to capture an image you can add a button and take a particular frame from the buffer and process it. The frame is given as Mat object by default.So you don't have to convert it. after processing, if you need it to converted into bitmap, you can call Utils.matToBitmap(mat, bmp); method to do that.