使用相机预览拍摄 SurfaceView 的屏幕截图 [英] Taking a ScreenShot of SurfaceView with Camera Preview in it

查看:22
本文介绍了使用相机预览拍摄 SurfaceView 的屏幕截图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实现包括在录制视频时拍照的功能.这就是我得出结论使用 SurfaceView 的 Screenshot 方法的原因.但是,当我尝试拍摄 SurfaceView 的屏幕截图时.我总是得到一个空白图像.

I am trying to Implement a Functionality that includes taking Pictures While Recording Video. That's the reason i have concluded to use the Screenshot approach of SurfaceView.
However, when i try to take the Screen Shot of SurfaceView. I am always getting a Blank Image.

这是我用来拍摄快照的代码:

Here is the code that i am using for taking a Snapshot:

View tempView = (View)MY_SURFACE_VIEW;
tempView.setDrawingCacheEnabled(true);
Bitmap tempBmp = Bitmap.createBitmap(tempView.getDrawingCache());
tempView.setDrawingCacheEnabled(false);
//Saving this Bitmap to a File....

如果你们可能认为这是一个重复的问题,让我向你们保证,在问这个问题之前,我已经尝试了在 SO 上针对同一问题提供的以下解决方案.

In case you guys may think this is a duplicate Question, let me assure you that i have tried the following Solutions provided on SO for the same Problem before asking this one.

  1. https://stackoverflow.com/questions/24134964/issue-with-camera-picture-taken-snapshot-using-surfaceview-in-android
  2. 在录制视频时遇到截屏问题视频
  3. 录制时拍摄相机截图 - 就像在 Galaxy S3 中一样?
  4. 在 android 中拍摄 SurfaceView 的屏幕截图
  5. 获取Android中surfaceView的截图(这是正确答案,但部分回答.我已经要求@sajar 解释答案)
  1. https://stackoverflow.com/questions/24134964/issue-with-camera-picture-taken-snapshot-using-surfaceview-in-android
  2. Facing issue to take a screenshot while recording a video
  3. Take camera screenshot while recording - Like in Galaxy S3?
  4. Taking screen shot of a SurfaceView in android
  5. Get screenshot of surfaceView in Android (This is the correct answer, but Partially Answered. I have already asked @sajar to Explain the Answer)

互联网上的其他资源:
1. http://www.coderanch.com/t/622613/Android/Mobile/capture-screenshot-simple-animation-project2. http://www.phonesdevelopers.com/1795894/

Other Resources on Internet:
1. http://www.coderanch.com/t/622613/Android/Mobile/capture-screenshot-simple-animation-project 2. http://www.phonesdevelopers.com/1795894/

到目前为止,这些对我来说都没有奏效.我也知道我们需要创建一些与 Surface Holder 交互并从中获取位图的线程.但我不确定如何实施.

None of this has worked so far for me. I also know that we need to create some Thread that interacts with the Surface Holder and Gets the Bitmap from It. But i am not sure how to implement that.

非常感谢任何帮助.

推荐答案

这是另一个:截屏SurfaceView.

SurfaceViews 有一个表面"部分和一个视图"部分;您的代码尝试捕获视图"部分.表面"部分是一个单独的层,没有简单的抓取所有像素"方法.基本的困难在于您的应用程序位于表面的生产者"一侧,而不是消费者"一侧,因此读取像素是有问题的.请注意,底层缓冲区采用对数据生产者最方便的任何格式,因此对于相机预览,它将是 YUV 缓冲区.

SurfaceViews have a "surface" part and a "view" part; your code tries to capture the "view" part. The "surface" part is a separate layer, and there's no trivial "grab all pixels" method. The basic difficulty is that your app is on the "producer" side of the surface, rather than the "consumer" side, so reading pixels back out is problematic. Note that the underlying buffers are in whatever format is most convenient for the data producer, so for camera preview it'll be a YUV buffer.

捕获"表面像素的最简单和最有效的方法是将它们绘制两次,一次用于屏幕,一次用于捕获.如果您使用 OpenGL ES 执行此操作,则 YUV 到 RGB 的转换可能会由硬件模块完成,这比在 YUV 缓冲区中接收相机帧并进行您自己的转换要快得多.

The easiest and most efficient way to "capture" the surface pixels is to draw them twice, once for the screen and once for capture. If you do this with OpenGL ES, the YUV to RGB conversion will likely be done by a hardware module, which will be much faster than receiving camera frames in YUV buffers and doing your own conversion.

Grafika 的来自相机的纹理"活动演示了使用 GLES 处理传入的视频数据.渲染后,您可以使用 glReadPixels() 获取像素.glReadPixels() 的性能可能因设备和不同用例而异.EglSurfaceBase#saveFrame() 展示了如何捕捉到 Bitmap 并保存为 PNG.

Grafika's "texture from camera" activity demonstrates manipulation of incoming video data with GLES. After rendering you can get the pixels with glReadPixels(). The performance of glReadPixels() can vary significantly between devices and different use cases. EglSurfaceBase#saveFrame() shows how to capture to a Bitmap and save as PNG.

有关 Android 图形架构的更多信息,特别是 SurfaceView 表面的生产者-消费者性质,可以在 本文档.

More information about the Android graphics architecture, notably the producer-consumer nature of SurfaceView surfaces, can be found in this document.

这篇关于使用相机预览拍摄 SurfaceView 的屏幕截图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆