Android--Camera2 (Android5.0)

Source: Internet
Author: User

Camera2

Camera2 is one of the new features in Android5.0, the new API. Compared to the original camera API, the difference is:

    • Native support Raw photo output
    • Burst shooting mode

It is no longer software but hardware that restricts the speed of photography. With Nexus 5 as an example, the continuous shooting speed of Andorid L can reach 30fps at full resolution.

    • Full Manual control

Various parameters such as shutter, speed, focus, metering, and hardware video stabilization are integrated into the new API. List of manual control features added to the new API:

    1. Speed
    2. Manual Focus/AF Switch
    3. AE/AF/AWB mode
    4. AE/AWB Lock
    5. Hardware video Stabilization
    6. Continuous frames
Dry

CAMERA2 program logic differs greatly from the original camera.

Cameramanager, System services, through Cameramanager to obtain camera device objects. Cameradevices provides parameters that describe the available and output support for camera hardware devices, which are obtained through Cameracharacteristics and cameracharacteristics from Getcameracharacteristics (Cameraid) obtained, through the source code found in Freamwork, in fact, this is directly called the Camera API interface.

With camera photography, the app first creates a photo session consisting of the output surface of the camera device, Createcapturesession (List, Cameracapturesession.statecallback, Handler). Each surface must be pre-set in the appropriate size and format to match the supported size and format of the camera device. A target surface can be obtained from different classes, including surfaceview,surfacetexture via Surface (surfacetexture), Mediacodec, Mediarecorder, Allocation, and ImageReader.

Once the request is established, he can hand it to the active photo session: A photo shoot (one-shot) or an endless continuous photo or preview (repeating). Both methods have one other way: to accept a series of requests as burst photo/repeat burst.

 Public void Opencamera (String cameraid, Cameradevice.statecallback callback, Handler Handler)

Use Getcameraidlist () to get a list of available camera devices. once the camera is successfully opened, onopened (Cameradevice) in Cameradevice.statecallback will be called . The camera device can set the operation by calling Createcapturesession () and Createcapturerequest (). If the camera device fails to open, then the OnError method of the device callback will be called, and subsequent calls to the camera device will throw a cameraaccessexception.

 Public Abstract Capturerequest.builder createcapturerequest (int templatetype)

Create a capturerequest.builder to request a picture, and initialize the template for the target use case. Choosing the best settings for a particular camera device, so it is not recommended to reuse the same request for different camera devices, create a builder for specific devices and templates, and override the settings as needed.

 Public Abstract void createcapturesession (list<surface> outputs, Cameracapturesession.statecallback callback, Handler Handler)

The active session determines the camera's output surfaces for each photo request. A given request can use all or only part of the output surfaces. Once cameracapturesession is created, you can submit a capture request, Captureburst request, setrepeatingrequest request, or Setrepeatingburst request.

Permissions
<android:name= "Android.permission.CAMERA"/>
Layout
<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "vertical">    <TextureviewAndroid:id= "@+id/textureview"Android:layout_width= "Fill_parent"Android:layout_height= "Fill_parent"/></LinearLayout>
Core code
 Public classCamerafragmentextendsFragmentImplementsTextureview.surfacetexturelistener {PrivateTextureview Mpreviewview; PrivateHandler Mhandler; PrivateHandlerthread Mthreadhandler; PrivateSize mpreviewsize; PrivateCapturerequest.builder Mpreviewbuilder;  Public Staticcamerafragment newinstance () {return Newcamerafragment (); } @SuppressWarnings ("ResourceType") @Override PublicView Oncreateview (layoutinflater inflater, ViewGroup container, Bundle savedinstancestate) {View V= Inflater.inflate (R.layout.camera_frag,NULL);        Initlooper ();        Inituiandlistener (v); returnv; }    //A lot of the process becomes asynchronous, so we need a sub-thread Looper    Private voidInitlooper () {Mthreadhandler=NewHandlerthread ("CAMERA2");        Mthreadhandler.start (); Mhandler=NewHandler (Mthreadhandler.getlooper ()); }    //can be done by Textureview or Surfaceview    Private voidInituiandlistener (View v) {Mpreviewview=(Textureview) V.findviewbyid (R.id.textureview); Mpreviewview.setsurfacetexturelistener ( This); } @SuppressWarnings ("ResourceType") @Override Public voidOnsurfacetextureavailable (Surfacetexture surface,intWidthintheight) {        Try {            //Get CameramanagerCameramanager Cameramanager =(Cameramanager) getactivity (). Getsystemservice (Context.camera_service); //Get PropertiesCameracharacteristics characteristics = Cameramanager.getcameracharacteristics ("0"); //supported Stream CONFIGURATIONStreamconfigurationmap map =Characteristics.get (CAMERACHARACTERISTICS.SCALER_STREAM_CONFIGURATION_MAP); //the size displayedMpreviewsize = map.getoutputsizes (surfacetexture.class) [0]; //turn on the cameraCameramanager.opencamera ("0", Mcameradevicestatecallback, Mhandler); } Catch(cameraaccessexception e) {e.printstacktrace (); }} @Override Public voidOnsurfacetexturesizechanged (Surfacetexture surface,intWidthintheight) {} @Override Public Booleanonsurfacetexturedestroyed (surfacetexture surface) {return false; }    //Textureview.surfacetexturelistener@Override Public voidonsurfacetextureupdated (Surfacetexture surface) {}PrivateCameradevice.statecallback Mcameradevicestatecallback =NewCameradevice.statecallback () {@Override Public voidonopened (Cameradevice camera) {Try{Startpreview (camera); } Catch(cameraaccessexception e) {e.printstacktrace (); }} @Override Public voidondisconnected (Cameradevice camera) {} @Override Public voidOnError (Cameradevice camera,interror)    {        }    }; //start previewing, mostly camera.createcapturesession this piece of code is important to create a session    Private voidStartpreview (Cameradevice camera)throwscameraaccessexception {surfacetexture texture=mpreviewview.getsurfacetexture ();        Texture.setdefaultbuffersize (Mpreviewsize.getwidth (), Mpreviewsize.getheight ()); Surface Surface=NewSurface (texture); Try{Mpreviewbuilder=camera.createcapturerequest (Cameradevice.template_preview); } Catch(cameraaccessexception e) {e.printstacktrace ();        } mpreviewbuilder.addtarget (surface);    Camera.createcapturesession (Arrays.aslist (surface), msessionstatecallback, Mhandler); }    PrivateCameracapturesession.statecallback Msessionstatecallback =NewCameracapturesession.statecallback () {@Override Public voidOnconfigured (cameracapturesession session) {Try{Updatepreview (session); } Catch(cameraaccessexception e) {e.printstacktrace (); }} @Override Public voidOnconfigurefailed (cameracapturesession session) {}}; Private voidUpdatepreview (cameracapturesession session)throwscameraaccessexception {session.setrepeatingrequest (Mpreviewbuilder.build (),NULL, Mhandler); }}
I'm the dividing line of the king of the Land Tiger.

Source: Https://github.com/pinguo-yuyidong/Camera2

Android--Camera2 (Android5.0)

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.