Android:Camera,onSurfaceTextureUpdated,Bitmap.getPixels– 帧率从30降至3

概述在尝试获取相机预览的像素时,我的表现非常糟糕.图像格式约为600×900.我的HTC上的预览率非常稳定30fps.一旦我尝试获取图像的像素,帧速率就会降到5以下!publicvoidonSurfaceTextureUpdated(SurfaceTexturesurfaceTexture){Bitmapbmp=mTextureView.getBitmap();

在尝试获取相机预览的像素时,我的表现非常糟糕.

图像格式约为600×900.
我的HTC上的预览率非常稳定30fps.

一旦我尝试获取图像的像素,帧速率就会降到5以下!

public voID onSurfaceTextureUpdated(SurfaceTexture surfaceTexture) {    Bitmap bmp = mTextureVIEw.getBitmap();    int wIDth = bmp.getWIDth();    int height = bmp.getHeight();    int[] pixels = new int[bmp.getHeight() * bmp.getWIDth()];    bmp.getPixels(pixels, 0, wIDth, 0, 0, wIDth, height);}

性能如此之慢,实际上并不可忍受.

现在我唯一的’简单’解决方案是跳帧,至少保持一些视觉表现.
但我真的希望让代码执行得更快.

我很感激任何想法和建议,也许有人已经解决了这个问题?

UPDATE

getbitmap: 188.341msarray: 122ms getPixels: 12.330msrecycle: 152ms

获取位图需要190毫秒!!那就是问题所在

解决方法:

我挖了几个小时.

如此简短的回答:我发现无法避免使用getBitmap()并提高性能.
已知这个函数很慢,我发现了很多类似的问题但没有结果.

然而,我找到了另一种解决方案,速度提高了约3倍,并为我解决了问题.
我继续使用TextureVIEw方法,我使用它,因为它为如何显示相机预览提供了更多的自由(例如,我可以在我自己的宽高比的小窗口中显示相机实时预览而不会失真)

但是为了处理图像数据,我不再使用onSurefaceTextureUpdated()了.

我注册了cameraPrevIEwFrame的回调函数,它给出了我需要的像素数据.
所以没有getBitmap,而且速度更快.

快速,新的代码:

myCamera.setPrevIEwCallback(prevIEw);Camera.PrevIEwCallback prevIEw = new Camera.PrevIEwCallback(){    public voID onPrevIEwFrame(byte[] data, Camera camera)    {        Camera.Parameters parameters = camera.getParameters();        Camera.Size size = parameters.getPrevIEwSize();        Image img = new Image(size.wIDth, size.height, "Y800");    }};

慢:

private int[] surface_pixels=null;private int surface_wIDth=0;private int surface_height=0;@OverrIDepublic voID onSurfaceTextureUpdated(SurfaceTexture surfaceTexture){    int wIDth,height;    Bitmap bmp= mTextureVIEw.getBitmap();    height=barcodeBmp.getHeight();    wIDth=barcodeBmp.getWIDth();    if (surface_pixels == null)    {        surface_pixels = new int[height * wIDth];    } else    {        if ((wIDth != surface_wIDth) || (height != surface_height))        {            surface_pixels = null;            surface_pixels = new int[height * wIDth];        }    }    if ((wIDth != surface_wIDth) || (height != surface_height))    {        surface_height = barcodeBmp.getHeight();        surface_wIDth = barcodeBmp.getWIDth();    }    bmp.getPixels(surface_pixels, 0, wIDth, 0, 0, wIDth, height);    bmp.recycle();    Image img = new Image(wIDth, height, "RGB4"); }

我希望这可以帮助一些有同样问题的人.

如果有人应该在onSurfaceTextureUpdated中找到一种快速创建位图的方法,请使用代码示例进行响应.

总结

以上是内存溢出为你收集整理的Android:Camera,onSurfaceTextureUpdated,Bitmap.getPixels – 帧率从30降至3全部内容,希望文章能够帮你解决Android:Camera,onSurfaceTextureUpdated,Bitmap.getPixels – 帧率从30降至3所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1119084.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-29
下一篇2022-05-29

发表评论

登录后才能评论

评论列表(0条)

    保存