下面是我自己整理的源码,网络上好多不能够运行,或者有bug。
我在emulator android 2.1运行良好,源码注释一定程度能够自我解释
强烈推荐配合adb locat Take2:d *:s查看程序运行函数调用情况

对Activity生命周期不是很理解的,请先看我之前的一片文章
http://menuz.iteye.com/blog/1255320


<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical" android:layout_width="fill_parent"android:layout_height="fill_parent"><Button android:layout_width="fill_parent"android:layout_height="wrap_content" android:id="@+id/cameraButton"android:text="拍照" /><SurfaceView android:id="@+id/surface_camera"android:layout_width="fill_parent" android:layout_height="10dip"android:layout_weight="1"></SurfaceView></LinearLayout>


<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"      package="com.busclient"      android:versionCode="1"      android:versionName="1.0">    <uses-sdk android:minSdkVersion="7" />        <uses-permission android:name="android.permission.CAMERA" /> <!-- 注意必须在application上面-->    <application android:icon="@drawable/icon" android:label="@string/app_name">    <uses-library android:name="com.google.android.maps" />        <activity android:name=".Take2"                  android:label="@string/app_name">            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>    </application>    <uses-permission android:name="android.permission.UPDATE_DEVICE_STATS"/>    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/></manifest>


package com.busclient;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import android.app.Activity;import android.hardware.Camera;import android.hardware.Camera.PictureCallback;import android.os.Bundle;import android.util.Log;import android.view.SurfaceHolder;import android.view.SurfaceView;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;public class Take2 extends Activity implements SurfaceHolder.Callback {    private final static String TAG = "Take2";;    private SurfaceView surfaceView;    private SurfaceHolder surfaceHolder;    private Camera mCam;    private boolean hasStartPreview = false;    private Button btnTakePicture;    // Camera API:    // Call release() to release the camera for use by other applications.    // Applications should release the camera immediately in onPause() (and    // re-open() it in onResume()).    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.takephoto);        Log.d(TAG, "onCreate");        // 取得surfaceView的引用,surface在surfaceView中展现        // 当surfaceView可见时,surface会被创建,实现surfaceCreated进行特定操作        surfaceView = (SurfaceView) findViewById(R.id.surface_camera);        btnTakePicture = (Button) findViewById(R.id.cameraButton);        // 注册按钮实现拍照        btnTakePicture.setOnClickListener(new OnClickListener() {            @Override            public void onClick(View v) {                if (mCam != null) {                    // 调用mCam进行拍照                    // param1 Camera.ShutterCallback                     // param2 Camera.PictureCallback raw 当原始图片数据获得时,会执行PictureCallback                    // param3 Camera.PictureCallback compressed 当压缩数据获得时,会执行PictureCallback                    mCam.takePicture(null, null, pictureCallBack);                }            }        });        // 要操作surface,只有通过surfaceHolder        surfaceHolder = surfaceView.getHolder();        surfaceHolder.addCallback(this);        surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);    }    private PictureCallback pictureCallBack = new Camera.PictureCallback() {        @Override        public void onPictureTaken(byte[] data, Camera camera) {            if (mCam != null) {                if (data != null) {                    File f = new File("/data/data/com.busclient/picture1.jpg");                    FileOutputStream fout = null;                    try {                        if (!f.exists())                            f.createNewFile();                        fout = new FileOutputStream(f);                        fout.write(data);                        fout.close();                        Log.d(TAG, "write success");                                                Thread.sleep(1500);                    } catch (IOException e) {                        e.printStackTrace();                    } catch (InterruptedException e) {                        e.printStackTrace();                    } finally {                        try {                            if (fout != null) {                                fout.close();                                fout = null;                            }                        } catch (Exception e) {                            e.printStackTrace();                        }                    }                }                finish();            }        }    };    // surface只能够由一个线程操作,一旦被操作,其他线程就无法操作surface    @Override    public void surfaceCreated(SurfaceHolder holder) {        Log.d(TAG, "surfaceCreated");        try {            // 必须设置一个初始化的surfaceHolder,若没有surface(由holder操作surface),            // 则camera无法启动预览 (就是一般打开照相机屏幕能够动态显示场景??描述的不够好)            mCam.setPreviewDisplay(surfaceHolder);        } catch (IOException e) {            Log.d(TAG, "error");            mCam.release();            mCam = null;        }    }    // 在surfaceCreated后调用,当surface发生变化也会触发该方法,这个方法    // 一般至少被调用一次    @Override    public void surfaceChanged(SurfaceHolder holder, int format, int width,            int height) {        Log.d(TAG, "surfaceChanged");        // 调用startPreview使预览surface可以更新,拍照        // 必须启动预览,而startPreview必须在setPreviewDisplay(surfaceHolder)之后        if (mCam != null && hasStartPreview == false) {            mCam.startPreview();            hasStartPreview = true;        }    }    // 析构surface    @Override    public void surfaceDestroyed(SurfaceHolder holder) {        Log.d(TAG, "surfaceDestroyed");        // 活都被onPause抢去了    }    @Override    protected void onDestroy() {        Log.d(TAG, "onDestroy");        super.onDestroy();    }    // onPause比surfaceDestroyed() 先调用    @Override    protected void onPause() {        Log.d(TAG, "onPause");        if (hasStartPreview) {            mCam.stopPreview();        }        mCam.release();        mCam = null;        hasStartPreview = false;        super.onPause();    }    @Override    protected void onRestart() {        Log.d(TAG, "onRestart");        super.onRestart();    }    @Override    protected void onResume() {        Log.d(TAG, "onResume");        if (mCam == null)            mCam = Camera.open();        super.onResume();    }    @Override    protected void onStart() {        Log.d(TAG, "onStart");        super.onStart();    }    @Override    protected void onStop() {        Log.d(TAG, "onStop");        super.onStop();    }}

更多相关文章

  1. 快速体验Android(安卓)2.3
  2. android中的Handler(1)
  3. Android设备和PC端通过USB线通信
  4. Android源码学习--SystemServer进程
  5. Android(安卓)源码分析之旅3.1--消息机制源码分析
  6. Android(安卓)sqlite 采用execSQL和rawQuery方法完成数据的添删
  7. Android(安卓)HandlerThread用法
  8. Android期末复习(3)-service
  9. Android(安卓)- 文件读写操作 总结

随机推荐

  1. Android之应用内部实现国际化
  2. Android各文件存储路径汇总
  3. android中添加通过内容提供者添加联系人
  4. android 从媒体库去数据
  5. An Asynchronous HTTP Library for Andro
  6. 怎样实现android http-post方法
  7. TextView本身可以加图片装饰
  8. 使用xml和java代码混合控制UI界面
  9. Android(安卓)Uri.getQueryParameter使用
  10. android自定义适配屏幕的ImageView