Android · 2014年8月24日 0

Android开发之拍照功能实现(附源码)

大家好,这是一个简单的拍照功能,很简单的界面,一个显示图像区域SurfaceView一个“拍照”按钮。直接上代码!

1、CameraDemoActivity.java(主界面)

  1. package org.winplus.camera;  
  2.   
  3. import java.io.File;  
  4. import java.io.FileOutputStream;  
  5. import java.io.IOException;  
  6. import java.util.Date;  
  7.   
  8. import android.app.Activity;  
  9. import android.content.pm.ActivityInfo;  
  10. import android.graphics.PixelFormat;  
  11. import android.hardware.Camera;  
  12. import android.os.AsyncTask;  
  13. import android.os.Bundle;  
  14. import android.os.Environment;  
  15. import android.text.format.DateFormat;  
  16. import android.util.Log;  
  17. import android.view.KeyEvent;  
  18. import android.view.SurfaceHolder;  
  19. import android.view.SurfaceView;  
  20. import android.view.View;  
  21. import android.view.Window;  
  22. import android.view.View.OnClickListener;  
  23. import android.widget.Button;  
  24.   
  25. public class CameraDemoActivity extends Activity{  
  26.   
  27.     private final static String TAG = “CameraActivity”;  
  28.     private SurfaceView surfaceView;  
  29.     private SurfaceHolder surfaceHolder;  
  30.     private Camera camera;  
  31.     private File picture;  
  32.     private Button btnSave;  
  33.       
  34.     @Override  
  35.     public void onCreate(Bundle savedInstanceState) {  
  36.         super.onCreate(savedInstanceState);  
  37.         setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);  
  38.         this.requestWindowFeature(Window.FEATURE_NO_TITLE);  
  39.         setContentView(R.layout.main);  
  40.         setupViews();  
  41.     }  
  42.       
  43.     private void setupViews(){  
  44.         surfaceView = (SurfaceView) findViewById(R.id.camera_preview); // Camera interface to instantiate components  
  45.         surfaceHolder = surfaceView.getHolder(); // Camera interface to instantiate components  
  46.         surfaceHolder.addCallback(surfaceCallback); // Add a callback for the SurfaceHolder  
  47.         surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);  
  48.           
  49.         btnSave = (Button) findViewById(R.id.save_pic);  
  50.           
  51.         btnSave.setOnClickListener(new OnClickListener() {  
  52.               
  53.             @Override  
  54.             public void onClick(View v) {  
  55.                 takePic();  
  56.             }  
  57.         });  
  58.     }  
  59.       
  60.       
  61.     @Override  
  62.     public boolean onKeyDown(int keyCode, KeyEvent event) {  
  63.         if (keyCode == KeyEvent.KEYCODE_CAMERA  
  64.                 || keyCode == KeyEvent.KEYCODE_SEARCH) {  
  65.             takePic();  
  66.             return true;  
  67.         }  
  68.         return super.onKeyDown(keyCode, event);  
  69.     }  
  70.   
  71.     private void takePic() {  
  72.   
  73.         camera.stopPreview();// stop the preview  
  74.   
  75.         camera.takePicture(nullnull, pictureCallback); // picture  
  76.     }  
  77.   
  78.     // Photo call back  
  79.     Camera.PictureCallback pictureCallback = new Camera.PictureCallback() {  
  80.         //@Override  
  81.         public void onPictureTaken(byte[] data, Camera camera) {  
  82.             new SavePictureTask().execute(data);  
  83.             camera.startPreview();  
  84.         }  
  85.     };  
  86.   
  87.     // save pic  
  88.     class SavePictureTask extends AsyncTask<byte[], String, String> {  
  89.         @Override  
  90.         protected String doInBackground(byte[]… params) {  
  91.             String fname = DateFormat.format(“yyyyMMddhhmmss”new Date()).toString()+“.jpg”;  
  92.               
  93.             Log.i(TAG, “fname=”+fname+“;dir=”+Environment.getExternalStorageDirectory());  
  94.             //picture = new File(Environment.getExternalStorageDirectory(),fname);// create file  
  95.               
  96.             picture = new File(Environment.getExternalStorageDirectory()+“/”+fname);  
  97.               
  98.             try {  
  99.                 FileOutputStream fos = new FileOutputStream(picture.getPath()); // Get file output stream  
  100.                 fos.write(params[0]); // Written to the file  
  101.                 fos.close();   
  102.             } catch (Exception e) {  
  103.                 e.printStackTrace();  
  104.             }  
  105.             return null;  
  106.         }  
  107.     }  
  108.   
  109.     // SurfaceHodler Callback handle to open the camera, off camera and photo size changes  
  110.     SurfaceHolder.Callback surfaceCallback = new SurfaceHolder.Callback() {  
  111.   
  112.         public void surfaceCreated(SurfaceHolder holder) {  
  113.             Log.i(TAG, “surfaceCallback====”);  
  114.             camera = Camera.open(); // Turn on the camera  
  115.             try {  
  116.                 camera.setPreviewDisplay(holder); // Set Preview  
  117.             } catch (IOException e) {  
  118.                 camera.release();// release camera  
  119.                 camera = null;  
  120.             }  
  121.         }  
  122.   
  123.         public void surfaceChanged(SurfaceHolder holder, int format, int width,  
  124.                 int height) {  
  125.             Log.i(TAG,“====surfaceChanged”);  
  126.             Camera.Parameters parameters = camera.getParameters(); // Camera parameters to obtain  
  127.             parameters.setPictureFormat(PixelFormat.JPEG);// Setting Picture Format  
  128. //          parameters.set(“rotation”, 180); // Arbitrary rotation  
  129.             camera.setDisplayOrientation(0);  
  130. //          parameters.setPreviewSize(400, 300); // Set Photo Size  
  131.             camera.setParameters(parameters); // Setting camera parameters  
  132.             camera.startPreview(); // Start Preview  
  133.         }  
  134.   
  135.         public void surfaceDestroyed(SurfaceHolder holder) {  
  136.             Log.i(TAG,“====surfaceDestroyed”);  
  137.             camera.stopPreview();// stop preview  
  138.             camera.release(); // Release camera resources  
  139.             camera = null;  
  140.         }  
  141.     };  
  142. }  

2、main.xml(布局文件)

  1. <?xml version=“1.0” encoding=“utf-8”?>  
  2. <LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android”  
  3.     android:layout_width=“fill_parent”  
  4.     android:layout_height=“fill_parent”  
  5.     android:orientation=“vertical” >  
  6.   
  7.     <android.view.SurfaceView  
  8.         android:id=“@+id/camera_preview”  
  9.         android:layout_width=“800dip”  
  10.         android:layout_height=“600dip”  
  11.         android:layout_alignParentTop=“true”  
  12.         android:layout_centerInParent=“true”  
  13.         android:layout_gravity=“center_vertical|center_horizontal” />  
  14.   
  15.     <Button  
  16.         android:id=“@+id/save_pic”  
  17.         android:layout_width=“fill_parent”  
  18.         android:layout_height=“wrap_content”  
  19.         android:text=“@string/txt_save” />  
  20.   
  21. </LinearLayout>  


3、别忘了添加权限:

  1. <uses-permission android:name=“android.permission.CAMERA” />  
  2.   
  3. <uses-feature android:name=“android.hardware.camera” />  
  4. <uses-feature android:name=“android.hardware.camera.autofocus” android:required=“false” />  
  1.   

源码下载==》

原创文章,转载请注明出处:http://www.blog.csdn.net/tangcheng_ok

Share this: