Android and android Official Website

Source: Internet
Author: User

Android and android Official Website
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:

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<Surface> 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
<uses-permission android:name="android.permission.CAMERA"/>
Layout
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"              android:layout_width="match_parent"              android:layout_height="match_parent"              android:orientation="vertical">    <TextureView        android:id="@+id/textureview"        android:layout_width="fill_parent"        android:layout_height="fill_parent"/></LinearLayout>
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 initlodid () {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. setSurfaceTextureListener (this) ;}@ SuppressWarnings ("ResourceType") @ Override public void onSurfaceTextureAvailable (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 );}}
I am the dividing line of tiantiao

Source code: https://github.com/pinguo-yuyidong/Camera2

Other brilliant articles

Android KSOAP2 call. net webservicejQuery tutorial (8)-DOM tree operation using reverse Insertion Method android learning notes (34) using AlertDialog to create a simple dialog box android learning notes (33) Gallery view (Gallery) android navidgation drawer in the navigation drawer how to change the List of selected items...

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.