gpt4 book ai didi

java - lockCanvas 不断抛出异常

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:53:29 30 4
gpt4 key购买 nike

我从 opencv4android(人脸检测)复制了示例代码。我通过了NDK部分。并在手机上运行该应用程序。但是 SurfaceView 仅显示相机预览。似乎在 OnCameraFrame 中所做的任何事情都不会影响 SurfaceView 上的 Canvas 。所以我追踪到他们的SDK。问题出在 deliverAndDrawFrame 函数中。以前有人遇到过同样的问题吗?

Canvas canvas = getHolder().lockCanvas();
if (canvas != null) {
canvas.drawColor(0, android.graphics.PorterDuff.Mode.CLEAR);
Log.d(TAG, "mStretch value: " + mScale);

if (mScale != 0) {
canvas.drawBitmap(mCacheBitmap, new Rect(0,0,mCacheBitmap.getWidth(), mCacheBitmap.getHeight()),
new Rect((int)((canvas.getWidth() - mScale*mCacheBitmap.getWidth()) / 2),
(int)((canvas.getHeight() - mScale*mCacheBitmap.getHeight()) / 2),
(int)((canvas.getWidth() - mScale*mCacheBitmap.getWidth()) / 2 + mScale*mCacheBitmap.getWidth()),
(int)((canvas.getHeight() - mScale*mCacheBitmap.getHeight()) / 2 + mScale*mCacheBitmap.getHeight())), null);
} else {
canvas.drawBitmap(mCacheBitmap, new Rect(0,0,mCacheBitmap.getWidth(), mCacheBitmap.getHeight()),
new Rect((canvas.getWidth() - mCacheBitmap.getWidth()) / 2,
(canvas.getHeight() - mCacheBitmap.getHeight()) / 2,
(canvas.getWidth() - mCacheBitmap.getWidth()) / 2 + mCacheBitmap.getWidth(),
(canvas.getHeight() - mCacheBitmap.getHeight()) / 2 + mCacheBitmap.getHeight()), null);
}

if (mFpsMeter != null) {
mFpsMeter.measure();
mFpsMeter.draw(canvas, 20, 30);
}
getHolder().unlockCanvasAndPost(canvas);
}

错误:

10-09 21:57:47.485  28018-28088/com.example.tim.r3dmobileclient E/SurfaceHolder﹕ Exception locking surface
java.lang.IllegalArgumentException
at android.view.Surface.nativeLockCanvas(Native Method)
at android.view.Surface.lockCanvas(Surface.java:253)
at android.view.SurfaceView$4.internalLockCanvas(SurfaceView.java:848)
at android.view.SurfaceView$4.lockCanvas(SurfaceView.java:816)
at org.opencv.android.CameraBridgeViewBase.deliverAndDrawFrame(CameraBridgeViewBase.java:412)
at org.opencv.android.JavaCameraView$CameraWorker.run(JavaCameraView.java:348)
at java.lang.Thread.run(Thread.java:841)

最佳答案

在尝试锁定 Canvas 之前,您是否尝试过检查表面是否有效?如果所有这些代码都在循环中,请尝试在锁定 Canvas 之前将其放入:

if(!getHolder().getSurface().isValid()){
continue;
}

如果它不在循环内,请尝试将所有代码(从锁定 Canvas 及下方)放入此 if 语句中:

if(getHolder().getSurface().isValid()){
//code goes here
}

关于java - lockCanvas 不断抛出异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33040448/

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