Packagecom.ddv.www.candidphotodemo;
Importandroid.annotation.TargetApi;
importandroid.app.Activity;
Importandroid.content.Context;
Importandroid.content.Intent;
Importandroid.content.pm.PackageManager;
Importandroid.hardware.Camera;
Importandroid.hardware.Camera.AutoFocusCallback;
Importandroid.hardware.Camera.PictureCallback;
Importandroid.os.Handler;
Importandroid.os.Message;
Importandroid.widget.FrameLayout;
Importjava.io.FileOutputStream;
* Set the timing camera function
* Create a timed photo task
* Resolutionstring Resolution
* Tvsavelocation Save Address
* Etextension Development Name
* Camerastart, start shooting time
* Cameranumber, shooting times
Publicclassinittimetotakepic {
Privatestaticinittimetotakepic minstance;
Privatestaticintcameratype = 1;
Staticframelayout Msurfaceviewframe;
Privatestaticcamera Mcamera;
Privatestaticcamerapreview Mpreview;
Privatestaticstring resolutionstring = "1920x1080";
privatestaticstring SaveLocation = Apputils.getsdcardpath ();
privatestaticstring extension = "JPG";
Privatestaticstring Camerastart = "1";
Privatestaticstring Cameranumber = "1";
Privatestaticstring camerastop = "10";
Privatestaticintnumber = 0;
Privatestaticbooleanclearvoice =false;
Privateinittimetotakepic (Context context) {
Publicsynchronizedstaticinittimetotakepic getinstance (Context context) {
Minstance =newinittimetotakepic (context);
Publicvoidinitview (Framelayout surfaceviewframe) {
Msurfaceviewframe = Surfaceviewframe;
* Start timed photo and upload function
Handler Mhandler =newhandler () {
Publicvoidhandlemessage (Message msg) {
LOGUTILS.V ("Start taking pictures");
Mhandler.removecallbacksandmessages (NULL);
if (number < integer.valueof (camerastop)) {
Mcamera.autofocus (Newautofocuscallback () {
Publicvoidonautofocus (booleansuccess, Camera Camera) {
Capturing pictures from camera
LOGUTILS.V ("autofocus" + success);
Mcamera.takepicture (Null,null, mpicture);
Mhandler.sendemptymessagedelayed (1, integer.valueof (cameranumber) *1000);
Mhandler.removecallbacksandmessages (NULL);
Mhandler.removecallbacksandmessages (NULL);
Mhandler.sendemptymessagedelayed (1,1*1000); start the camera after//7s
Privatevoidinitcarema () {
LOGUTILS.V ("Initcarema");
LOGUTILS.V ("Camera=null");
Mcamera = Getcamerainstance ();
Mpreview =newcamerapreview (Mcontext, Mcamera);
Msurfaceviewframe.removeallviews ();
Msurfaceviewframe.addview (Mpreview);
LOGUTILS.V (Mcamera ==null?) Mcamera is null ': ' Mcamera is not null ');
Mhandler.sendemptymessagedelayed (2, integer.valueof (Camerastart) *1000); Photos after//3s
* Detect whether the device exists camera hardware
Privatebooleancheckcamerahardware (Context context) {
if (Context.getpackagemanager (). Hassystemfeature (
Packagemanager.feature_camera)) {
@TargetApi (build.version_codes. JELLY_BEAN_MR1)
Publicstaticcamera getcamerainstance () {
c = Camera.open (Cameratype);
C.setdisplayorientation (90);
Camera.parameters mparameters = C.getparameters ();
C.enableshuttersound (Clearvoice);
You can use the picture size that you currently support, and then
List<size> ms = Mparameters.getsupportedpicturesizes ();
Mparameters.setpicturesize (Ms.get (0). Width, ms.get (0). height); Default maximum photo to take maximum clarity
string[] xes = Resolutionstring.split ("x");
Logutils.i ("ms.get (0) .width==>" +ms.get (0). width);
Logutils.i ("ms.get (0) .height==>" +ms.get (0). height);
Logutils.i ("integer.valueof (Xes[0]) ==>" +integer.valueof (xes[0));
Logutils.i ("integer.valueof (xes[1]) ==>" +integer.valueof (xes[1));
Mparameters.setpicturesize (Integer.valueof (Xes[0]), integer.valueof (xes[1));//default maximum photo taking maximum resolution
C.setparameters (mparameters);
LOGUTILS.V ("Open camera failure failed");
Privatepicturecallback mpicture =newpicturecallback () {
Publicvoidonpicturetaken (byte[] data, Camera Camera) {
Get the JPEG picture and save it on the SD card
String path = SaveLocation;
File DIRF =newfile (path);
File picturefile =newfile (path + "/" + system.currenttimemillis () + ".") + extension);//Extension
FileOutputStream Fos =newfileoutputstream (picturefile);
LOGUTILS.V ("Save diagram Success");
Intent.setaction ("Camerafragment.start");
Intent.putextra ("number", number);
Mcontext.sendbroadcast (Intent);
LOGUTILS.V ("Save picture Failed");
Publicvoidreleasecarema () {