Android Development No preview timed shots

Source: Internet
Author: User

Demo Implementation Features:

Open the main page automatically start a timed photo shoot, 10s shot once.

Precautions, after initializing the camera can not immediately take pictures, otherwise invalid, must wait a few seconds before shooting. This is used by handler to delay the processing of photo messages.

 PackageCom.feijian.takephotodemo;ImportJava.io.File;ImportJava.io.FileOutputStream;Importjava.util.List;Importandroid.app.Activity;ImportAndroid.content.Context;ImportAndroid.content.pm.PackageManager;ImportAndroid.hardware.Camera;ImportAndroid.hardware.Camera.AutoFocusCallback;ImportAndroid.hardware.Camera.PictureCallback;Importandroid.hardware.Camera.Size;Importandroid.os.Environment;ImportAndroid.os.Handler;ImportAndroid.os.Message;ImportAndroid.util.Log;Importandroid.widget.FrameLayout;/*** Set up timed photo function *@authorHuqiang **/ Public classInittimetotakepic {Private StaticInittimetotakepic minstance;    Activity Mcontext; StaticFramelayout Msurfaceviewframe; Private StaticCamera Mcamera; Private StaticCamerapreview Mpreview; StaticString TAG = inittimetotakepic.class. Getsimplename (); PrivateInittimetotakepic (Activity context) { This. Mcontext =context; }     Public synchronized Staticinittimetotakepic getinstance (Activity context) {if(Minstance = =NULL) {minstance=Newinittimetotakepic (context); }        returnminstance; }     Public voidInitview (framelayout surfaceviewframe) {msurfaceviewframe=Surfaceviewframe; }    /*** Start a timed photo and upload function*/Handler Mhandler=NewHandler () {@Override Public voidhandlemessage (Message msg) {Switch(msg.what) { Case1: LOG.V (TAG,"Start taking pictures");                Initcarema ();  Break;  Case2: Mcamera.autofocus (NewAutofocuscallback () {@Override Public voidOnautofocus (Booleansuccess, Camera camera) {                        //capturing pictures from the cameraLOG.V (TAG, "Auto Focus 111" +success); Mcamera.takepicture (NULL,NULL, mpicture); Mhandler.sendemptymessagedelayed (1, 5*1000);                }                });  Break;    }        }    };  Public voidstart () {mhandler.sendemptymessagedelayed (1, 7*1000);//Start the camera after 7s    }    Private voidInitcarema () {log.v (TAG,"Initcarema"); if(mcamera==NULL) {log.v (TAG,"Camera=null"); Mcamera=getcamerainstance (); Mpreview=NewCamerapreview (Mcontext, Mcamera);            Msurfaceviewframe.removeallviews ();        Msurfaceviewframe.addview (Mpreview); } log.v (TAG, Mcamera==NULL?" Mcamera is null ":" Mcamera is not NULL ");        Mcamera.startpreview (); Mhandler.sendemptymessagedelayed (2, 3*1000);//Take photos after 3s    }    /**detects if the device has camera hardware*/    Private BooleanCheckcamerahardware (Context context) {if(Context.getpackagemanager (). Hassystemfeature (Packagemanager.feature_camera)) {//exist            return true; } Else {            //does not exist            return false; }    }    /**Open a camera*/     Public Staticcamera getcamerainstance () {camera C=NULL; Try{C=Camera.open (); C.setdisplayorientation (90); Camera.parameters mparameters=c.getparameters (); //you can use it to get the size of the photo currently supported, and thenList<size> ms =mparameters.getsupportedpicturesizes (); Mparameters.setpicturesize (Ms.get (0). Width, ms.get (0). height);//default maximum photo for maximum resolutionc.setparameters (mparameters); } Catch(Exception e) {log.d (TAG,"Open camera failed Failed"); }        returnC; }    PrivatePicturecallback mpicture =NewPicturecallback () {@Override Public voidOnpicturetaken (byte[] data, camera camera) {            //get the JPEG picture and save it on the SD cardString path = Environment.getexternalstoragedirectory (). GetAbsolutePath () + "/data/data/"; File DIRF=NewFile (path); if(!dirf.exists ())            {Dirf.mkdirs (); } File PictureFile=NewFile (path + system.currenttimemillis () + ". jpg"); Try{FileOutputStream fos=NewFileOutputStream (PictureFile);                Fos.write (data);                Fos.close (); LOG.D (TAG,"Save Diagram Success"); } Catch(Exception e) {log.d (TAG,"Save picture Failed");            E.printstacktrace ();        } releasecarema ();    }    };  Public voidReleasecarema () {if(mcamera!=NULL) {Mcamera.stoppreview ();            Mcamera.release (); Mcamera=NULL; }    }}

Demo download

Android Development No preview timed shots

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.