English 中文(简体)
Android SDK:获取原始预览相机图像而不显示
原标题:Android SDK: Get raw preview camera image without displaying it
  • 时间:2012-05-27 17:33:53
  •  标签:
  • android

我想用原始图像进行图像处理,而不在屏幕上显示,这显然会降低性能。

根据此话题的答案在没有预览的情况下从相机拍摄照片这在Android 1.5中是不可能的,但有人知道这在Android 4(API 15级)中是否可能吗?

最佳回答

在Android 4中,接收原始图像数据而不在屏幕上显示的最简单方法是使用Camera.setPreviewTexture()调用将预览帧路由到GPU。

您可以通过两种方式使用它:

  1. Do your actual processing on the GPU: Set up an OpenGL context (OpenGL ES 2 tutorial), and create a SurfaceTexture object in that context. Then pass that object to setPreviewTexture, and start preview. Then, in your OpenGL code, you can call SurfaceTexture.updateTexImage, and the texture ID associated with the SurfaceTexture will be updated to the latest preview frame from the camera. You can also read back the RGB texture data to the CPU for further processing using glReadPixels, if desired.
  2. Do your processing on the CPU: You can simply create a dummy SurfaceTexture object without any OpenGL context set up. Pass any integer you want as the texture ID, and connect the SurfaceTexture to the camera using setPreviewTexture. As long as you don t call updateTexImage, the SurfaceTexture will simply discard all data passed into it by the camera. Then set up preview callbacks using setPreviewCallback, and use that data (typically in a YUV format) for CPU processing. This is probably less efficient than #1, but does not require knowing OpenGL.
问题回答

因为我不允许发表评论。关于Eddy的回答。您需要在NDK中使用它,因为使用Java接口会抵消任何性能优势。从性能的角度来看,必须使用PixelBuffer是绝对疯狂的。从RGBA888到YUV的转换也需要在C中完成。

不要尝试使用TextureView,否则会更糟。在转换为YUV之前,您必须将像素复制到位图中,然后从位图复制到数组中。这本身就占据了全新Nexus 7 2013上近30%的cpu利用率。

最有效的方法是直接与Camera.h对话,绕过所有Android API。您可以创建自己的缓冲区,并在YUV数据传输到其他任何地方之前拦截它。

在屏幕上显示预览不会对性能产生影响。在我遇到的所有设备上,相机输出都“连接”到表面或纹理,不涉及CPU,所有颜色转换和缩放都由专用硬件处理。

“隐藏”预览可能还有其他原因,但请记住,出于隐私和安全原因,API最初的目的是确保最终用户看到从相机到应用程序的任何内容。





相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签