亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費(fèi)注冊 查看新帖 |

Chinaunix

  平臺 論壇 博客 文庫
最近訪問板塊 發(fā)新帖
查看: 1281 | 回復(fù): 0
打印 上一主題 下一主題

看代碼學(xué)Android :如何實(shí)現(xiàn)Timer控制照相機(jī)? [復(fù)制鏈接]

論壇徽章:
0
跳轉(zhuǎn)到指定樓層
1 [收藏(0)] [報(bào)告]
發(fā)表于 2011-12-23 03:03 |只看該作者 |倒序?yàn)g覽
  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.              /* 構(gòu)建Camera.Parameters對相機(jī)的參數(shù)進(jìn)行設(shè)置 */
  143.          //Camera.Parameters parameters = mCamera.getParameters();
  144.          /* 設(shè)置拍照的圖片格式 */
  145.          //parameters.setPictureFormat(PixelFormat.JPEG);
  146.          /* 設(shè)置Preview的尺寸 */
  147.          //parameters.setPreviewSize(320, 480);
  148.          /* 設(shè)置圖像分辨率 */
  149.          //parameters.setPictureSize(320, 480);
  150.          /* 設(shè)置相機(jī)采用parameters */
  151.          //mCamera.setParameters(parameters);
  152.          /* 開始預(yù)覽 */
  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. }
您需要登錄后才可以回帖 登錄 | 注冊

本版積分規(guī)則 發(fā)表回復(fù)

  

北京盛拓優(yōu)訊信息技術(shù)有限公司. 版權(quán)所有 京ICP備16024965號-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報(bào)專區(qū)
中國互聯(lián)網(wǎng)協(xié)會會員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關(guān)心和支持過ChinaUnix的朋友們 轉(zhuǎn)載本站內(nèi)容請注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP