由于要在Android系统上进行OpenCV相关的功能使用,所以写本篇blog记录一下如何使用Camera API 获取Android的图像数据。本篇暂且使用 deprecated 的Camera API
先看API:https://developer.android.com/reference/android/hardware/Camera
The Camera class is used to set image capture settings, start/stop preview, snap pictures, and retrieve frames for encoding for video. This class is a client for the Camera service, which manages the actual camera hardware.
To access the device camera, you must declare the Manifest.permission.CAMERA
permission in your Android Manifest. Also be sure to include the <uses-feature> manifest element to declare camera features used by your application. For example, if you use the camera and auto-focus feature, your Manifest should include the following:
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
To take pictures with this class, use the following steps:
- Obtain an instance of Camera from
open(int)
. - Get existing (default) settings with
getParameters()
. - If necessary, modify the returned
Camera.Parameters
object and callsetParameters(Camera.Parameters)
. - Call
setDisplayOrientation(int)
to ensure correct orientation of preview. - Important: Pass a fully initialized
SurfaceHolder
tosetPreviewDisplay(SurfaceHolder)
. Without a surface, the camera will be unable to start the preview. - Important: Call
startPreview()
to start updating the preview surface. Preview must be started before you can take a picture. - When you want, call
takePicture(Camera.ShutterCallback, Camera.PictureCallback, Camera.PictureCallback, Camera.PictureCallback)
to capture a photo. Wait for the callbacks to provide the actual image data. - After taking a picture, preview display will have stopped. To take more photos, call
startPreview()
again first. - Call
stopPreview()
to stop updating the preview surface. - Important: Call
release()
to release the camera for use by other applications. Applications should release the camera immediately inActivity.onPause()
(and re-open()
it inActivity.onResume()
).
To quickly switch to video recording mode, use these steps:
- Obtain and initialize a Camera and start preview as described above.
- Call
unlock()
to allow the media process to access the camera. - Pass the camera to
MediaRecorder.setCamera(Camera)
. SeeMediaRecorder
information about video recording. - When finished recording, call
reconnect()
to re-acquire and re-lock the camera. - If desired, restart preview and take more photos or videos.
- Call
stopPreview()
andrelease()
as described above.
This class is not thread-safe, and is meant for use from one event thread. Most long-running operations (preview, focus, photo capture, etc) happen asynchronously and invoke callbacks as necessary. Callbacks will be invoked on the event thread open(int)
was called from. This class's methods must never be called from multiple threads at once.
Camera的API文档如上,如何在Android中使用:
1.新建Android 工程,在AndroidManifest.xml文件中添加权限。(必要)
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
2.检测Android设备中存在多少个camera device。(可选)
int cameraNum = Camera.getNumberOfCameras();
3.打开Camera设备 (必要)
try {
mCamera = Camera.open();
}catch (Exception e){
e.printStackTrace();
}
4.设置Camera的orientation,[ 0度, 90度, 180度, 270度 ] (可选)
mCamera.setDisplayOrientation(90);
5.将完全初始化的SurfaceHolder传递给setPreviewDisplay(SurfaceHolder)。(必要)
mCamera.setPreviewDisplay(surfaceHolder);
6.调用startPreview()开始更新预览表面。(必要)
mCamera.startPreview();
7.调用setPreviewCallback(Camera.PreviewCallback cb) 获取每一帧的数据(根据业务)
mCamera.setPreviewCallback(new Camera.PreviewCallback() {
@Override
public void onPreviewFrame(byte[] bytes, Camera camera) {
Log.e("Camera","onPreviewFrame()");
}
});
8.调用stopPreview()以停止更新预览曲面。(必要)
mCamera.stopPreview();
9.调用release()以释放相机以供其他应用程序使用。(必要)
mCamera.release();
10.建议在activity的生命周期切换中,同样注意Camera的状态。
others:关于Camera.Parameters android相机参数暂时没有发现什么用途。
附上链接以后看:https://developer.android.com/reference/android/hardware/Camera.Parameters
下载源代码:https://github.com/tony1213/CameraPreviewDemo
更新:有了Camera返回的每一帧byte数组数据,我就可以在Android JNI 中使用OpenCV了。
Android JNI入门级使用:https://blog.csdn.net/u011608180/article/details/85063634
Android JNI 中使用OpenCV:https://blog.csdn.net/u011608180/article/details/85244329