opencv的相机预览显示错误/不正确的图像。使用javacameraview

时间:2016-03-11 01:08:14

标签: android opencv opencv4android

我使用的系统是: Mac + Eclipse + Opencv4Android(v3.0)

除了相机显示的结果或质量外,所有样品和一切都正常运行。这是下面的样子 http://imgur.com/fGgw1Sr

这不仅仅是通过模拟器,还有我上传到手机时。 在图像中平板电脑的外壳实际上是鲜红色,手机是蓝色的。这是来自实际的Android相机应用程序的清晰图像: http://imgur.com/kSdDVSI

顶部的透明层也很麻烦。我认为这是因为alpha通道,但我已经尝试删除它也为什么会出现4次?

我只使用了一个基本代码来设置我的opencv AndroidManifest.xml文件:

<uses-sdk android:minSdkVersion="19" android:targetSdkVersion="21" />

<uses-permission android:name="android.permission.CAMERA"/>

<uses-feature android:name="android.hardware.camera" android:required="false"/>
<uses-feature android:name="android.hardware.camera.autofocus" android:required="false"/>
<uses-feature android:name="android.hardware.camera.front" android:required="false"/>
<uses-feature android:name="android.hardware.camera.front.autofocus" android:required="false"/>

MainActivity:

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

            // Load native library after(!) OpenCV initialization
            // System.loadLibrary("mixed_sample");

            mOpenCvCameraView.enableView();
            break;
        }
        default: {
            super.onManagerConnected(status);
        }
            break;
        }
    }
};

@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.activity_main);
    mOpenCvCameraView = (JavaCameraView) findViewById(R.id.aslgesturerecogapp_surface_view);
    mOpenCvCameraView.setVisibility(SurfaceView.VISIBLE);
    mOpenCvCameraView.setCvCameraViewListener(this);

}

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

这是我唯一的布局文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:opencv="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.aslgesturerecogapp.MainActivity" >

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

1 个答案:

答案 0 :(得分:0)

我假设您已成功将opencv库添加到项目中。

以下是使用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;
}

}

XML Layout文件是:

<?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>

你可以试试这个。