OpenCv Camera View提供Mat对象获取错误数据

时间:2014-01-28 20:58:30

标签: android opencv camera buffer frame

我正在尝试使用OpenCv4android为Android开发增强现实应用程序。我需要处理相机帧缓冲区。为了查看帧缓冲区矩阵的内容,我将Mat对象转换为Bitmap并编写了一个png图像。我得到的只是一大块绿色像素。相机缓冲区应该没问题,因为opencv正常。我的代码如下。

public class OpenGLSurfaceWithCameraTest extends Activity implements CvCameraViewListener2{


private static final String  TAG = "OpenGLSurfaceWithCamera";
Mat mRgba;

boolean flag = true;

private CameraBridgeViewBase cameraPreview;

private BaseLoaderCallback  mLoaderCallback = new BaseLoaderCallback(this) {
    @Override
    public void onManagerConnected(int status) {
        switch (status) {
            case LoaderCallbackInterface.SUCCESS:
            {
                Log.i(TAG, "OpenCV loaded successfully");
                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_with_opengl);


    cameraPreview = (CameraBridgeViewBase)findViewById(R.id.camera_view_opengl);


    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();


    Mat tmp = new Mat(mRgba.rows(), mRgba.cols(), CvType.CV_8UC1, new Scalar(4));
    Imgproc.pyrDown(mRgba, tmp);
    Imgproc.pyrDown(tmp, tmp);


    Bitmap bitmap = Bitmap.createBitmap(tmp.cols(), tmp.rows(), Config.ARGB_8888);

    Utils.matToBitmap(tmp, bitmap);

    if(flag){
    try {
        File root = null;
        String state = Environment.getExternalStorageState();
        if(state.equals(Environment.MEDIA_MOUNTED)){
            root = new File(Environment.getExternalStorageDirectory(), "aqib");
            if(root == null){
                Log.e(TAG, "root is null");
            }
            if(!root.exists()){
                root.mkdirs();
            }
        }

        if(root.exists()){
            Log.e(TAG, "root exists");
        }
        File file = new File(root, "test.png");
        OutputStream stream = new FileOutputStream(file);
        bitmap.compress(Bitmap.CompressFormat.PNG, 90, stream);
        stream.close();
        flag = false;
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        Log.e(TAG, e.getMessage());
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

    bitmap.recycle();
    return mRgba;
}

}

有人可以建议我如何获得相机帧缓冲区的正确输出,以便我可以在opengl中使用它进行处理吗?

1 个答案:

答案 0 :(得分:0)

显然,在我的情况下,第一帧始终是绿色框架。尝试从晚些时候开始编写框架,如10日或类似的东西,并且它有效。