由于相机预览而产生OutOfMemoryError [英] OutOfMemoryError generated due to camera preview

查看:143
本文介绍了由于相机预览而产生OutOfMemoryError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设法使用opencv打开Android相机.但是,当我使用固定相机方向的代码-请参阅onCameraFrame(..)方法中下面提到的代码"时-几秒钟后应用崩溃,并且logcat在"logcat部分"中生成belwo发布的消息.

I managed to open the Android camera using opencv. but when i use the code that fixes the camera orientation -"see the code mentioned below in the onCameraFrame(..) method"- the App crashes after few seconds and the logcat generates the belwo posted messages in the "logcat section".

要解决此问题:

  1. 我尝试使用SystemClock.sleep强制应用延迟一段时间,但这不是一个好的解决方案,因为它会延迟相机预览

  1. i tried to use SystemClock.sleep to force the App to dely for a while but this was not a good solution because it delays the camera preview

我试图尽可能地减小帧大小,因此我使用mOpenCvCameraView.setMaxFrameSize(320,240)在代码部分中提到了belwo"将其设置为320x240.但是此解决方案设法使摄像机的预览时间延长了几分钟,但最终该应用程序也崩溃了.

i tried to minimize the frame size as much as i can, so i set it to 320x240 using mOpenCvCameraView.setMaxFrameSize(320, 240) "mentioned belwo in the code section". But this solution managed to keep the camera preview longer for few minutes but in the end The App crashed also.

请告诉我这种情况的正确解决方案是什么,如何避免呢?

Please tell me what is the proper solution for such situation and how to avoid it?

Logcat :

10-07 14:42:43.445 30510-31656/com.example.bak.opencvcamera_00 E/cv::error(): OpenCV Error: Insufficient memory (Failed to allocate 307200 bytes) in void* cv::OutOfMemoryError(size_t), file /home/maksim/workspace/android-pack/opencv/modules/core/src/alloc.cpp, line 52
10-07 14:42:43.445 30510-31656/com.example.bak.opencvcamera_00 E/cv::error(): OpenCV Error: Assertion failed (u != 0) in void cv::Mat::create(int, const int*, int), file /home/maksim/workspace/android-pack/opencv/modules/core/src/matrix.cpp, line 411
10-07 14:42:43.450 30510-31656/com.example.bak.opencvcamera_00 E/org.opencv.core.Mat: Mat::n_1t() caught cv::Exception: /home/maksim/workspace/android-pack/opencv/modules/core/src/matrix.cpp:411: error: (-215) u != 0 in function void cv::Mat::create(int, const int*, int)
10-07 14:42:43.450 30510-31656/com.example.bak.opencvcamera_00 E/AndroidRuntime: FATAL EXCEPTION: Thread-9334
Process: com.example.bak.opencvcamera_00, PID: 30510
CvException [org.opencv.core.CvException: cv::Exception: /home/maksim/workspace/android-pack/opencv/modules/core/src/matrix.cpp:411: error: (-215) u != 0 in function void cv::Mat::create(int, const int*, int)
at org.opencv.core.Mat.n_t(Native Method)
at org.opencv.core.Mat.t(Mat.java:852)
at com.example.bak.opencvcamera_00.MainActivity.onCameraFrame(MainActivity.java:109)
at org.opencv.android.CameraBridgeViewBase.deliverAndDrawFrame(CameraBridgeViewBase.java:391)
at org.opencv.android.JavaCameraView$CameraWorker.run(JavaCameraView.java:350)

代码:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

    setContentView(R.layout.activity_main);
    mOpenCvCameraView = (JavaCameraView) findViewById(R.id.surfaceView);
    mOpenCvCameraView.setOnTouchListener(this);
    mOpenCvCameraView.setVisibility(SurfaceView.VISIBLE);
    mOpenCvCameraView.setCvCameraViewListener(this);
    mOpenCvCameraView.setMaxFrameSize(320, 240);
}
...
...
...
@Override
public Mat onCameraFrame(Mat inputFrame) {
    mRgbaT = inputFrame.t();
    Core.flip(inputFrame.t(), mRgbaT, 1);
    Imgproc.resize(mRgbaT, mRgbaT, inputFrame.size());
    return mRgbaT;
}

更新:

我修改了以下方法,将mRgbaT声明为字段,并在捕获新帧后清除了其内容...但问题仍然存在

I modified the below method in such a way that mRgbaT is declared as a field and i clear its contents after capturing a new frame...but still the problem persists

@Override
public Mat onCameraFrame(Mat inputFrame) {

    if (mRgbaT != null) {
        mRgbaT.release();
    }

    mRgbaT = inputFrame.t();
    Core.flip(inputFrame.t(), mRgbaT, 1);
    Imgproc.resize(mRgbaT, mRgbaT, inputFrame.size());
    return mRgbaT;
}

推荐答案

我通过避免两次执行矩阵转置操作来解决它,如下代码所示:

I solved it by avoiding performing matrix transpose operation twice as shown below in the code:

@Override
public Mat onCameraFrame(Mat inputFrame) {
    if (mRgbaT != null) {
        mRgbaT.release();
    }

    mRgbaT = inputFrame.t();
    Core.flip(mRgbaT, mRgbaT, 1);
    Imgproc.resize(mRgbaT, mRgbaT, inputFrame.size());
    return mRgbaT;
}

这篇关于由于相机预览而产生OutOfMemoryError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆