onPreviewFrame 返回倾斜的 YUV 数据

发布于 2024-11-29 22:32:58 字数 1758 浏览 0 评论 0原文

好吧,我已经无计可施了。我正在编写一个机器人应用程序,它接收预览数据,处理它,然后将更改后的数据显示到屏幕上。到目前为止一切都很好——除了这样一个事实:在一段不确定的时间(通常是一两分钟)之后,在 onPreviewFrame 回调期间传入的 YUV 数据会......滚动......水平。我将原始 yuv 捕获到 SD 卡:

[看起来我还不能在这里发布图片,所以这里是 url:raw yuv capture]

这是我的第一个 droid 应用程序,但我花了很多时间在各种桌面视频软件上工作。当我看到这个时,我立即想到的是我遇到了线程同步问题,并且缓冲区因此以某种奇怪的方式被覆盖。

我的代码最初确实使用了工作线程。因此,为了消除问题与线程相关的可能性,我取出工作线程并将所有内容放入 onPreviewFrame 中。同样的问题又来了:YUV 数据倾斜。

然后我做了一些谷歌搜索,发现 onPreviewFrame 内部的繁重处理可能会导致随机的奇怪现象。为了避免这个问题,我将回调更改为 OneShot 品种,这导致了我现在使用的代码:

public void surfaceCreated(SurfaceHolder holder) {
    Log.i(TAG, "surfaceCreated");
    mCamera = Camera.open();
    mCamera.setOneShotPreviewCallback(new PreviewCallback() {

        public void onPreviewFrame(byte[] data, Camera camera) {

            Canvas canvas = mHolder.lockCanvas();
            try
            {             
                Bitmap bmp = processFrame(data);
                if (bmp != null) {

                    if (canvas != null) {
                        canvas.drawBitmap(bmp, (canvas.getWidth() - getFrameWidth()) / 2, (canvas.getHeight() - getFrameHeight()) / 2, null);                           
                    }
                    bmp.recycle();
                }
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
            finally
            {
                mHolder.unlockCanvasAndPost(canvas);
                mCamera.setOneShotPreviewCallback(this);
            }
        }
    });
}

不幸的是,我仍然得到了倾斜的 yuv 数据。

我的测试设备是 HTC EVO 4G。我知道这款手机存在问题,但我仍谨慎地将其归因于硬件错误,主要是因为我几乎可以肯定这是由于程序员错误造成的。问题是,我已经将代码精简到了骨干,但问题仍然存在。

以前有人遇到过这种情况吗?

All right, I'm at my wit's end. I'm writing a droid app that receives preview data, processes it, and then displays the altered data to the screen. So far so good -- except for the fact that after some indeterminate amount of time (usually a minute or two) the YUV data that comes in during the onPreviewFrame callback is...rolled...horizontally. I captured the raw yuv to the sd card:

[looks like I can't post pictures here yet, so here's the url: raw yuv capture]

This is my first droid app, but I've spent a lot of time working on various desktop video software. The thing that immediately came to mind when I saw this was that I had a thread synchronization problem, and the buffer was getting overwritten in some strange way because of it.

And my code did use a worker thread initially. So to eliminate this possibility that the issue was thread-releated, I took the worker thread out and put everything inside of onPreviewFrame. Again, the same problem: skewed YUV data.

Then I did some googling and discovered that heavy processing inside of onPreviewFrame could result in random weirdness. To circumvent that, I changed my callback to the OneShot variety, which resulted in the code I'm using now:

public void surfaceCreated(SurfaceHolder holder) {
    Log.i(TAG, "surfaceCreated");
    mCamera = Camera.open();
    mCamera.setOneShotPreviewCallback(new PreviewCallback() {

        public void onPreviewFrame(byte[] data, Camera camera) {

            Canvas canvas = mHolder.lockCanvas();
            try
            {             
                Bitmap bmp = processFrame(data);
                if (bmp != null) {

                    if (canvas != null) {
                        canvas.drawBitmap(bmp, (canvas.getWidth() - getFrameWidth()) / 2, (canvas.getHeight() - getFrameHeight()) / 2, null);                           
                    }
                    bmp.recycle();
                }
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
            finally
            {
                mHolder.unlockCanvasAndPost(canvas);
                mCamera.setOneShotPreviewCallback(this);
            }
        }
    });
}

Unfortunately, I still get the skewed yuv data.

My testing device is an HTC EVO 4G. I know this phone has its issues, but I'm wary to chalk this up to a hardware bug just yet, mainly because I'm almost certain it's due to programmer error. Problem is, I've pared my code down to the bone, and the problem is still manifesting.

Has anybody encountered this before?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北音执念 2024-12-06 22:32:58

嗯,看起来这是 HTC 手机的问题。 正是问题所在我有。 在这里他们说这是由后置摄像头镜头松动,解决方案是用拇指用力将其推入几秒钟以重新安装。

毕竟不是软件问题。

Well, it looks like it's an issue with HTC phones. This is the exact problem I'm having. Over here they say it's caused by the rear camera lens coming loose and the solution is to push it in hard with your thumb for a few seconds to reseat it.

Not a software problem after all.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文