Android Camera2 Photo (iii)--toggle camera, time lapse and flash mode

Source: Internet
Author: User

Original: Android Camera2 photo (iii)--toggle camera, time lapse and flash mode

First, switch the camera

To switch between the front and rear cameras, you first need to close the previously opened camera, turn off preview, then reopen the new camera and reopen preview.

public void Switchcamera () {        if (mcameraid.equals (Camera_front)) {            mcameraid = camera_back;            Closecamera ();            Reopencamera ();        } else if (mcameraid.equals (Camera_back)) {            mcameraid = Camera_front;            Closecamera ();            Reopencamera ();        }    }

Turn off the camera:

/** * Closes the current {@link Cameradevice}.            */private void Closecamera () {try {mcameraopencloselock.acquire ();                Synchronized (Mcamerastatelock) {//Reset state and clean up resources used by the camera. Note:after calling this, the imagereaders would be a closed after any background//tasks Saving Images F                Rom These readers has been completed.                mpendingusercaptures = 0;                Mstate = state_closed;                    if (null! = mcapturesession) {mcapturesession.close ();                Mcapturesession = null;                    } if (null! = Mcameradevice) {mcameradevice.close ();                Mcameradevice = null;                    } if (null! = Mjpegimagereader) {mjpegimagereader.close ();                Mjpegimagereader = null; } if (null! = MrawimagEReader) {mrawimagereader.close ();                Mrawimagereader = null; }}} catch (Interruptedexception e) {throw new RuntimeException ("interrupted while trying t        o Lock camera closing. ", e);        } finally {mcameraopencloselock.release (); }    }

To open a new camera:

public void Reopencamera () {        if (mtextureview.isavailable ()) {            opencamera ();        } else {            Mtextureview.setsurfacetexturelistener (Msurfacetexturelistener);        }    }

If Textureview is available, then call Opencamera () directly to open the camera (Opencamera () code reference before the blog); Textureview is unavailable, then call Textureview's Setsurfacetexturelistener settings Surfacetexturelistener (refer to the previous blog). Second, time-lapse shooting

To achieve time-lapse shooting, the core is to implement a timer. The delay of this example is divided into three modes: 3s delay, 10s on time, no delay. You can switch between the three modes of your own.

private void Switchdelaystate () {        switch (mdelaystate) {case            0:                mtimer.setimageresource (r.mipmap.ic_3s) ;                Mdelaytime = 3 *;                Mdelaystate = 1;                break;            Case 1:                mtimer.setimageresource (r.mipmap.ic_10s);                Mdelaytime = ten *;                Mdelaystate = 2;                break;            Case 2:                mtimer.setimageresource (r.mipmap.timer);                mdelaytime = 0;                mdelaystate = 0;                break;            Default: Break                ;        }    }

Definitions of variables Mdelaytime and mdelaysate:

/     * Delay State, 0 represents no delay, 1 represents 3s delay, while 2 represents 10s delay     */    private short mdelaystate = 0; Timer    private short mdelaytime;

When the shooting button is clicked:

 @Override public void OnClick (view view) {switch (View.getid ())                {Case R.id.capture: {if (mdelaystate = = 0) {takepicture ();                        } else {new Countdowntimer (Mdelaytime, time_interval) {@Override public void OnTick (long millisuntilfinished) {mtimetext.setvisibility (view.visible                            );                        Mtimetext.settext ("" + millisuntilfinished/time_interval); } @Override public void OnFinish () {mtimetext.se                            Tvisibility (View.gone);                        Takepicture ();                }}.start ();            } break; }

However, 3s or 10s delay time, the use of Countdowntimer to achieve the countdown and every second to update the remaining time hint, then perform the camera function. Three, switch flash mode

Support auto, force, suppress flash. The properties associated with the flash mode are capturerequest. Control_ae_mode and Capturerequest. Flash_mode. If you want to use Flash_mode Zodiac, the Flash must be available, and the Control_ae_mode must be set to on or off, otherwise, the on_auto_flash,on_always_flash of the AE property , On_ The Auto_flash_redeye will overwrite the Flash_mode settings.

private void Switchflashmode () {switch (mflashmode) {case 0:mflashmode = 1;                Mflashbtn.setimageresource (R.mipmap.flash_auto);                Mpreviewrequestbuilder.set (Capturerequest.control_ae_mode, Capturerequest.control_ae_mode_on_auto_flash);                            try {mcapturesession.setrepeatingrequest (), Mpreviewrequestbuilder.build (),                Mprecapturecallback, Mbackgroundhandler);                    } catch (Cameraaccessexception e) {e.printstacktrace ();                Return            } break;                Case 1:mflashmode = 2;                Mflashbtn.setimageresource (r.mipmap.flash_on);                Mpreviewrequestbuilder.set (Capturerequest.control_ae_mode, Capturerequest.control_ae_mode_on_always_flash); try {mcapturesession.setrepeatingrequest (MPReviewrequestbuilder.build (), Mprecapturecallback, Mbackgroundhandler);                    } catch (Cameraaccessexception e) {e.printstacktrace ();                Return            } break;                Case 2:mflashmode = 0;                Mflashbtn.setimageresource (R.mipmap.flash_off);                Mpreviewrequestbuilder.set (Capturerequest.control_ae_mode, capturerequest.control_ae_mode_on);                Mpreviewrequestbuilder.set (Capturerequest.flash_mode, Capturerequest.flash_mode_off);                            try {mcapturesession.setrepeatingrequest (), Mpreviewrequestbuilder.build (),                Mprecapturecallback, Mbackgroundhandler);                    } catch (Cameraaccessexception e) {e.printstacktrace ();                Return        } break; }    }

Before Takepicture (), you need to set it again:

private void Setflashmode () {        switch (mflashmode) {case            0:                mpreviewrequestbuilder.set ( Capturerequest.control_ae_mode, capturerequest.control_ae_mode_on);                Mpreviewrequestbuilder.set (Capturerequest.flash_mode, capturerequest.flash_mode_off);                break;            Case 1:                mpreviewrequestbuilder.set (Capturerequest.control_ae_mode, Capturerequest.control_ae_mode_on_auto_ FLASH);                break;            Case 2:                mpreviewrequestbuilder.set (Capturerequest.control_ae_mode, Capturerequest.control_ae_mode_on_always _flash);                break;        }    }
Code Reference: Https://github.com/gengqifu/361Camera, Welcome to Fork/star.

Android Camera2 Photo (iii)--toggle camera, time lapse and flash mode

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.