转老戴自动对焦camera view

来源:互联网 发布:淘宝cos店推荐 编辑:程序博客网 时间:2024/04/29 17:48
转自http://dai-lm.iteye.com/blog/1586458
Java代码  收藏代码
  1. import java.io.BufferedOutputStream;  
  2. import java.io.File;  
  3. import java.io.FileOutputStream;  
  4. import java.io.IOException;  
  5. import java.lang.reflect.Method;  
  6. import java.util.Date;  
  7.   
  8. import android.content.Context;  
  9. import android.content.pm.ActivityInfo;  
  10. import android.graphics.Bitmap;  
  11. import android.graphics.BitmapFactory;  
  12. import android.hardware.Camera;  
  13. import android.media.MediaPlayer;  
  14. import android.os.Build;  
  15. import android.util.AttributeSet;  
  16. import android.view.SurfaceHolder;  
  17. import android.view.SurfaceView;  
  18.   
  19. public class CameraView extends SurfaceView implements SurfaceHolder.Callback,  
  20.         Camera.AutoFocusCallback, Camera.PictureCallback {  
  21.   
  22.     private TakePictureListener mListener;  
  23.   
  24.     /** 
  25.      * This is a holder that holding a display surface. 
  26.      */  
  27.     private SurfaceHolder mHolder = null;  
  28.   
  29.     private int sdk = 3;  
  30.   
  31.     /** 
  32.      * This is a camera object using for connect/disconnect with the camera 
  33.      * service,and so on. 
  34.      */  
  35.     private Camera mCamera;  
  36.   
  37.     /** 
  38.      * Perform inflation from XML and apply a class-specific base style. This 
  39.      * constructor of View allows subclasses to use their own base style when 
  40.      * they are inflating. 
  41.      *  
  42.      * @param context 
  43.      *            The Context the view is running in, through which it can 
  44.      *            access the current theme, resources, etc. 
  45.      * @param attrs 
  46.      *            The attributes of the XML tag that is inflating the view. 
  47.      * @param defStyle 
  48.      *            The default style to apply to this view. If 0, no style will 
  49.      *            be applied (beyond what is included in the theme). This may 
  50.      *            either be an attribute resource, whose value will be retrieved 
  51.      *            from the current theme, or an explicit style resource. 
  52.      */  
  53.     public CameraView(Context context, AttributeSet attrs, int defStyle) {  
  54.         super(context, attrs, defStyle);  
  55.         init();  
  56.     }  
  57.   
  58.     /** 
  59.      * Constructor that is called when inflating a view from XML. This is called 
  60.      * when a view is being constructed from an XML file, supplying attributes 
  61.      * that were specified in the XML file. This version uses a default style of 
  62.      * 0, so the only attribute values applied are those in the Context's Theme 
  63.      * and the given AttributeSet. 
  64.      *  
  65.      * @param context 
  66.      *            The Context the view is running in, through which it can 
  67.      *            access the current theme, resources, etc. 
  68.      * @param attrs 
  69.      *            The attributes of the XML tag that is inflating the view. 
  70.      */  
  71.     public CameraView(Context context, AttributeSet attrs) {  
  72.         super(context, attrs);  
  73.         init();  
  74.     }  
  75.   
  76.     /** 
  77.      * Simple constructor to use when creating a view from code. 
  78.      *  
  79.      * @param context 
  80.      *            The Context the view is running in, through which it can 
  81.      *            access the current theme, resources, etc. 
  82.      */  
  83.     public CameraView(Context context) {  
  84.         super(context);  
  85.         init();  
  86.     }  
  87.   
  88.     public void init() {  
  89.         if (mHolder == null) {  
  90.             mHolder = getHolder();  
  91.             mHolder.addCallback(this);  
  92.             mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);  
  93.   
  94.             sdk = getSDKInt();  
  95.         }  
  96.     }  
  97.   
  98.     @Override  
  99.     public void surfaceChanged(SurfaceHolder holder, int format, int width,  
  100.             int height) {  
  101.   
  102.         try {  
  103.   
  104.             // rotate camera 90 degree on portrait mode  
  105.             if (getContext().getResources().getConfiguration().orientation == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) {  
  106.   
  107.                 if (sdk <= 4) {  
  108.   
  109.                     // 1.5 & 1.6  
  110.                     Camera.Parameters parameters = mCamera.getParameters();  
  111.                     parameters.set("orientation""portrait");  
  112.                     mCamera.setParameters(parameters);  
  113.                 } else {  
  114.                     setDisplayOrientation(mCamera, 90);  
  115.                 }  
  116.             }  
  117.   
  118.         } catch (Exception e) {  
  119.             e.printStackTrace();  
  120.         }  
  121.   
  122.         startPreview();  
  123.     }  
  124.   
  125.     /** 
  126.      * rotate camera with any degree, only available for SDK 5 and later 
  127.      *  
  128.      * @param camera 
  129.      * @param angle 
  130.      */  
  131.     private void setDisplayOrientation(Camera camera, int angle) {  
  132.         Method downPolymorphic;  
  133.   
  134.         if (sdk <= 4)  
  135.             return;  
  136.   
  137.         try {  
  138.             if (sdk > 4 && sdk < 8) {  
  139.   
  140.                 // parameters for pictures created by a Camera service.  
  141.                 Camera.Parameters parameters = mCamera.getParameters();  
  142.   
  143.                 // 2.0, 2.1  
  144.                 downPolymorphic = parameters.getClass().getMethod(  
  145.                         "setRotation"new Class[] { int.class });  
  146.                 if (downPolymorphic != null)  
  147.                     downPolymorphic.invoke(parameters, new Object[] { angle });  
  148.   
  149.                 // Sets the Parameters for pictures from this Camera  
  150.                 // service.  
  151.                 mCamera.setParameters(parameters);  
  152.   
  153.             } else {  
  154.   
  155.                 downPolymorphic = camera.getClass().getMethod(  
  156.                         "setDisplayOrientation"new Class[] { int.class });  
  157.                 if (downPolymorphic != null)  
  158.                     downPolymorphic.invoke(camera, new Object[] { angle });  
  159.             }  
  160.         } catch (Exception e) {  
  161.         }  
  162.     }  
  163.   
  164.     @Override  
  165.     public void surfaceCreated(SurfaceHolder holder) {  
  166.   
  167.         // get Camera object.  
  168.         try {  
  169.             mCamera = Camera.open();  
  170.             mCamera.setPreviewDisplay(holder);  
  171.   
  172.         } catch (RuntimeException e) {  
  173.             e.printStackTrace();  
  174.             releaseCamera();  
  175.         } catch (IOException e) {  
  176.             e.printStackTrace();  
  177.             releaseCamera();  
  178.         }  
  179.     }  
  180.   
  181.     public void stopPreview() {  
  182.         releaseCamera();  
  183.     }  
  184.   
  185.     public void startPreview() {  
  186.         if (mCamera != null) {  
  187.             mCamera.startPreview();  
  188.             mCamera.autoFocus(this);  
  189.         }  
  190.     }  
  191.   
  192.     @Override  
  193.     public void surfaceDestroyed(SurfaceHolder holder) {  
  194.         releaseCamera();  
  195.     }  
  196.   
  197.     private void releaseCamera() {  
  198.         if (mCamera != null) {  
  199.             mCamera.stopPreview();  
  200.             mCamera.release();  
  201.         }  
  202.         mCamera = null;  
  203.         System.gc();  
  204.     }  
  205.   
  206.     private int getSDKInt() {  
  207.         // this is safe so that we don't need to use SDKInt which is only  
  208.         // available after 1.6  
  209.         try {  
  210.             return Integer.parseInt(Build.VERSION.SDK);  
  211.         } catch (Exception e) {  
  212.             return 3// default to target 1.5 cupcake  
  213.         }  
  214.     }  
  215.   
  216.     @Override  
  217.     public void onAutoFocus(boolean success, Camera camera) {  
  218.         if (success)  
  219.             mCamera.takePicture(nullnullthis);  
  220.     }  
  221.   
  222.     @Override  
  223.     public void onPictureTaken(byte[] data, Camera camera) {  
  224.   
  225.         if (mListener != null) {  
  226.   
  227.             Bitmap cameraBitmap = BitmapFactory.decodeByteArray(data, 0,  
  228.                     data.length);  
  229.             } catch (Exception e) {  
  230.                 e.printStackTrace();  
  231.             }  
  232.   
  233.             mListener.onTakePicture(cameraBitmap);  
  234.         }  
  235.   
  236.         startPreview();  
  237.     }  
  238.   
  239.     public void setTakePictureListener(TakePictureListener listener) {  
  240.         mListener = listener;  
  241.     }  
  242.   
  243.     public interface TakePictureListener {  
  244.   
  245.         public void onTakePicture(Bitmap bitmap);  
  246.     }  


原创粉丝点击