Come from: http://blog.csdn.net/jianghuiquan/article/details/8569187
Android has two ways to take photos: one is to call the system's photo Intent directly, use onActivityResult to get picture resources or specify a picture path, and read the picture after the photo returns successfully; the other is to use SurfaceView to customize the interface and add business personalization function.
1, The first method
1. Design interface
(1) layout documents
Open the activity main.xml file.
Enter the following code:
?
- <?xml?version="1.0"?encoding="utf-8"??>??
- ??
- <LinearLayout???
- ????xmlns:android="http://schemas.android.com/apk/res/android"???
- ????android:orientation="vertical"???
- ????android:layout_width="fill_parent"???
- ????android:layout_height="fill_parent">??
- ??
- ????<Button??
- ????????android:id="@+id/bysystem"??
- ????????android:layout_width="wrap_content"??
- ????????android:layout_height="wrap_content"??
- ????????android:text="Calling system camera does not return results"?/>??
- ??
- ????<Button??
- ????????android:id="@+id/byself"??
- ????????android:layout_width="wrap_content"??
- ????????android:layout_height="wrap_content"??
- ????????android:text="Call the system camera and return the result"?/>??
- ??
- ????<ImageView??
- ????????android:id="@+id/photo"??
- ????????android:layout_width="wrap_content"??
- ????????android:layout_height="wrap_content"?/>??
- ??
- </LinearLayout>??
2. Procedure documents
Open the "src/com.genwoxue.camera/MainActivity.java" file.
Then enter the following code:
- package?com.genwoxue.camera;??
- ??
- ??
- import?java.io.File;??
- import?android.app.Activity;??
- import?android.content.Intent;??
- import?android.net.Uri;??
- import?android.os.Bundle;??
- import?android.os.Environment;??
- import?android.provider.MediaStore;??
- import?android.view.View;??
- import?android.view.View.OnClickListener;??
- import?android.widget.Button;??
- import?android.widget.Toast;??
- ??
- public?class?MainActivity?extends?Activity?{??
- ??????
- ????private?Button?btnSystem=null;??
- ????private?Button?btnSelf=null;??
- ????private?File?file=null;???
- ????private?static?final?String?FILENAME="photo.jpg";??
- ??????
- ????private?static?String?path="";??
- ??
- ????@Override??
- ????public?void?onCreate(Bundle?savedInstanceState)?{??
- ????????super.onCreate(savedInstanceState);??
- ????????setContentView(R.layout.activity_main);??
- ??????????
- ????????btnSystem=(Button)super.findViewById(R.id.bysystem);??
- ????????btnSelf=(Button)super.findViewById(R.id.byself);??
- ??????????
- ????????//Call system camera, no result returned??
- ????????btnSystem.setOnClickListener(new?OnClickListener(){??
- ????????????public?void?onClick(View?v)??
- ????????????{???
- ????????????????Intent?intent?=?new?Intent();????
- ????????????????intent.setAction("android.media.action.STILL_IMAGE_CAMERA");???
- ????????????????startActivity(intent);???
- ????????????}??
- ????????});??
- ??????????
- ????????//Call the system camera and return the result??
- ????????btnSelf.setOnClickListener(new?OnClickListener(){??
- ????????????public?void?onClick(View?v)??
- ????????????{????
- ????????????????//Determine whether the external memory card exists??
- ????????????????if(!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){??
- ????????????????????Toast.makeText(getApplicationContext(),?"Read failed, SD Memory card does not exist!",?Toast.LENGTH_LONG).show();????
- ????????????????????return;??
- ????????????????}??
- ??????????????????
- ????????????????//Judge whether the file exists??
- ????????????????path=Environment.getExternalStorageDirectory().toString()+File.separator+"genwoxue"+File.separator+FILENAME;??
- ????????????????file=new?File(path);??
- ????????????????if(!file.exists()){??
- ????????????????????File?vDirPath?=?file.getParentFile();???
- ????????????????????vDirPath.mkdirs();???
- ????????????????????Toast.makeText(getApplicationContext(),?"photo.jpg File does not exist!",?Toast.LENGTH_LONG).show();????
- ????????????????????return;??
- ????????????????}??
- ??????????????????
- ????????????????Uri?uri?=?Uri.fromFile(file);???
- ????????????????Intent?intent?=?new?Intent(MediaStore.ACTION_IMAGE_CAPTURE);???
- ????????????????intent.putExtra(MediaStore.EXTRA_OUTPUT,?uri);??
- ????????????????startActivityForResult(intent,?1);???
- ??????????????????
- ????????????}??
- ????????});??
- ??????????
- ????}??
- ??????
- }??
? 3. Operation results
?
2, The second method.
1. Design interface
(1) layout documents
Open the activity main.xml file.
Enter the following code:
- <?xml?version="1.0"?encoding="utf-8"??>??
- ??
- <LinearLayout???
- ????xmlns:android="http://schemas.android.com/apk/res/android"???
- ????android:orientation="vertical"???
- ????android:layout_width="fill_parent"???
- ????android:layout_height="fill_parent">??
- ??
- ??
- ????<Button??
- ????????android:id="@+id/byself"??
- ????????android:layout_width="wrap_content"??
- ????????android:layout_height="wrap_content"??
- ????????android:text="Take a picture (custom camera)"?/>??
- ??????
- ????<SurfaceView??
- ????????android:id="@+id/photo"??
- ????????android:layout_width="300dip"??
- ????????android:layout_height="400dip"?/>??
- ??
- </LinearLayout>??
2. Procedure documents
Open the "Src / com. Genwoxue. Camera / mainactivity. Java" file.
Then enter the following code:
- package?com.genwoxue.cameradiy;??
- ??
- ??
- import?java.io.BufferedOutputStream;??
- import?java.io.File;??
- import?java.io.FileNotFoundException;??
- import?java.io.FileOutputStream;??
- import?java.io.IOException;??
- import?android.app.Activity;??
- import?android.graphics.Bitmap;??
- import?android.graphics.BitmapFactory;??
- import?android.graphics.PixelFormat;??
- import?android.hardware.Camera;??
- import?android.hardware.Camera.AutoFocusCallback;??
- import?android.hardware.Camera.Parameters;??
- import?android.hardware.Camera.PictureCallback;??
- import?android.hardware.Camera.ShutterCallback;??
- import?android.os.Bundle;??
- import?android.os.Environment;??
- import?android.util.Log;??
- import?android.view.SurfaceHolder;??
- import?android.view.SurfaceView;??
- import?android.view.View;??
- import?android.view.View.OnClickListener;??
- import?android.widget.Button;??
- import?android.widget.Toast;??
- ??
- public?class?MainActivity?extends?Activity?{??
- ??????
- ????private?Button?btnSelf=null;??
- ????private?Camera?camera=null;??
- ????private?static?final?String?TAG="PhotoDIY";??
- ????private?String?path="";??
- ????private?boolean?previewRuning=true;??
- ??
- ????@Override??
- ????public?void?onCreate(Bundle?savedInstanceState)?{??
- ????????super.onCreate(savedInstanceState);??
- ????????setContentView(R.layout.activity_main);??
- ??????????
- ????????//Initialize SurfaceView??
- ????????SurfaceView?mpreview?=?(SurfaceView)?this.findViewById(R.id.photo);???
- ????????SurfaceHolder?mSurfaceHolder?=?mpreview.getHolder();???
- ????????mSurfaceHolder.addCallback(new?SurfaceViewCallback());???
- ????????mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);???
- ??
- ??????????
- ????????btnSelf=(Button)super.findViewById(R.id.byself);??
- ??????????
- ????????//Take a picture (custom camera)??
- ????????btnSelf.setOnClickListener(new?OnClickListener(){??
- ????????????public?void?onClick(View?v)??
- ????????????{????
- ????????????????if(camera!=null){??
- ????????????????????camera.autoFocus(new?AutoFocusCallbackimpl());??
- ????????????????}??
- ????????????}??
- ????????});??
- ??????????
- ????}??
- ??????
- ????public?class?SurfaceViewCallback?implements?SurfaceHolder.Callback{??
- ??????????
- ????????@Override??
- ????????public?void?surfaceChanged(SurfaceHolder?holder,int?format,int?width,int?heith){??
- ??
- ????????}??
- ??????????
- ????????@Override??
- ????????public?void?surfaceCreated(SurfaceHolder?holder){??
- ????????????//Now the smart machine may have multiple lenses: generally, the front is 1; the back is 0??
- ????????????MainActivity.this.camera=Camera.open(0);??
- ????????????//Setting parameters??
- ????????????Parameters?param=camera.getParameters();??
- ????????????param.setPictureFormat(PixelFormat.JPEG);??
- ????????????param.set("jpeg-quality",85);??
- ????????????param.setPreviewFrameRate(5);??
- ????????????camera.setParameters(param);??
- ??????????????
- ????????????try?{??
- ????????????????camera.setPreviewDisplay(holder);???//Imaging in SurfaceView??
- ????????????}?catch?(IOException?e)?{??
- ????????????????e.printStackTrace();??
- ????????????}??
- ??????????????
- ????????????//Start Preview??
- ????????????camera.startPreview();??
- ????????????previewRuning=true;??
- ????????}??
- ??????????
- ????????@Override??
- ????????public?void?surfaceDestroyed(SurfaceHolder?holder){??
- ????????????if(camera!=null){??
- ????????????????if(previewRuning){??
- ????????????????????camera.stopPreview();??
- ????????????????????previewRuning=false;??
- ????????????????}??
- ????????????????camera.release();??
- ????????????}??
- ????????}??
- ????}??
- ??????
- ????//When the takePicture() method is called, the pictureCallback method is automatically executed??
- ????public?PictureCallback?picture=new?PictureCallback(){??
- ????????@Override??
- ????????public?void?onPictureTaken(byte[]?data,Camera?camera){????????
- ????????????Bitmap?bmp=BitmapFactory.decodeByteArray(data,?0,?data.length);??
- ????????????//Determine whether the external memory card exists??
- ????????????if(!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){??
- ????????????????Toast.makeText(getApplicationContext(),?"Read failed, SD Memory card does not exist!",?Toast.LENGTH_LONG).show();????
- ????????????????return;??
- ????????????}??
- ??????????????
- ????????????//Judge whether the file exists??
- ????????????path=Environment.getExternalStorageDirectory().toString()??
- ????????????????????+File.separator??
- ????????????????????+"genwoxue"??
- ????????????????????+File.separator??
- ????????????????????+System.currentTimeMillis()??
- ????????????????????+".jpg";??
- ??????????????
- ????????????File?file=new?File(path);??
- ????????????if(!file.exists()){??
- ????????????????File?vDirPath?=?file.getParentFile();???
- ????????????????vDirPath.mkdirs();???
- ????????????????Toast.makeText(getApplicationContext(),?"photo.jpg File does not exist!",?Toast.LENGTH_LONG).show();????
- ????????????????return;??
- ????????????}??
- ??????????????
- ????????????try?{??
- ????????????????BufferedOutputStream?bos=new?BufferedOutputStream(new?FileOutputStream(file));??
- ????????????????bmp.compress(Bitmap.CompressFormat.JPEG,?80,?bos);??
- ????????????????try?{??
- ????????????????????bos.flush();??
- ????????????????????bos.close();??
- ????????????????}?catch?(IOException?e)?{??
- ????????????????????e.printStackTrace();??
- ????????????????}??
- ??????????????????
- ????????????}?catch?(FileNotFoundException?e)?{??
- ????????????????e.printStackTrace();??
- ????????????}??
- ??????????????
- ????????????camera.stopPreview();??
- ????????????camera.startPreview();??
- ??????????????
- ????????}??
- ????};??
- ??
- ????//Focus back callback??
- ????public?class?AutoFocusCallbackimpl?implements?AutoFocusCallback{??
- ????????public?void?onAutoFocus(boolean?success,Camera?camera){??
- ??????????????
- ????????????if(success){??
- ????????????????camera.takePicture(shutter,?null,?picture);??
- ????????????????camera.stopPreview();??
- ????????????}??
- ????????}??
- ????}??
- ??????
- ????//Shutter callback??
- ????public?ShutterCallback?shutter=new?ShutterCallback(){??
- ????????public?void?onShutter(){??
- ??????????????
- ????????}??
- ????};??
- }??
3. Operation results