简体   繁体   English

具有Android Camera Surfaceview的OpenCv

[英]OpenCv with Android Camera Surfaceview

I am trying to develop an image recognition android application... I am using the customized camera using surface view.... As Here in android to capture images ... I want to process the captured images using opencv , how can i get the captured image and convert it to mat ? 我想开发图像识别Android应用程序......我使用的是定制的相机使用表面观.... 这里的机器人来捕捉图像...我想用OpenCV的处理捕获的图像,我怎么能得到捕获的图像并将其转换为mat? also is there a way to save the captured image Temporary ? 还有一种方法可以保存捕获的图像临时吗? thanks in advance 提前致谢

I assume that you have added opencv library to your project successfully. 我假设您已将opencv库成功添加到项目中。

Here is a sample code using OpenCV4android. 这是使用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布局文件是:

<?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. 在onCameraFrame方法中,您可以从摄像机的帧缓冲区访问每个帧。 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. 该框架默认为Mat对象,因此您无需对其进行转换。 after processing, if you need it to converted into bitmap, you can call Utils.matToBitmap(mat, bmp); 处理后,如果需要将其转换为位图,则可以调用Utils.matToBitmap(mat,bmp);。 method to do that. 做到这一点的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM