中国科学技术大学电子工程与信息科学系©2018 实验十七Android下的摄像头编程 实验目的 (l)学习Android下进行摄像头控制的基本方法。 (2)理解回调函数。 二、 实验原理 l、android.hardware android.hardware包下面包含了如摄像头、传感器等硬件的支持(不是所有 的硬件)。如表1所示。 表1 android.hardware中类[1] 类 类的功能 Camera The Camera class is used to set image capture settings,start/stop preview,snap pictures,and retrieve frames for encoding for video. Camera.Area The Area class is used for choosing specific metering and focus areas for the camera to use when calculating auto-exposure,auto- white balance,and auto-focus Camera.Cameralnfo Information about a camera Camera.Face Information about a face identified through camera face detection. Camera.Parameters Camera service settings. Camera.Size Image size(width and height dimensions). ConsumerIrManager Class that operates consumer infrared on the device. ConsumerIrManager.Carrier Represents a range of carrier frequencies (inclusive)on which the FrequencyRange infrared transmitter can transmit GeomagneticField Estimates magnetic field at a given point on Earth,and in particular, to compute the magnetic declination from true north. Sensor Class representing a sensor. SensorEvent This class represents a Sensor event and holds informations such as the sensor's type,the time-stamp,accuracy and of course the sensor's data. SensorManager SensorManager lets you access the device's sensors. TriggerEvent This class represents a Trigger Event-the event associated with a Trigger Sensor. TriggerEventListener This class is the listener used to handle Trigger Sensors 2、android.hardware..Camera类 现在的手机一般都会提供相机功能,有些相机的镜头甚至支持1000万以上 像素,有些甚至支持光学变焦,这些手机已经变成了专业数码相机。为了充分利 用手机上的相机功能,Android APIs提供了丰富的支持库。如表2所示。 使用Android设备的摄像头(照相机)需要设备访问权限如下。 如果您在阅读过程中发现疏漏和错误,请联系《多媒体技术基础》课程组{network,.cxh}@ustc.edu.cn
中国科学技术大学电子工程与信息科学系© 2018 如果您在阅读过程中发现疏漏和错误,请联系《多媒体技术基础》课程组{network,cxh}@ustc.edu.cn 实验十七 Android 下的摄像头编程 一、 实验目的 (1) 学习 Android 下进行摄像头控制的基本方法。 (2) 理解回调函数。 二、 实验原理 1、android.hardware android.hardware 包下面包含了如摄像头、传感器等硬件的支持(不是所有 的硬件)。如表 1 所示。 表 1 android.hardware 中类[1] 类 类的功能 Camera The Camera class is used to set image capture settings, start/stop preview, snap pictures, and retrieve frames for encoding for video. Camera.Area The Area class is used for choosing specific metering and focus areas for the camera to use when calculating auto-exposure, autowhite balance, and auto-focus. Camera.CameraInfo Information about a camera Camera.Face Information about a face identified through camera face detection. Camera.Parameters Camera service settings. Camera.Size Image size (width and height dimensions). ConsumerIrManager Class that operates consumer infrared on the device. ConsumerIrManager.Carrier FrequencyRange Represents a range of carrier frequencies (inclusive) on which the infrared transmitter can transmit GeomagneticField Estimates magnetic field at a given point on Earth, and in particular, to compute the magnetic declination from true north. Sensor Class representing a sensor. SensorEvent This class represents a Sensor event and holds informations such as the sensor's type, the time-stamp, accuracy and of course the sensor's data. SensorManager SensorManager lets you access the device's sensors. TriggerEvent This class represents a Trigger Event - the event associated with a Trigger Sensor. TriggerEventListener This class is the listener used to handle Trigger Sensors. 2、android.hardware.Camera 类 现在的手机一般都会提供相机功能,有些相机的镜头甚至支持 1000 万以上 像素,有些甚至支持光学变焦,这些手机已经变成了专业数码相机。为了充分利 用手机上的相机功能,Android APIs 提供了丰富的支持库。如表 2 所示。 使用 Android 设备的摄像头(照相机)需要设备访问权限如下
中国科学技术大学电子工程与信息科学系©2018 表2 android.hardware..Camera的成员函数 成员函数名称 final void addCallbackBuffer(byte[]callbackBuffer) Adds a pre-allocated buffer to the preview callback buffer queue. final void autoFocus(Camera.AutoFocusCallback cb) Starts camera auto-focus and registers a callback function to run when the camera is focused. final void cancelAutoFocus() Cancels any auto-focus function in progress. final boolean enableShutterSound(boolean enabled) Enable or disable the default shutter sound when taking a picture static void getCameralnfo(int camerald,Camera.Cameralnfo cameralnfo) Returns the information about a particular camera. static int getNumberOfCameras() Returns the number of physical cameras available on this device. Camera.Parameters getParameters() Returns the current settings for this Camera service. final void lockO Re-locks the camera to prevent other processes from accessing it. static Camera open(int camerald) Creates a new Camera object to access a particular hardware camera. static Camera open() Creates a new Camera object to access the first back-facing camera on the device. final void reconnect() Reconnects to the camera service after another process used it. final void release() Disconnects and releases the Camera object resources. void setAutoFocusMoveCallback(Camera.AutoFocusMoveCallback cb) Sets camera auto-focus move callback. final void setDisplayOrientation(int degrees) Set the clockwise rotation of preview display in degrees. final void setErrorCallback(Camera.ErrorCallback cb) Registers a callback to be invoked when an error occurs. final void setFaceDetectionListener(Camera.FaceDetectionListener listener) Registers a listener to be notified about the faces detected in the preview frame. final void setOneShotPreviewCallback(Camera.PreviewCallback cb) Installs a callback to be invoked for the next preview frame in addition to displaying it on the screen. void setParameters(Camera.Parameters params) Changes the settings for this Camera service. final void setPreviewCallback(Camera.PreviewCallback cb) Installs a callback to be invoked for every preview frame in addition to displaying them on the screen. final void setPreviewCallback WithBuffer(Camera.PreviewCallback cb) Installs a callback to be invoked for every preview frame,using buffers supplied with addCallbackBuffer(byte[]),in addition to displaying them on the screen. final void setPreviewDisplay(SurfaceHolder holder) Sets the Surface to be used for live preview. 如果您在阅读过程中发现疏漏和错误,请联系《多媒体技术基础》课程组{network,cxh}@ustc.edu.cn
中国科学技术大学电子工程与信息科学系© 2018 如果您在阅读过程中发现疏漏和错误,请联系《多媒体技术基础》课程组{network,cxh}@ustc.edu.cn 表 2 android.hardware.Camera 的成员函数 成员函数名称 final void addCallbackBuffer(byte[] callbackBuffer) Adds a pre-allocated buffer to the preview callback buffer queue. final void autoFocus(Camera.AutoFocusCallback cb) Starts camera auto-focus and registers a callback function to run when the camera is focused. final void cancelAutoFocus() Cancels any auto-focus function in progress. final boolean enableShutterSound(boolean enabled) Enable or disable the default shutter sound when taking a picture. static void getCameraInfo(int cameraId, Camera.CameraInfo cameraInfo) Returns the information about a particular camera. static int getNumberOfCameras() Returns the number of physical cameras available on this device. Camera.Parameters getParameters() Returns the current settings for this Camera service. final void lock() Re-locks the camera to prevent other processes from accessing it. static Camera open(int cameraId) Creates a new Camera object to access a particular hardware camera. static Camera open() Creates a new Camera object to access the first back-facing camera on the device. final void reconnect() Reconnects to the camera service after another process used it. final void release() Disconnects and releases the Camera object resources. void setAutoFocusMoveCallback(Camera.AutoFocusMoveCallback cb) Sets camera auto-focus move callback. final void setDisplayOrientation(int degrees) Set the clockwise rotation of preview display in degrees. final void setErrorCallback(Camera.ErrorCallback cb) Registers a callback to be invoked when an error occurs. final void setFaceDetectionListener(Camera.FaceDetectionListener listener) Registers a listener to be notified about the faces detected in the preview frame. final void setOneShotPreviewCallback(Camera.PreviewCallback cb) Installs a callback to be invoked for the next preview frame in addition to displaying it on the screen. void setParameters(Camera.Parameters params) Changes the settings for this Camera service. final void setPreviewCallback(Camera.PreviewCallback cb) Installs a callback to be invoked for every preview frame in addition to displaying them on the screen. final void setPreviewCallbackWithBuffer(Camera.PreviewCallback cb) Installs a callback to be invoked for every preview frame, using buffers supplied with addCallbackBuffer(byte[]), in addition to displaying them on the screen. final void setPreviewDisplay(SurfaceHolder holder) Sets the Surface to be used for live preview
中国科学技术大学电子工程与信息科学系©2018 final void setPreviewTexture(SurfaceTexture surfaceTexture) Sets the SurfaceTexture to be used for live preview. final void setZoomChangeListener(Camera.OnZoomChangeListener listener) Registers a listener to be notified when the zoom value is updated by the camera driver during smooth zoom. final void startFaceDetection() Starts the face detection. final void startPreview() Starts capturing and drawing preview frames to the screen. final void startSmoothZoom(int value) Zooms to the requested value smoothly. final void stopFaceDetection() Stops the face detection final void stopPreview() Stops capturing and drawing preview frames to the surface,and resets the camera for a future call to startPreview() final void stopSmoothZoom() Stops the smooth zoom. final void takePicture(Camera.ShutterCallback shutter,Camera.PictureCallback raw,Camera.PictureCallback jpeg) Equivalent to takePicture(shutter,raw,null,jpeg). final void takePicture(Camera.ShutterCallback shutter,Camera.PictureCallback raw,Camera.PictureCallback postview,Camera.PictureCallback jpeg) Triggers an asynchronous image capture. final void unlock() Unlocks the camera to allow another process to access it. 3、Camera2类说明 Android5.O(SDK21)之后android.hardware.Camera就被废弃了,取而代之的 是全新的android.hardware.Camera22。Android5.0对拍照API进行了全新的设计, 新增了全新设计的Camera v2API,这些API不仅大幅提高了Android系统拍照 的功能,还能支持RAW照片输出,甚至允许程序调整相机的对焦模式、曝光模 式、快门等2]。 CameraManager:摄像头管理器。这是一个全新的系统管理器,专门用于检测系统 摄像头、打开系统摄像头。除此之外,调用CameraManager的 getCameraCharacteristics(String)方法即可获取指定摄像头的相关特性。 CameraCharacteristics:摄像头特性。该对象通过CameraManager来获取,用于描 述特定摄像头所支持的各种特性。 CameraDevice:代表系统摄像头。该类的功能类似于早期的Camera类。 CameraCaptureSession:这是一个非常重要的API,当程序需要预览、拍照时,都 需要先通过该类的实例创建Session。而且不管预览还是拍照,也都是由该对象的 方法进行控制的,其中控制预览的方法为setRepeatingRequest():控制拍照的方法 为capture()。 CameraRequest和CameraRequest..Builder:当程序调用setRepeatingRequest()方法进 行预览时,或调用capture()方法进行拍照时,都需要传入CameraRequest参数。 CameraRequest代表了一次捕获请求,用于描述捕获图片的各种参数设置,比如对 焦模式、曝光模式.…总之,程序需要对照片所做的各种控制,都通过 如果您在阅读过程中发现疏漏和错误,请联系《多媒体技术基础》课程组{network,cxh}@ustc.edu.cn
中国科学技术大学电子工程与信息科学系© 2018 如果您在阅读过程中发现疏漏和错误,请联系《多媒体技术基础》课程组{network,cxh}@ustc.edu.cn final void setPreviewTexture(SurfaceTexture surfaceTexture) Sets the SurfaceTexture to be used for live preview. final void setZoomChangeListener(Camera.OnZoomChangeListener listener) Registers a listener to be notified when the zoom value is updated by the camera driver during smooth zoom. final void startFaceDetection() Starts the face detection. final void startPreview() Starts capturing and drawing preview frames to the screen. final void startSmoothZoom(int value) Zooms to the requested value smoothly. final void stopFaceDetection() Stops the face detection. final void stopPreview() Stops capturing and drawing preview frames to the surface, and resets the camera for a future call to startPreview(). final void stopSmoothZoom() Stops the smooth zoom. final void takePicture(Camera.ShutterCallback shutter, Camera.PictureCallback raw, Camera.PictureCallback jpeg) Equivalent to takePicture(shutter, raw, null, jpeg). final void takePicture(Camera.ShutterCallback shutter, Camera.PictureCallback raw, Camera.PictureCallback postview, Camera.PictureCallback jpeg) Triggers an asynchronous image capture. final void unlock() Unlocks the camera to allow another process to access it. 3、Camera2 类说明 Android 5.0(SDK 21)之后 android.hardware.Camera 就被废弃了,取而代之的 是全新的 android.hardware.Camera2 。Android 5.0 对拍照 API 进行了全新的设计, 新增了全新设计的 Camera v2 API,这些 API 不仅大幅提高了 Android 系统拍照 的功能,还能支持 RAW 照片输出,甚至允许程序调整相机的对焦模式、曝光模 式、快门等[2]。 CameraManager:摄像头管理器。这是一个全新的系统管理器,专门用于检测系统 摄像头、打开系统摄像头。除此之外,调用 CameraManager 的 getCameraCharacteristics(String)方法即可获取指定摄像头的相关特性。 CameraCharacteristics:摄像头特性。该对象通过 CameraManager 来获取,用于描 述特定摄像头所支持的各种特性。 CameraDevice:代表系统摄像头。该类的功能类似于早期的 Camera 类。 CameraCaptureSession:这是一个非常重要的 API,当程序需要预览、拍照时,都 需要先通过该类的实例创建 Session。而且不管预览还是拍照,也都是由该对象的 方法进行控制的,其中控制预览的方法为 setRepeatingRequest();控制拍照的方法 为 capture()。 CameraRequest 和 CameraRequest.Builder:当程序调用 setRepeatingRequest()方法进 行预览时,或调用 capture()方法进行拍照时,都需要传入 CameraRequest 参数。 CameraRequest 代表了一次捕获请求,用于描述捕获图片的各种参数设置,比如对 焦模式、曝光模式……总之,程序需要对照片所做的各种控制,都通过
中国科学技术大学电子工程与信息科学系©2018 CameraRequest参数进行设置。CameraRequest.Builder则负责生成CameraRequest 对象。 4、android..hardware.ImageReader类 与播放声音不同,在使用摄像头的时候,我们需要把摄像头捕获的图像进行 显示。示例程序中使用ImageReader类完成摄像头捕获的图像数据到界面上显示 控件的显示。类ImageReader支持图像数据的着色(The ImageReader class allows direct application access to image data rendered into a Surface). 表3 android.hardware.ImageReader类的成员函数[3] 函数返回值类型 成员函数名称 Image acquireLatestImage() Acquire the latest Image from the ImageReader's queue,dropping older images. Image acquireNextlmage() Acquire the next Image from the ImageReader's queue. void close( Free up all the resources associated with this ImageReader. int getHeight() The height of each Image,in pixels. int getlmageFormat() The image format of each Image. int getMaxlmages() Maximum number of images that can be acquired from the ImageReader by any time (for example,with acquireNextlmage()). Surface getSurface() Get a Surface that can be used to produce Images for this ImageReader. int getWidth() The width of each Image,in pixels. static ImageReader newInstance(int width,int height,int format,int maxlmages) Create a new reader for images of the desired size and format. void setOnlmageAvailableListener(ImageReader.OnlmageAvailableListener listener.Handler handler) Register a listener to be invoked when a new image becomes available from the ImageReader. 表4 android.hardware.ImageReader类的回调接口[3] interface ImageReader.OnlmageAvailableListener Callback interface for being notified that a new image is available. 5、回调函数 回调函数就是一个通过函数指针调用的函数。如果你把函数的指针(地址) 作为参数传递给另一个函数,当这个指针被用来调用其所指向的函数时,我们就 说这是回调函数。回调函数不是由该函数的实现方直接调用,而是在特定的事件 或条件发生时由另外的一方调用的,用于对该事件或条件进行响应。 多媒体操作往往需要回调函数的支持,例如,我们通过摄像头捕获图像,启 动摄像头后,会通知摄像头数据采集有关的函数,等采集完一帧图像后,你要做 一些处理(回调函数中就是这些处理的具体代码)。 如果您在阅读过程中发现疏漏和错误,请联系《多媒体技术基础》课程组{network,.cxh}@ustc.edu.cn
中国科学技术大学电子工程与信息科学系© 2018 如果您在阅读过程中发现疏漏和错误,请联系《多媒体技术基础》课程组{network,cxh}@ustc.edu.cn CameraRequest 参数进行设置。CameraRequest.Builder 则负责生成 CameraRequest 对象。 4、android.hardware.ImageReader 类 与播放声音不同,在使用摄像头的时候,我们需要把摄像头捕获的图像进行 显示。示例程序中使用 ImageReader 类完成摄像头捕获的图像数据到界面上显示 控件的显示。类 ImageReader 支持图像数据的着色(The ImageReader class allows direct application access to image data rendered into a Surface)。 表 3 android.hardware.ImageReader 类的成员函数[3] 函数返回值类型 成员函数名称 Image acquireLatestImage() Acquire the latest Image from the ImageReader's queue, dropping older images. Image acquireNextImage() Acquire the next Image from the ImageReader's queue. void close() Free up all the resources associated with this ImageReader. int getHeight() The height of each Image, in pixels. int getImageFormat() The image format of each Image. int getMaxImages() Maximum number of images that can be acquired from the ImageReader by any time (for example, with acquireNextImage()). Surface getSurface() Get a Surface that can be used to produce Images for this ImageReader. int getWidth() The width of each Image, in pixels. static ImageReader newInstance(int width, int height, int format, int maxImages) Create a new reader for images of the desired size and format. void setOnImageAvailableListener(ImageReader.OnImageAvailableListener listener, Handler handler) Register a listener to be invoked when a new image becomes available from the ImageReader. 表 4 android.hardware.ImageReader 类的回调接口[3] interface ImageReader.OnImageAvailableListener Callback interface for being notified that a new image is available. 5、回调函数 回调函数就是一个通过函数指针调用的函数。如果你把函数的指针(地址) 作为参数传递给另一个函数,当这个指针被用来调用其所指向的函数时,我们就 说这是回调函数。回调函数不是由该函数的实现方直接调用,而是在特定的事件 或条件发生时由另外的一方调用的,用于对该事件或条件进行响应。 多媒体操作往往需要回调函数的支持,例如,我们通过摄像头捕获图像,启 动摄像头后,会通知摄像头数据采集有关的函数,等采集完一帧图像后,你要做 一些处理(回调函数中就是这些处理的具体代码)
中国科学技术大学电子工程与信息科学系©2018 三、 示例说明 示例代码用到的主要的类如下。 private CameraManager mCamera Manager; The ImageReader class allows direct application access to image data rendered into a Surface * private ImageReader mlmageReader, /*系统向摄像头发送Capture请求,而摄像头会返回CameraMetadata,这一切都是在由对应的CameraDevice创建的 CameraCaptureSession会活完成,当程序需要预览、拍照、再次预览时,都需要先通过会活◆ private CameraCaptureSession mCameraCaptureSession; /*CameraDevice是Camera2中抽象出来的一个对象,直接与系统硬件摄像头相联系*/ private CameraDevice mCameraDevice; /*CameraRequest代表了一次捕获请求,而CameraRequest..Builder用于描述捕获图片的各种参数设置,包含捕获硬件 (传感器,镜头,闪存),对焦模式、堡光模式,处理流水线,控制算法和输出缓冲区的配置/ private CaptureRequest Builder picture Builder; 四、 实验要求 (1)编译示例程序,运行结果截图放入实验报告。 (2)读懂示例程序VideoCapture的操作步骤,在实验报告中画出访问摄像头的 流程示意图。 五、 思考题 (1)示例代码中用到的TextureView与一般的View有什么不同? (2)阐述一下个人关于Callback函数用途的理解。 参考文献 [1] AndroidAPIs.android.hardware [Onlinel.Available:http://www.android- doc.com/reference/android/hardware/package-summary.html [2] 简书.Android Camera2使用总结[Onlinel. Available: https://www.jianshu.com/p/73fed068a795 [3] AndroidAPIs.ImageReader [Online].Available: http://www.android- doc.com/reference/android/media/ImageReader.html 如果您在阅读过程中发现疏漏和错误,请联系《多媒体技术基础》课程组{network,cxh}@ustc.edu.cn
中国科学技术大学电子工程与信息科学系© 2018 如果您在阅读过程中发现疏漏和错误,请联系《多媒体技术基础》课程组{network,cxh}@ustc.edu.cn 三、 示例说明 示例代码用到的主要的类如下。 private CameraManager mCameraManager; * The ImageReader class allows direct application access to image data rendered into a Surface */ private ImageReader mImageReader; /*系统向摄像头发送 Capture 请求,而摄像头会返回 CameraMetadata,这一切都是在由对应的 CameraDevice 创建的 CameraCaptureSession 会话完成,当程序需要预览、拍照、再次预览时,都需要先通过会话*/ private CameraCaptureSession mCameraCaptureSession; /*CameraDevice 是 Camera2 中抽象出来的一个对象,直接与系统硬件摄像头相联系*/ private CameraDevice mCameraDevice; /*CameraRequest 代表了一次捕获请求,而 CameraRequest.Builder 用于描述捕获图片的各种参数设置,包含捕获硬件 (传感器,镜头,闪存),对焦模式、曝光模式,处理流水线,控制算法和输出缓冲区的配置*/ private CaptureRequest.Builder pictureBuilder; 四、 实验要求 (1) 编译示例程序,运行结果截图放入实验报告。 (2) 读懂示例程序 VideoCapture 的操作步骤,在实验报告中画出访问摄像头的 流程示意图。 五、 思考题 (1) 示例代码中用到的 TextureView 与一般的 View 有什么不同? (2) 阐述一下个人关于 Callback 函数用途的理解。 参考文献 [1] AndroidAPIs. android.hardware [Online]. Available: http://www.androiddoc.com/reference/android/hardware/package-summary.html [2] 简 书 . Android Camera2 使用总结 [Online]. Available: https://www.jianshu.com/p/73fed068a795 [3] AndroidAPIs. ImageReader [Online]. Available: http://www.androiddoc.com/reference/android/media/ImageReader.html