Chinaunix首页 | 论坛 | 博客
  • 博客访问: 137449
  • 博文数量: 38
  • 博客积分: 1605
  • 博客等级: 上尉
  • 技术积分: 370
  • 用 户 组: 普通用户
  • 注册时间: 2006-04-12 14:59
文章分类

全部博文(38)

文章存档

2013年(3)

2012年(8)

2011年(28)

分类: 嵌入式

2011-09-22 14:09:29

  1. package com.yarin.android.Examples_07_06;

  2. import java.util.Timer;
  3. import java.util.TimerTask;

  4. import android.app.Activity;
  5. import android.content.Intent;
  6. import android.os.Bundle;
  7. import android.util.Log;
  8. import android.view.View;
  9. import android.view.Window;

  10. public class Activity01 extends Activity
  11. {

  12.     /* (non-Javadoc)
  13.      * @see android.app.Activity#onDestroy()
  14.      */
  15.     @Override
  16.     protected void onDestroy() {
  17.         if( timer != null){
  18.             timer.cancel();
  19.         }
  20.         // TODO Auto-generated method stub
  21.         super.onDestroy();
  22.     }

  23.     /* (non-Javadoc)
  24.      * @see android.app.Activity#onResume()
  25.      */
  26.     @Override
  27.     protected void onResume() {        
  28.         // TODO Auto-generated method stub
  29.         super.onResume();            
  30.     }

  31.     Timer timer = new Timer();
  32.     
  33.     /** Called when the activity is first created. */
  34.     @Override
  35.     public void onCreate(Bundle savedInstanceState)
  36.     {
  37.         super.onCreate(savedInstanceState);
  38.         requestWindowFeature(Window.FEATURE_NO_TITLE);
  39.         
  40.         setContentView(R.layout.main);
  41.     
  42.     }

  43.     public void Test(View view)
  44.     {        
  45.         timer.schedule(new TimerTask()
  46.         {             
  47.      public void run() {     
  48.          Log.v("CA", "TimerTask");
  49.          Intent intent = new Intent();
  50.          intent.setClass(Activity01.this, CameraActivity.class);
  51.          Activity01.this.startActivity(intent);    
  52.      }
  53.         }, 5000, 2000);
  54.     }
  55.     
  56.     public void Stop(View view)
  57.     {
  58.         this.timer.cancel();
  59.         this.finish();
  60.     }
  61. }

  1. package com.yarin.android.Examples_07_06;

  2. import java.io.BufferedOutputStream;
  3. import java.io.File;
  4. import java.io.FileOutputStream;
  5. import java.io.IOException;

  6. import android.app.Activity;
  7. import android.content.Context;
  8. import android.graphics.Bitmap;
  9. import android.graphics.BitmapFactory;
  10. import android.graphics.Canvas;
  11. import android.graphics.PixelFormat;
  12. import android.hardware.Camera;
  13. import android.hardware.Camera.PictureCallback;
  14. import android.os.Bundle;
  15. //import android.view.KeyEvent;
  16. import android.util.Log;
  17. import android.view.SurfaceHolder;
  18. import android.view.SurfaceView;
  19. import android.view.Window;

  20. public class CameraActivity extends Activity {

  21.     private Preview    mPreview = null;
  22.     
  23.     /* (non-Javadoc)
  24.      * @see android.app.Activity#onPause()
  25.      */
  26.     @Override
  27.     protected void onPause() {
  28.         Log.v("CA", "onPause");
  29.         if( mPreview != null ){            
  30.             mPreview = null;
  31.         }
  32.         
  33.         super.onPause();
  34.     }

  35.     /* (non-Javadoc)
  36.      * @see android.app.Activity#onResume()
  37.      */
  38.     @Override
  39.     protected void onResume() {
  40.         // Create our Preview view and set it as the content of our activity.
  41.         Log.v("CA", "onResume");
  42.         
  43.         super.onResume();
  44.     }
  45.     

  46.     /** Called when the activity is first created. */
  47.     @Override
  48.     public void onCreate(Bundle savedInstanceState)
  49.     {
  50.         super.onCreate(savedInstanceState);
  51.         requestWindowFeature(Window.FEATURE_NO_TITLE);
  52.         
  53.         mPreview = new Preview(this);
  54.         setContentView(mPreview);        
  55.     }



  56. /* Preview-显示Preview */
  57. class Preview extends SurfaceView implements SurfaceHolder.Callback
  58. {
  59.     SurfaceHolder mHolder = null;
  60.     Camera mCamera = null;
  61.     Bitmap CameraBitmap = null;
  62.     
  63.     Preview(Context context)
  64.     {
  65.         super(context);
  66.         mHolder = getHolder();
  67.         mHolder.addCallback(this);
  68.         mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
  69.     }
  70.     
  71.     public void startCamera() {
  72.         Log.v("CA", "startCamera");
  73.         
  74.         if (mCamera == null) {
  75.             try {
  76.                 mCamera = Camera.open();
  77.                 Log.v("CA", "Camera.open");
  78.             } catch (Exception e) {
  79.             }
  80.             
  81.         } else {
  82.             try {
  83.                 mCamera.reconnect();
  84.             } catch (IOException e) {
  85.                 mCamera.release();
  86.                 mCamera = null;
  87.             }
  88.         }
  89.         
  90.     }

  91.     public void stopCamera() {
  92.         Log.v("CA", "stopCamera");
  93.         
  94.         if (mCamera != null) {
  95.             try {
  96.                 mCamera.stopPreview();
  97.             } catch (Exception e) {
  98.                 // Ignore
  99.             }

  100.             try {
  101.                 mCamera.release();
  102.                 Log.v("CA", "mCamera.release");
  103.             } catch (Exception e) {
  104.                 // Ignore
  105.             }

  106.             mCamera = null;
  107.         }
  108.     }

  109.     @Override
  110.     public void surfaceCreated(SurfaceHolder holder)
  111.     {
  112.         Log.v("CA", "surfaceCreated");
  113.         
  114.         startCamera();
  115.         
  116.         if( mCamera == null ) return;
  117.         
  118.         Log.v("CA", "Camera create!");
  119.         
  120.         try {
  121.             mCamera.setPreviewDisplay(holder);
  122.         } catch (IOException e) {
  123.             mCamera.release();
  124.             mCamera = null;
  125.             e.printStackTrace();
  126.         }             
  127.     }

  128.     @Override
  129.     public void surfaceDestroyed(SurfaceHolder holder)
  130.     {
  131.         Log.v("CA", "surfaceDestroyed");
  132.         stopCamera();
  133.     }
  134.     
  135.     @Override
  136.     public void surfaceChanged(SurfaceHolder holder, int format, int w, int h)
  137.     {
  138.         Log.v("CA", "surfaceChanged");
  139.         if( holder.isCreating() && mCamera != null ){
  140.             
  141.             if( mCamera != null ){
  142.              /* 构建Camera.Parameters对相机的参数进行设置 */
  143.          //Camera.Parameters parameters = mCamera.getParameters();
  144.          /* 设置拍照的图片格式 */
  145.          //parameters.setPictureFormat(PixelFormat.JPEG);
  146.          /* 设置Preview的尺寸 */
  147.          //parameters.setPreviewSize(320, 480);
  148.          /* 设置图像分辨率 */
  149.          //parameters.setPictureSize(320, 480);
  150.          /* 设置相机采用parameters */
  151.          //mCamera.setParameters(parameters);
  152.          /* 开始预览 */
  153.          mCamera.startPreview();    
  154.          
  155.          CameraActivity.this.finish();
  156.             }
  157.         }
  158.     }
  159.     /* 拍照片 */
  160.     public void takePicture()
  161.     {
  162.       if (mCamera != null)
  163.       {
  164.          mCamera.takePicture(null, null, jpegCallback);
  165.       }
  166.     }
  167.     /* 拍照后输出图片 */
  168.     private PictureCallback jpegCallback = new PictureCallback()
  169.     {
  170.       public void onPictureTaken(byte[] _data, Camera _camera)
  171.       {
  172.         // TODO Handle JPEG image data
  173.         CameraBitmap = BitmapFactory.decodeByteArray(_data, 0, _data.length);
  174.         File myCaptureFile = new File("/sdcard/camera1.jpg");
  175.         try
  176.         {
  177.           BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(myCaptureFile));
  178.           CameraBitmap.compress(Bitmap.CompressFormat.JPEG, 80, bos);
  179.           bos.flush();
  180.           bos.close();
  181.           /* 将拍到的图片绘制出来 */
  182.           Canvas canvas= mHolder.lockCanvas();
  183.           canvas.drawBitmap(CameraBitmap, 0, 0, null);
  184.           mHolder.unlockCanvasAndPost(canvas);
  185.           
  186.         }
  187.         catch (Exception e)
  188.         {
  189.             e.getMessage();
  190.         }
  191.       }
  192.     };
  193. }

  194. }
阅读(1596) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~