Android -- Camera2 (Android5.0)
Android -- Camera2 (Android5.0)
Camera2
Camera2 is a new feature and API in Android5.0. Compared with the original camera API, the difference is:
Native supports RAW photo output burst shooting Mode
The speed of photography is no longer limited by software but hardware. Taking Nexus 5 as an example, the resolution can be fully enabled and the sequential speed of Andorid L can reach 30 FPS.
Full manual control
Shutter, sensitivity, focus, metering, hardware video stabilization, and other parameters are integrated into the new API. List of manual control functions added to the new API:
Sensitivity manual focus/AF switch AE/AF/AWB mode AE/AWB lock hardware video anti-shake continuous frame dry goods
The program logic of Camera2 is quite different from that of the original Camera.
CameraManager, a system service that uses CameraManager to obtain the camera device object.CameraDevices provides parameters describing the availability and output of camera hardware devices, which are obtained through CameraCharacteristics., CameraCharacteristics is obtained from getCameraCharacteristics (cameraId). The source code in freamwork shows that the Camera API is called directly.
When taking a photo using camera, the app first needs to create a photo session consisting of the output surface of the camera device, createCaptureSession (List, CameraCaptureSession. StateCallback, Handler ). Each surface must be pre-configured with an appropriate size and format to match the supported size and format of the camera device. A target surface can be obtained from different classes, includingSurfaceView, SurfaceTexture via Surface (SurfaceTexture), MediaCodec, MediaRecorder, Allocation, and ImageReader.
Once a request is created, it can be handed over to the photo session of the activity: one-shot or continuous photograph or preview (Repeating ). Either method has another method: accept a series of requests as the burst Photo/Duplicate burst.
public void openCamera (String cameraId, CameraDevice.StateCallback callback, Handler handler)
Use getCameraIdList () to obtain a list of available camera devices. Once the camera is turned on, onOpened (CameraDevice) in CameraDevice. StateCallback will be called. The camera device can call createCaptureSession () and createCaptureRequest () to set the operation. If the camera fails to be opened, the onError method of the device callback will be called, And a CameraAccessException will be thrown out after the camera device is called.
public abstract CaptureRequest.Builder createCaptureRequest (int templateType)
Create a CaptureRequest. Builder for the request to take a photo and initialize the template of the target Use Case. Select the best settings as a specific camera device, so we do not recommend that you reuse the same request for different camera devices, create a builder for specific devices and templates, and overwrite the settings as needed.
public abstract void createCaptureSession (List
outputs, CameraCaptureSession.StateCallback callback, Handler handler)
Active sessions determine the output Surfaces of the camera for each photo. For a given request, all or only some output Surfaces can be used. Once CameraCaptureSession is created, you can submit the capture request, captureBurst request, setRepeatingRequest, or setRepeatingBurst request.
Permission
Layout
Core code
Public class CameraFragment extends Fragment implements TextureView. surfaceTextureListener {private TextureView mPreviewView; private Handler mHandler; private HandlerThread mThreadHandler; private Size mPreviewSize; private CaptureRequest. builder mPreviewBuilder; public static vertex newInstance () {return new vertex () ;}@ SuppressWarnings (ResourceType) @ Override public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {View v = inflater. inflate (R. layout. camera_frag, null); initloener (); initUIAndListener (v); return v ;}// many processes have become asynchronous, so here we need a sub-thread logoff private void initLooper () {mThreadHandler = new HandlerThread (CAMERA2); mThreadHandler. start (); mHandler = new Handler (mThreadHandler. getLooper ();} // you can use TextureView or SurfaceView private void initUIAndListener (View v) {mPreviewView = (TextureView) v. findViewById (R. id. textureview); mPreviewView. summary (this) ;}@ SuppressWarnings (ResourceType) @ Override public void Preview (SurfaceTexture surface, int width, int height) {try {// get CameraManager cameraManager = (CameraManager) getActivity (). getSystemService (Context. CAMERA_SERVICE); // get the attribute CameraCharacteristics characteristics = cameraManager. getCameraCharacteristics (0); // supported stream configuration StreamConfigurationMap map = characteristics. get (CameraCharacteristics. SCALER_STREAM_CONFIGURATION_MAP); // The displayed size mPreviewSize = map. getOutputSizes (SurfaceTexture. class) [0]; // turn on the camera cameraManager. openCamera (0, mCameraDeviceStateCallback, mHandler);} catch (CameraAccessException e) {e. printStackTrace () ;}@ Override public void onSurfaceTextureSizeChanged (SurfaceTexture surface, int width, int height) {}@ Override public boolean onSurfaceTextureDestroyed (SurfaceTexture surface) {return false ;} // TextureView. surfaceTextureListener @ Override public void onSurfaceTextureUpdated (SurfaceTexture surface) {} private CameraDevice. stateCallback mCameraDeviceStateCallback = new CameraDevice. stateCallback () {@ Override public void onOpened (CameraDevice camera) {try {startPreview (camera);} catch (CameraAccessException e) {e. printStackTrace () ;}@ Override public void onDisconnected (CameraDevice camera) {}@ Override public void onError (CameraDevice camera, int error) {}}; // start previewing, mainly camera. the createCaptureSession code is very important. Create a session private void startPreview (CameraDevice camera) throws CameraAccessException {SurfaceTexture texture = mPreviewView. getSurfaceTexture (); texture. setdefabuffbuffersize (mPreviewSize. getWidth (), mPreviewSize. getHeight (); Surface surface = new Surface (texture); try {mPreviewBuilder = camera. createCaptureRequest (CameraDevice. TEMPLATE_PREVIEW);} catch (CameraAccessException e) {e. printStackTrace ();} mPreviewBuilder. addTarget (surface); camera. createCaptureSession (Arrays. asList (surface), mSessionStateCallback, mHandler);} private CameraCaptureSession. stateCallback mSessionStateCallback = new CameraCaptureSession. stateCallback () {@ Override public void onConfigured (CameraCaptureSession session) {try {updatePreview (session);} catch (CameraAccessException e) {e. printStackTrace () ;}@ Override public void onConfigureFailed (CameraCaptureSession session) {}}; private void updatePreview (CameraCaptureSession session) throws CameraAccessException {session. setRepeatingRequest (mPreviewBuilder. build (), null, mHandler );}}