和你一起终身学习,这里是程序员Android
经典好文推荐,通过阅读本文,您将收获以下知识点:
一、 SurfaceView 、Surface 、 SurfaceHolder
二、Camera 概览
三、总结
前言
上一篇文章介绍了如何调用系统相机进行拍照裁剪等功能,一般情况下这些已经能满足我们的需求了。但是在有些场景和特殊需求下,比如要进行人脸检测、要不间断地抓取多张照片等等,那就需要使用原生Camera来进行开发啦
这里并不打算讲如何用代码去实现,而是先给小伙们介绍相关的知识点,等对这些知识有了大致了解后在动手去写,这样既能有目的的去写又能加深对知识点的理解
本篇文章主要给大家讲解进行Camera开发需要用到的类和方法,以及在开发过程中遇到的方向问题的分析
进行Camra开发主要用到了以下两个类:
Camera
SurfaceView (当然也可以是TextureView,本文我们使用SurfaceView)
这两者的关系如下图:
image
一、 SurfaceView 、Surface 、 SurfaceHolder
关系图
image
Surface
什么是Surface?源码中是这样描述的:
* Handle onto a raw buffer that is being managed by the screen compositor.
*
* <p>A Surface is generally created by or from a consumer of image buffers (such as a
* {@link android.graphics.SurfaceTexture}, {@link android.media.MediaRecorder}, or
* {@link android.renderscript.Allocation}), and is handed to some kind of producer (such as
* {@link android.opengl.EGL14#eglCreateWindowSurface(android.opengl.EGLDisplay,android.opengl.EGLConfig,java.lang.Object,int[],int) OpenGL},
* {@link android.media.MediaPlayer#setSurface MediaPlayer}, or
* {@link android.hardware.camera2.CameraDevice#createCaptureSession CameraDevice}) to draw
* into.</p>
简单翻译一下,大概意思就是:
Surfaces是用来处理屏幕显示内容合成器所管理的原始缓存区的工具。它通常由图像缓冲区的消费者来创建(如:SurfaceTexture,MediaRecorder),然后被移交给生产者(如:MediaPlayer)或者是显示到其上(如:CameraDevice)
SurfaceHolder
源码描述:
* Abstract interface to someone holding a display surface. Allows you to
* control the surface size and format, edit the pixels in the surface, and
* monitor changes to the surface. This interface is typically available
* through the {@link SurfaceView} class.
简单翻译一下:
一个抽象接口,给持有surface的对象使用。它可以控制surface的大小和格式,编辑surface中的像素,以及监听surface的变化。这个接口通常通过SurfaceView类获得
SurfaceHolder中有一个Callbcak接口,它有3个回调方法
surfaceCreated(SurfaceHo