使用OpenCV打开Android原生相机

时间:2013-06-18 09:52:24

标签: java android opencv camera

我只需要知道如何从这开始实际上我需要使用opencv打开Android原生相机。

我在哪里可以找到相关文档或任何帮助材料? 我设置了我的eclipse使用opencv示例项目!

1 个答案:

答案 0 :(得分:15)

看看opencv / samples / android /中提供的opencv示例,你应该能找到一些很好的例子。以下是显示how to open the camera的文档的链接。不要忘记请求访问摄像头的权限。

简短版本,请参阅链接以获取完整详细信息: 添加布局:

<org.opencv.android.JavaCameraView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:visibility="gone"
    android:id="@+id/HelloOpenCvView"
    opencv:show_fps="true"
    opencv:camera_id="any" />

初始化程序:

private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) {
    @Override
    public void onManagerConnected(int status) {
        switch (status) {
            case LoaderCallbackInterface.SUCCESS:
            {
                Log.i(TAG, "OpenCV loaded successfully");
                mOpenCvCameraView.enableView();
            } break;
            default:
            {
                super.onManagerConnected(status);
            } break;
        }
    }
};

@Override
public void onResume()
{
    super.onResume();
    OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_3, this, mLoaderCallback);
}

实施CVFrameListener2接口:

 private CameraBridgeViewBase mOpenCvCameraView;

 @Override
 public void onCreate(Bundle savedInstanceState) {
     Log.i(TAG, "called onCreate");
     super.onCreate(savedInstanceState);
     getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
     setContentView(R.layout.HelloOpenCvLayout);
     mOpenCvCameraView = (CameraBridgeViewBase) findViewById(R.id.HelloOpenCvView);
     mOpenCvCameraView.setVisibility(SurfaceView.VISIBLE);
     mOpenCvCameraView.setCvCameraViewListener(this);
 }

 @Override
 public void onPause()
 {
     super.onPause();
     if (mOpenCvCameraView != null)
         mOpenCvCameraView.disableView();
 }

 public void onDestroy() {
     super.onDestroy();
     if (mOpenCvCameraView != null)
         mOpenCvCameraView.disableView();
 }

 public void onCameraViewStarted(int width, int height) {
 }

 public void onCameraViewStopped() {
 }

 public Mat onCameraFrame(CvCameraViewFrame inputFrame) {
     return inputFrame.rgba();
 }