MainActivity: [java] package c. c; import java. io. IOException; import java. util. iterator; import java. util. list; import android. app. activity; import android. content. contentResolver; import android. content. pm. activityInfo; import android. graphics. bitmap; import android. graphics. bitmapFactory; import android. graphics. pixelFormat; import android. hardware. camera; import android. OS. bundle; import android. provider. mediaStore; import android. view. surfaceHolder; import android. view. surfaceView; import android. view. view; import android. view. view. onClickListener; import android. widget. relativeLayout;/*** Requirement Description: * use SurfaceView to preview and take a photo. * Save the captured image to the album ** note ** 1 permission * <uses-permission android: name = "android. permission. CAMERA "/> * 2 after the screen is rotated, the preview image in SurfaceView has a 90-degree rotation * solution: * 2.1 set the screen to a portrait * setRequestedOrientation (ActivityInfo. SCREEN_ORIENTATION_PORTRAIT); * 2.2 set the camera's Orientation * mCamera. setDisplayOrientation (90); * Note: * Although this problem is fixed, it may cause the following problems. * 3 the image stored in the photo may be rotated 90 degrees. * solution :* http://blog.csdn.net/walker02/article/details/8211628 * http://www.cnblogs.com/andgoo/archive/2012/08/29/2661896.html * This problem is not solved in this Demo */public class MainActivity extends Activity implements SurfaceHolder. callback, OnClickListener, Camera. pictureCallback {private SurfaceView mSurfaceView; private SurfaceHolder mSurfaceHolder; private Camera mCamera; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); // sets the portrait setRequestedOrientation (ActivityInfo. SCREEN_ORIENTATION_PORTRAIT ); SetContentView (R. layout. main); init ();} private void init () {mSurfaceView = (SurfaceView) findViewById (R. id. surfaceView); mSurfaceView. setFocusable (true); mSurfaceView. setFocusableInTouchMode (true); mSurfaceView. setClickable (true); mSurfaceView. setOnClickListener (this); mSurfaceHolder = mSurfaceView. getHolder (); // set this SurfaceView to a "push" type SurfaceView mSurfaceHolder. setType (SurfaceHolder. SURFACE_TYP E_PUSH_BUFFERS); mSurfaceHolder. addCallback (this);} // 2 parameters. some effects or modes supported by the getSupportedXXX to the camera, such as: // parameters. getSupportedPictureFormats (); // parameters. getSupportedColorEffects (); // parameters. getSupportedFocusModes (); // parameters. getSupportedWhiteBalance (); public void surfaceCreated (SurfaceHolder holder) {final int LARGEST_WIDTH = 200; final int LARGEST_HEIGHT = 200; int bestWidth = 0; int be StHeight = 0; mCamera = Camera. open (); Camera. parameters parameters = mCamera. getParameters (); // operation 1 sets a special effect List for the camera <String> colorEffects = parameters. getSupportedColorEffects (); Iterator <String> iterator1 = colorEffects. iterator (); while (iterator1.hasNext () {String effect = (String) iterator1.next (); if (effect. equals (Camera. parameters. EFFECT_SOLARIZE) {// If the over-exposure effect is supported, set parameters. setColorEffect (Came Ra. parameters. EFFECT_SOLARIZE); break ;}// operation 2 changes the SurfaceView size List <Camera. size> previewSizes = parameters. getSupportedPreviewSizes (); if (previewSizes. size ()> 1) {Iterator <Camera. size> iterator2 = previewSizes. iterator (); while (iterator2.hasNext () {Camera. size size = (Camera. size) iterator2.next (); if (size. width> bestWidth & size. width <= LARGEST_WIDTH & size. height> bestHeight & size. height <= LARGES T_HEIGHT) {bestWidth = size. width; bestHeight = size. height ;}} if (bestWidth! = 0 & bestHeight! = 0) {parameters. setPreviewSize (bestWidth, bestHeight); mSurfaceView. setLayoutParams (new RelativeLayout. layoutParams (bestWidth, bestHeight) ;}// operation 3. Rotation Angle when the screen changes. otherwise, it is not mCamera. setDisplayOrientation (90); // operation end try {// set the camera preview to mSurfaceHolder mCamera. setPreviewDisplay (mSurfaceHolder);} catch (IOException e) {mCamera. release ();} // set the output format parameters. setPictureFormat (PixelFormat. JPEG); // set the camera parameters. otherwise, the preceding settings are invalid. setParameters (parameters); // the camera starts previewing mCamera. startPreview ();} public void surfaceChanged (SurfaceHolder holder, int format, int width, int height) {mCamera. startPreview ();} public void surfaceDestroyed (SurfaceHolder holder) {mCamera. stopPreview (); mCamera. release ();} // process the SurfaceView Click Event public void onClick (View v) {mCamera. takePicture (null, null, this);} public void onPictureTaken (byte [] data, Camera camera) {// Save the image to the album ContentResolver resolver = getContentResolver (); bitmap bitmap = BitmapFactory. decodeByteArray (data, 0, data. length); MediaStore. images. media. insertImage (resolver, bitmap, "t", "des"); // after taking the photo, start previewing camera again. startPreview () ;}} main. xml: [html] <RelativeLayout xmlns: android =" http://schemas.android.com/apk/res/android "Xmlns: tools =" http://schemas.android.com/tools "Android: layout_width =" match_parent "android: layout_height =" match_parent "tools: context = ". mainActivity "> <SurfaceView android: id =" @ + id/surfaceView "android: layout_width =" fill_parent "android: layout_height =" fill_parent "/> </RelativeLayout>