Textureview/Surface view for Camera preview - Android

佐手、 提交于 2021-02-11 05:12:41

问题


I am working on developing an Android applications that uses camera. To display the camera preview, I observed a lot of examples using surface view or texture view. Why are these preferred over using ImageView to display the camera preview? I have used Imageview to display the preview and it works fine.

Thanks.


回答1:


Please do not use ImageView for this. It is extremely inefficient, and I'd be surprised if you are able to produce high-resolution preview (1080p) with this approach.

SurfaceView and TextureView both directly push zero-copy buffers from the camera drivers to the hardware composer or the GPU, respectively. There are no extra buffer copies or transformations, except whatever final conversions need to be done at the destination. This reduces memory bandwidth and CPU overhead substantially.

Creating Bitmaps is slow, CPU-hungry, and makes several copies of each frame, wasting memory as well.




回答2:


Why are these preferred over using ImageView to display the camera preview?

Efficiency, among perhaps other reasons.

With both SurfaceView and TextureView, the underlying camera engine can work more directly with the output surface via the GPU and the OpenGL engine, bypassing the Dalvik/ART runtime. In your case, you are forcing the camera engine to fill in a Java byte[], then you are decoding it into a Bitmap, then updating the UI via your ImageView. That's a lot of extra CPU processing and memory fussing than is needed if you use a surface.



来源:https://stackoverflow.com/questions/30536564/textureview-surface-view-for-camera-preview-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!