Today, let's talk about how to develop a camera on the Android platform! Here we will talk about calling the System camera. 1. Obtain the surfaceview object [html] surfaceView = (SurfaceView) findViewById (R. id. g3_server_preview); surfaceView = (SurfaceView) findViewById (R. id. g3_server_preview); 2. Declare some callback Methods later to enable the camera and set the camera parameter [html] // SurfaceHodler callback to process the camera opening, closing the camera and changing the camera size SurfaceHolder. callback surfaceCallback = new SurfaceHolder. callback () {@ Override public void surfaceCreated (SurfaceHolder holder) {// enable camera Camera = camera. open (); Try {// set to preview camera. setPreviewDisplay (holder);} catch (IOException e) {e. printStackTrace () ;}@ Override public void surfaceChanged (SurfaceHolder holder, int format, int width, int height) {// obtain the Camera Parameter Camera. parameters parameters = camera. getParameters (); // SDK version selection, compatible with if (Integer. parseInt (Build. VERSION. SDK)> = 8) {setDisplayOrientation (camera, 90);} else {if (getResources (). getConfigur Ation (). orientation = Configuration. ORIENTATION_PORTRAIT) {parameters. set ("orientation", "portrait"); parameters. set ("rotation", 90);} if (getResources (). getConfiguration (). orientation = Configuration. ORIENTATION_LANDSCAPE) {parameters. set ("orientation", "landscape"); parameters. set ("rotation", 90) ;}// set the preview size parameters. setPreviewSize (width, height); pic_Width = g3_setting.getInt (AppData. G3_PIC_WIDTH, 640); pic_Height = g3_setting.getInt (AppData. g3_PIC_HEIGHT, 800); Log. I ("Travel", pic_Width + ":" + pic_Height); // set the Photo size parameters. setPictureSize (pic_Width, pic_Height); // set the photo format // parameters. setPictureFormat (PixelFormat. RGB_565); // start previewing camera. startPreview (); // sets the camera Parameter camera. setParameters (parameters) ;}@ Override public void surfaceDestroyed (SurfaceHolder holder) {// stop previewing if (Camera! = Null) {camera. stopPreview (); // release the camera resource camera. release (); camera = null ;}}; // SurfaceHodler callback to handle camera opening, camera closing, and SurfaceHolder size change. callback surfaceCallback = new SurfaceHolder. callback () {@ Overridepublic void surfaceCreated (SurfaceHolder holder) {// enable camera Camera = camera. open (); try {// set to preview camera. setPreviewDisplay (holder);} catch (IOException e) {e. printStackTrace () ;}@ Overridepublic void surfa CeChanged (SurfaceHolder holder, int format, int width, int height) {// obtain the Camera Parameter Camera. parameters parameters = camera. getParameters (); // SDK version selection, compatible with if (Integer. parseInt (Build. VERSION. SDK)> = 8) {setDisplayOrientation (camera, 90);} else {if (getResources (). getConfiguration (). orientation = Configuration. ORIENTATION_PORTRAIT) {parameters. set ("orientation", "portrait"); parameters. set ("rotation", 90);} if (GetResources (). getConfiguration (). orientation = Configuration. ORIENTATION_LANDSCAPE) {parameters. set ("orientation", "landscape"); parameters. set ("rotation", 90) ;}// set the preview size parameters. setPreviewSize (width, height); pic_Width = g3_setting.getInt (AppData. g3_PIC_WIDTH, 640); pic_Height = g3_setting.getInt (AppData. g3_PIC_HEIGHT, 800); Log. I ("Travel", pic_Width + ":" + pic_Height); // set the Photo size parameters. setPi CtureSize (pic_Width, pic_Height); // set the photo format // parameters. setPictureFormat (PixelFormat. RGB_565); // start previewing camera. startPreview (); // sets the camera Parameter camera. setParameters (parameters) ;}@ Overridepublic void surfaceDestroyed (SurfaceHolder holder) {// stop previewing if (camera! = Null) {camera. stopPreview (); // release the camera resource camera. release (); camera = null ;}}; [html] view plaincopyprint? Protected void setDisplayOrientation (Camera camera, int angle) {Method downPolymorphic; try {downPolymorphic = camera. getClass (). getMethod ("setDisplayOrientation", new Class [] {int. class}); if (downPolymorphic! = Null) downPolymorphic. invoke (camera, new Object [] {angle});} catch (Exception e1) {}} protected void setDisplayOrientation (Camera camera, int angle) {Method downPolymorphic; try {downPolymorphic = camera. getClass (). getMethod ("setDisplayOrientation", new Class [] {int. class}); if (downPolymorphic! = Null) downPolymorphic. invoke (camera, new Object [] {angle});} catch (Exception e1) {}} 3. Then set the surfaceview parameter, load callback method [html] // set the surfaceView that is always on the screen. setKeepScreenOn (true); surfaceView. setFocusable (true); releaseCamera (); // obtain SurfaceHolder SurfaceView from surfaceView. getHolder (). addCallback (surfaceCallback); // Add a callback for SurfaceHolder // surfaceHolder. addCallback (surfaceCallback); surfaceView. getHolder (). set Type (SurfaceHolder. SURFACE_TYPE_PUSH_BUFFERS); // you can specify the regular surfaceView on the screen. setKeepScreenOn (true); surfaceView. setFocusable (true); releaseCamera (); // obtain SurfaceHoldersurfaceView from SurfaceView. getHolder (). addCallback (surfaceCallback); // Add a callback for SurfaceHolder // surfaceHolder. addCallback (surfaceCallback); surfaceView. getHolder (). setType (SurfaceHolder. SURFACE_TYPE_PUSH_BUFFERS); release camera resources [html] private void releaseCamer A () {// TODO Auto-generated method stub if (camera! = Null) {camera. stopPreview (); // release the camera resource camera. release (); camera = null;} private void releaseCamera () {// TODO Auto-generated method stubif (camera! = Null) {camera. stopPreview (); // release the camera resource camera. release (); camera = null;} 4. Callback Method for configuring Camera sound or vibration [html] camera. shutterCallback shutter = new ShutterCallback () {@ Override public void onShutter () {// TODO Auto-generated method stub if (tone = null) {// send a prompt to the user. tone = new ToneGenerator (AudioManager. STREAM_SYSTEM, ToneGenerator. MAX_VOLUME);} if (g3_setting.getBoolean (AppData. g3_CONFIG_AUDIO, true )){ Tone. startTone (ToneGenerator. TONE_PROP_BEEP);} else {tone. stopTone (); Vibrator vibrator = (Vibrator) context. getSystemService (Service. VIBRATOR_SERVICE); vibrator. vibrate (100) ;}}; Camera. shutterCallback shutter = new ShutterCallback () {@ Overridepublic void onShutter () {// TODO Auto-generated method stubif (tone = null) {// send a prompt to the user. tone = new ToneGenerator (AudioManager. STREAM_SYSTEM, ToneGe Nerator. MAX_VOLUME);} if (g3_setting.getBoolean (AppData. g3_CONFIG_AUDIO, true) {tone. startTone (ToneGenerator. TONE_PROP_BEEP);} else {tone. stopTone (); Vibrator vibrator = (Vibrator) context. getSystemService (Service. VIBRATOR_SERVICE); vibrator. vibrate (100) ;}}; minor episode: determines whether the system sound can be disabled [html] File file = new File ("/system/media/audio/ui/Camera_click.ogg "); if (! File. canWrite () {Tools. showToast (context, "the set sound is invalid! ");} Else {file. renameTo (new File ("/system/media/audio/ui/Camera_click.ogg1 "));} file file = new File ("/system/media/audio/ui/Camera_click.ogg"); if (! File. canWrite () {Tools. showToast (context, "the set sound is invalid! ");} Else {file. renameTo (new File ("/system/media/audio/ui/Camera_click.ogg1");} 5. focus automatically and take a photo [html] // After focusing, take a photo of camera. autoFocus (new AutoFocusCallback () {@ Override public void onAutoFocus (boolean success, Camera camera) {// TODO Auto-generated method stub // if success is true, the focus is successful if (success) {if (camera! = Null) {// stop previewing camera. stopPreview (); // take a photo of camera. takePicture (shutter, null, pictureCallback) ;}}}); // take a photo of camera after focusing. autoFocus (new AutoFocusCallback () {@ Overridepublic void onAutoFocus (boolean success, Camera camera) {// TODO Auto-generated method stub // if success is true, the focus is successful if (success) {if (camera! = Null) {// stop previewing camera. stopPreview (); // take a photo of camera. takePicture (shutter, null, pictureCallback) ;}}}); [html] // photo callback Camera. pictureCallback pictureCallback = new Camera. pictureCallback () {@ Override public void onPictureTaken (byte [] data, Camera camera) {Log. I ("G3", "picture operating ............. "); new savepicturetask(cmd.exe cute (data); camera. startPreview () ;}}; // callback Camera. pictureCallback pictur ECallback = new Camera. pictureCallback () {@ Overridepublic void onPictureTaken (byte [] data, Camera camera) {Log. I ("G3", "picture operating ............. "); new savepicturetask(cmd.exe cute (data); camera. startPreview () ;}}; [html] // class SavePictureTask extends AsyncTask <byte [], String, string >{@ Override protected String doInBackground (byte []... params) {Log. I ("G3", "picture saving ............. "); File path = new File (Tools. getSDPath (); if (! Path. exists () {path. mkdirs ();} // create the File picture = new File (Tools. getSDPath (), Tools. initPhotoName (); try {// get the file output stream FileOutputStream fos = new FileOutputStream (picture. getPath (); // write to this file fos. write (params [0]); // close the file stream fos. close ();} catch (Exception e) {e. printStackTrace () ;}return null ;}// class SavePictureTask extends AsyncTask <byte [], String, String >{@ Overrideprotected String doInBackground (byte []... params) {Log. I ("G3", "picture saving ............. "); File path = new File (Tools. getSDPath (); if (! Path. exists () {path. mkdirs ();} // create the File picture = new File (Tools. getSDPath (), Tools. initPhotoName (); try {// get the file output stream FileOutputStream fos = new FileOutputStream (picture. getPath (); // write to this file fos. write (params [0]); // close the file stream fos. close ();} catch (Exception e) {e. printStackTrace ();} return null;} 6. Note:. the code of this project has implemented the focus function. If your camera cannot focus, it is not supported by the mobile phone itself and is related to hardware B. resolution settings have been implemented in the surfaceChanged method in step 1. The data exists in SharedPreference, There is a small problem. The configuration resolution page must be written on another page, because the surfaceCallback event cannot be removed and the camera is released, it still does not work, however, the system camera of the original mobile phone can be configured as a page, so if you implement this function, please kindly advise!