我设法使用opencv打开了Android相机。但是当我使用修复相机方向的代码时 - “请参阅下面 onCameraFrame(..) 方法中提到的代码” - 应用程序在几秒钟后崩溃,并且 logcat 在“logcat 部分”中生成 belwo 发布的消息。
要解决此问题:
我尝试使用 SystemClock.sleep 强制应用延迟一段时间,但这不是一个好的解决方案,因为它会延迟相机预览
我试图尽可能地最小化帧大小,所以我使用 mOpenCvCameraView.setMaxFrameSize(320, 240) 将其设置为 320x240“在代码部分中提到”。但是这个解决方案设法让相机预览时间更长几分钟,但最终应用程序也崩溃了。
请告诉我这种情况的正确解决方案是什么以及如何避免它?
日志猫:
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 声明为一个字段,并在捕获新帧后清除其内容......但问题仍然存在
@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;
}