gpt4 book ai didi

android - 由于相机预览而产生 OutOfMemoryError

转载 作者:搜寻专家 更新时间:2023-11-01 09:40:51 26 4
gpt4 key购买 nike

我成功地使用 opencv 打开了 Android 摄像头。但是当我使用修复相机方向的代码时 - “请参阅下面在 onCameraFrame(..) 方法中提到的代码” - 应用程序在几秒钟后崩溃并且 logcat在“logcat 部分”生成 belwo 发布的消息。

解决这个问题:

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

  2. 我尝试尽可能地减少帧大小,所以我使用 mOpenCvCameraView.setMaxFrameSize(320, 240) 将其设置为 320x240,“在代码部分提到了 belwo”。但是这个解决方案设法让相机预览时间更长了几分钟,但最终应用程序也崩溃了。

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

逻辑猫:

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;
}

最佳答案

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

@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;
}

关于android - 由于相机预览而产生 OutOfMemoryError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39918063/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com