The company recently made a project to take pictures always crash, so decided to write a photo of their own method, in the online research, wrote a simple demo, now share the following
Main cameraactivity
Cameraactivity
PackageCom.wondertek.video.camera;ImportJava.io.FileOutputStream;Importandroid.app.Activity;ImportAndroid.hardware.Camera;ImportAndroid.hardware.Camera.AutoFocusCallback;ImportAndroid.hardware.Camera.PictureCallback;ImportAndroid.os.Bundle;ImportAndroid.util.Log;ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener;ImportAndroid.view.WindowManager;ImportAndroid.widget.Button;Importandroid.widget.FrameLayout;ImportCOM.WONDERTEK.XSGJ.R;/*** * @Description: TODO photo Optimizer *@author: SKYCC * @time: 2014-6-6 pm 5:03:10*/ Public classCameraactivityextendsActivity { Public Static Final intCamera_result = 1234; Public Static Final intRequest_picker_album = 2345; Private StaticString TAG = "Cameraobserver"; Public StaticString Photopath = ""; Button Buttonexit; Button buttoncapture; PrivateCamera Mcamera; Privatecameraviewnew Mpreview; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); //Cameraobserver has passed the Photopath path.LOG.D (TAG, "cameraactivity onCreate Photopath =" +Photopath); GetWindow (). Clearflags (WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN); GetWindow (). Addflags (WindowManager.LayoutParams.FLAG_FULLSCREEN); Setcontentview (r.layout.capture_activity); Mcamera=getcamerainstance (); //Create a preview class and associate with the camera, and finally add to the interface layoutMpreview =NewCameraviewnew ( This, Mcamera); Framelayout Preview=(framelayout) Findviewbyid (R.id.camera_preview); Preview.addview (Mpreview); //requestwindowfeature (window.feature_no_title);//Set Landscape mode and full screen mode//Setcontentview (New Cameraview (This,photopath));//Set the ViewButtoncapture =(Button) Findviewbyid (r.id.button_capture); Buttoncapture.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (View v) {//TODO auto-generated Method StubMcamera.autofocus (NewAutofocuscallback () {@Override Public voidOnautofocus (Booleansuccess, Camera camera) { //TODO auto-generated Method StubMcamera.takepicture (NULL,NULL, mypicture); } }); } }); Buttonexit=(Button) Findviewbyid (r.id.button_exit); Buttonexit.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View v) {//TODO auto-generated Method Stubfinish (); } }); } /**Open a camera*/ Public Staticcamera getcamerainstance () {camera C=NULL; Try{C=Camera.open (); } Catch(Exception e) {log.d (TAG,"Open camera failed Failed"); } returnC; } PrivatePicturecallback mypicture =NewPicturecallback () {@Override Public voidOnpicturetaken (byte[] data, camera camera) { //TODO auto-generated Method StubLOG.D (TAG, "Onpicturetaken =" +Photopath); Try { if(Photopath.length () > 0) data2file (data, Photopath); } Catch(Exception e) {} camera.stoppreview (); } Private voidData2file (byte[] W, String FileName)throwsException {//A function that converts binary data to a fileFileOutputStream out =NULL; Try{ out=NewFileOutputStream (fileName); Out.write (w); Out.close (); } Catch(Exception e) {if(Out! =NULL) Out.close (); Throwe; } } };}
Cameraviewnew
PackageCom.wondertek.video.camera;Importjava.io.IOException;Importorg.jivesoftware.smack.sasl.SASLMechanism.Success;ImportAndroid.content.Context;ImportAndroid.hardware.Camera;ImportAndroid.view.SurfaceHolder;ImportAndroid.view.SurfaceView;ImportAndroid.view.SurfaceHolder.Callback;/*** @Description: Todo re-layout photo screen *@author: SKYCC * @time: 2014-6-5 pm 11:33:11*/ Public classCameraviewnewextendsSurfaceviewImplementscallback{Private Final StaticString tag= "Cameraview";PrivateCamera Mcamera;PrivateSurfaceholder Mholder; Publiccameraviewnew (Context context,camera Camera) {Super(context); Mcamera=camera; Mholder=Getholder (); Mholder.addcallback ( This); Mholder.settype (surfaceholder.surface_type_push_buffers); //TODO auto-generated Constructor stub} @Override Public voidSurfacechanged (Surfaceholder holder,intFormatintwidth,intheight) { //TODO auto-generated Method Stub if(Mholder.getsurface () = =NULL){ return ; } mcamera.stoppreview (); Try{mcamera.setpreviewdisplay (holder); Mcamera.startpreview (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); }} @Override Public voidsurfacecreated (Surfaceholder holder) {//TODO auto-generated Method Stub Try{mcamera.setpreviewdisplay (holder); Mcamera.startpreview (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); }} @Override Public voidsurfacedestroyed (Surfaceholder holder) {//TODO auto-generated Method StubMcamera.setpreviewcallback (NULL); Mcamera.stoppreview (); Mcamera.release (); Mcamera=NULL; }}
Capture_activity.xm
<?xml version= "1.0" encoding= "Utf-8"?> <LinearLayout xmlns:android= "Http://schemas.android.com/apk/res/android"android:orientation= "Horizontal"Android:layout_width= "Fill_parent"Android:layout_height= "Fill_parent" > <framelayout Android:id= "@+id/camera_preview"Android:layout_width= "Fill_parent"Android:layout_height= "Fill_parent"Android:layout_weight= "1"/> <Button Android:id= "@+id/button_capture"Android:text= "Take pictures"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"android:layout_gravity= "Center"/> <Button Android:text= "Back"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:id= "@+id/button_exit"android:layout_gravity= "Center"/> </LinearLayout>