二维码一

  1. import java.io.ByteArrayOutputStream;  

  2. import java.io.IOException;  

  3. import java.util.Timer;  

  4. import java.util.TimerTask;  

  5.   

  6. import android.app.Activity;  

  7. import android.content.Context;  

  8. import android.graphics.Bitmap;  

  9. import android.graphics.BitmapFactory;  

  10. import android.graphics.ImageFormat;  

  11. import android.graphics.Rect;  

  12. import android.graphics.YuvImage;  

  13. import android.hardware.Camera;  

  14. import android.os.Bundle;  

  15. import android.util.Log;  

  16. import android.view.Display;  

  17. import android.view.SurfaceHolder;  

  18. import android.view.SurfaceView;  

  19. import android.view.WindowManager;  

  20. import android.widget.ImageView;  

  21.   

  22. public class CameraTestActivity extends Activity implements SurfaceHolder.Callback {  

  23.     private static String TAG = CameraTestActivity.class.getSimpleName();  

  24.     private SurfaceHolder surfaceHolder;  

  25.     private Camera camera;  

  26.     private ImageView imageView;  

  27.     private Timer mTimer;  

  28.     private TimerTask mTimerTask;  

  29.   

  30.     private Camera.AutoFocusCallback mAutoFocusCallBack;  

  31.     private Camera.PreviewCallback previewCallback;  

  32.   

  33.     /** Called when the activity is first created. */  

  34.     @Override  

  35.     public void onCreate(Bundle savedInstanceState) {  

  36.         super.onCreate(savedInstanceState);  

  37.         setContentView(R.layout.main);  

  38.         SurfaceView surfaceView = (SurfaceView) findViewById(R.id.preview_view);  

  39.         imageView = (ImageView) findViewById(R.id.image_view);  

  40.         surfaceHolder = surfaceView.getHolder();  

  41.         surfaceHolder.addCallback(this);  

  42.         surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);  

  43.         mAutoFocusCallBack = new Camera.AutoFocusCallback() {  

  44.             @Override  

  45.             public void onAutoFocus(boolean success, Camera camera) {  

  46.                 if (success) {  

  47.                     // isAutoFocus = true;  

  48.                     camera.setOneShotPreviewCallback(previewCallback);  

  49.                     Log.d(TAG, "onAutoFocus success");  

  50.                 }  

  51.             }  

  52.         };  

  53.   

  54.         previewCallback = new Camera.PreviewCallback() {  

  55.             @Override  

  56.             public void onPreviewFrame(byte[] data, Camera arg1) {  

  57.                 if (data != null)  

  58.                 {  

  59.                     Camera.Parameters parameters = camera.getParameters();  

  60.                     int imageFormat = parameters.getPreviewFormat();  

  61.                     Log.i("map", "Image Format: " + imageFormat);  

  62.   

  63.                     Log.i("CameraPreviewCallback", "data length:" + data.length);  

  64.                     if (imageFormat == ImageFormat.NV21)  

  65.                     {  

  66.                         // get full picture  

  67.                         Bitmap image = null;  

  68.                         int w = parameters.getPreviewSize().width;  

  69.                         int h = parameters.getPreviewSize().height;  

  70.                             

  71.                         Rect rect = new Rect(0, 0, w, h);   

  72.                         YuvImage img = new YuvImage(data, ImageFormat.NV21, w, h, null);  

  73.                         ByteArrayOutputStream baos = new ByteArrayOutputStream();  

  74.                         if (img.compressToJpeg(rect, 100, baos))   

  75.                         {   

  76.                             image =  BitmapFactory.decodeByteArray(baos.toByteArray(), 0, baos.size());  

  77.                             imageView.setImageBitmap(image);  

  78.                         }  

  79.                   

  80.                     }  

  81.                 }  

  82.             }  

  83.         };  

  84.   

  85.         mTimer = new Timer();  

  86.         mTimerTask = new CameraTimerTask();  

  87.         mTimer.schedule(mTimerTask, 0, 500);  

  88.     }  

  89.   

  90.     @Override  

  91.     public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {  

  92.         // TODO Auto-generated method stub  

  93.     }  

  94.   

  95.     @Override  

  96.     public void surfaceCreated(SurfaceHolder arg0) {  

  97.         // TODO Auto-generated method stub  

  98.         initCamera();  

  99.     }  

  100.   

  101.     @Override  

  102.     public void surfaceDestroyed(SurfaceHolder arg0) {  

  103.         // TODO Auto-generated method stub  

  104.         if (camera != null) {  

  105.             camera.stopPreview();  

  106.             camera.release();  

  107.             camera = null;  

  108.         }  

  109.         previewCallback = null;  

  110.         mAutoFocusCallBack = null;  

  111.     }  

  112.   

  113.     public void initCamera() {  

  114.         camera = Camera.open();  

  115.         Camera.Parameters parameters = camera.getParameters();  

  116.         WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE); // 获取当前屏幕管理器对象  

  117.         Display display = wm.getDefaultDisplay(); // 获取屏幕信息的描述类  

  118.         parameters.setPreviewSize(display.getWidth(), display.getHeight());  

  119.         camera.setParameters(parameters);  

  120.         try {  

  121.             camera.setPreviewDisplay(surfaceHolder);  

  122.         } catch (IOException e) {  

  123.             System.out.println(e.getMessage());  

  124.         }  

  125.         camera.startPreview();  

  126.     }  

  127.   

  128.     class CameraTimerTask extends TimerTask {  

  129.         @Override  

  130.         public void run() {  

  131.             if (camera != null) {  

  132.                 camera.autoFocus(mAutoFocusCallBack);  

  133.             }  

  134.         }  

  135.     }  



你可能感兴趣的:(二维码一)