android Camera开发-手机拍照流程

泡在网上的日子 / 文 发表于2013-07-09 00:22 次阅读 Camera

android 拍照API流程


1. 在布局文件中添加一个 surfaceView (摄影平面)

2.根据 SurfaceView  获得 Holder (固定器)

3.给固定器设置 SurfaceHolder.Callback ; 这个 Callback 有一系列的周期方法,

比如:surfaceCreated,surfaceChanged,surfaceDestroyed等方法.

①.在surfaceDestroyed 中 打开相机,设置预览效果 ;

②.在surfaceChanged中可以初始化相机( Camera );比如相机的参数: 像素, 大小,格式,开始预览,等等

③.在surfaceDestroyed可以可以停止相机拍摄;释放相机资源

4. 相机自动对焦 的回调处理

Camera.autoFocus( AutoFocusCallback )

AutoFocusCallback 有个接口方法
 public void onAutoFocus(boolean focused, Camera camera) {
                /* 对到焦点拍照 */
                if (focused) {
                        takePicture();//见第5步
                }
        }

5.Camera.takePicture(ShutterCallback shutter/*摄像*/, PictureCallback raw/*拍照*/, PictureCallback jpeg/*照片*/)

查看API或者源码,可以发现,这些callback里面都有包含摄像,或者被拍照时的byte[]数据,而这些数据可以根据BitmapFactory变成Bitmap图片.


见源代码如下:

public class ScanBarZBarActivity extends Activity implements SurfaceHolder.Callback{ 
    private static String TAG = "ScanBarZBarActivity"; 
     private Camera mCamera; 
    private Button mButton, mButton1, mButton2; 
    private SurfaceView mSurfaceView; 
    private SurfaceHolder holder; 
    private AutoFocusCallback mAutoFocusCallback = new AutoFocusCallback(); 
    private String path = "WXJSMyPhoto"; 
    private String path1; 
    private Bitmap bmp; 
    private int cnt = 1; 
    private Calendar c; 
    public native String getISBN(Bitmap bmp); 
    private int flag=5; 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
            super.onCreate(savedInstanceState); 
            /* 隐藏状态栏 */ 
            this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
                            WindowManager.LayoutParams.FLAG_FULLSCREEN); 
            /* 隐藏标题栏 */ 
            requestWindowFeature(Window.FEATURE_NO_TITLE); 
            /* 设定屏幕显示为横向 */ 
            this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); 
      
            setContentView(R.layout.camera); 
            /* SurfaceHolder设定 */ 
            mSurfaceView = (SurfaceView) findViewById(R.id.mSurfaceView); 
            holder = mSurfaceView.getHolder(); 
            holder.addCallback(ScanBarZBarActivity.this); 
            holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); 
            c = Calendar.getInstance(); 
      
            /* Button初始化 */ 
            mButton = (Button) findViewById(R.id.myButton); 
            mButton1 = (Button) findViewById(R.id.myButton1); 
            mButton2 = (Button) findViewById(R.id.myButton2); 
            /* 拍照Button的事件处理 */ 
            mButton.setOnClickListener(new Button.OnClickListener() { 
                    @Override 
                    public void onClick(View arg0) { 
                            /* 告动对焦后拍照 */ 
                            mCamera.autoFocus(mAutoFocusCallback); 
                    } 
            }); 
            /* Button的事件处理 */ 
            mButton1.setOnClickListener(new Button.OnClickListener() { 
                    @Override 
                    public void onClick(View arg0) { 
                            /* 保存文件 */ 
                     Log.i(TAG,"click button2"); 
                            if (bmp != null) { 
                                    /* 检查SDCard是否存在 */ 
                                    if (!Environment.MEDIA_MOUNTED.equals(Environment 
                                                    .getExternalStorageState())) { 
                                            /* SD卡不存在,显示Toast信息 */ 
                                            Toast.makeText(ScanBarZBarActivity.this, 
                                                            "SD卡不存在!无法保存相片,请插入SD卡。", Toast.LENGTH_LONG) 
                                                            .show(); 
                                    } else { 
                                            try { 
                                                    /* 文件不存在就创建 */ 
                                                    File f = new File(Environment 
                                                                    .getExternalStorageDirectory(), path); 
                                                    Log.i(TAG,"click button2:" + f.getAbsolutePath()); 
                                                    if (!f.exists()) { 
                                                            f.mkdir(); 
                                                    } 
                                                    /* 保存相片文件 */ 
                                                    path1 = String.valueOf(c.get(Calendar.MILLISECOND)) 
                                                                    + "camera.jpg"; 
                                                    File n = new File(f, path1); 
                                                    FileOutputStream bos = new FileOutputStream(n 
                                                                    .getAbsolutePath()); 
                                                    /* 文件转换 */ 
                                                    bmp.compress(Bitmap.CompressFormat.JPEG, 100, bos); 
                                                    /* 调用flush()方法,更新BufferStream */ 
                                                    bos.flush(); 
                                                    /* 结束OutputStream */ 
                                                    bos.close(); 
                                                    Toast.makeText(ScanBarZBarActivity.this, 
                                                                    path1 + "保存成功!", Toast.LENGTH_LONG).show(); 
                                                    cnt++; 
                                            } catch (Exception e) { 
                                                    e.printStackTrace(); 
                                            } 
                                    } 
                            } 
      
                            mButton.setVisibility(View.VISIBLE); 
                            mButton1.setVisibility(View.VISIBLE); 
                            mButton2.setVisibility(View.VISIBLE); 
                            /* 重新设定Camera */ 
                            stopCamera(); 
                            initCamera(); 
                    } 
            }); 
            /* 放弃Button的事件处理 */ 
            mButton2.setOnClickListener(new Button.OnClickListener() { 
                    @Override 
                    public void onClick(View arg0) { 
                            mButton.setVisibility(View.VISIBLE); 
                            mButton1.setVisibility(View.VISIBLE); 
                            mButton2.setVisibility(View.VISIBLE); 
                            /* 重新设定Camera */ 
                            stopCamera(); 
                            initCamera(); 
                    } 
            }); 
    } 
      
    @Override 
    public void surfaceCreated(SurfaceHolder surfaceholder) { 
            try { 
                    /* 打开相机, */ 
                    mCamera = Camera.open(); 
                    mCamera.setPreviewDisplay(holder); 
            } catch (IOException exception) { 
                    mCamera.release(); 
                    mCamera = null; 
            } 
    } 
      
    @Override 
    public void surfaceChanged(SurfaceHolder surfaceholder, int format, int w, 
                    int h) { 
            /* 相机初始化 */ 
            Log.i(TAG,"init camera"); 
            initCamera(); 
    } 
      
    @Override 
    public void surfaceDestroyed(SurfaceHolder surfaceholder) { 
            Log.i(TAG,"destoryed camera"); 
            stopCamera(); 
            mCamera.release(); 
            mCamera = null; 
    } 
      
    /* 拍照的method */ 
    private void takePicture() { 
            if (mCamera != null) { 
                Log.i(TAG,"takePicture"); 
                    mCamera.takePicture(shutterCallback, rawCallback, jpegCallback); 
            } 
    } 
      
    private ShutterCallback shutterCallback = new ShutterCallback() { 
            public void onShutter() { 
      
            } 
    }; 
      
    private PictureCallback rawCallback = new PictureCallback() { 
            public void onPictureTaken(byte[] _data, Camera _camera) { 
            } 
    }; 
      
    private PictureCallback jpegCallback = new PictureCallback() { 
            public void onPictureTaken(byte[] _data, Camera _camera) { 
                    /* 取得相仞 */ 
                    try { 
                            /* 设定Button?视性 */ 
                            mButton.setVisibility(View.GONE); 
                            mButton1.setVisibility(View.VISIBLE); 
                            mButton2.setVisibility(View.VISIBLE); 
                            /* 取得相仞Bitmap对象 */ 
                            bmp = BitmapFactory.decodeByteArray(_data, 0, _data.length); 
                            //发送给isbn分析 
                           // String isbn = getISBN(bmp);获得图片解析后的信息 
                          //  Log.i(TAG, "isbn:" + flag + isbn); 
                            flag--; 
                            if(flag>0){ 
                                mCamera.autoFocus(mAutoFocusCallback); 
                            } 
                                 
                    } catch (Exception e) { 
                            e.printStackTrace(); 
                    } 
            } 
    }; 
      
    /* 告定义class AutoFocusCallback */ 
    public final class AutoFocusCallback implements 
                    android.hardware.Camera.AutoFocusCallback { 
            public void onAutoFocus(boolean focused, Camera camera) { 
                    /* 对到焦点拍照 */ 
                    if (focused) { 
                            takePicture(); 
                    } 
            } 
    }; 
      
    /* 相机初始化的method */ 
    private void initCamera() { 
            if (mCamera != null) { 
                    try { 
                            Camera.Parameters parameters = mCamera.getParameters(); 
                            /*
                             * 设定相片大小为1024*768, 格式为JPG
                             */ 
                            parameters.setPictureFormat(PixelFormat.JPEG); 
                            parameters.setPictureSize(1024, 768); 
                            mCamera.setParameters(parameters); 
                            /* 开启预览画面 */ 
                            mCamera.startPreview(); 
                    } catch (Exception e) { 
                            e.printStackTrace(); 
                    } 
            } 
    } 
      
    /* 停止相机的method */ 
    private void stopCamera() { 
            if (mCamera != null) { 
                    try { 
                            /* 停止预览 */ 
                            mCamera.stopPreview(); 
                    } catch (Exception e) { 
                            e.printStackTrace(); 
                    } 
            } 
    } 
}


收藏 赞 (1) 踩 (0)
上一篇:android应用升级代码
看了几个博客,讲自动升级的程序,但是感觉都不是很完整,因为项目需要,自己手动写了个自动更新的程序,备忘下。 一、 需求: 如下图流程所示,需要在后台检查APK是否需要升级,需要升级则弹出提示下载升级对话框,用户点击下载进行升级,然后自动安装。 软
下一篇:Android.mk的用法和基础
一个Android.mk file用来向编译系统描述你的源代码。具体来说:该文件是GNU Makefile的一小部分,会被编译系统解析一次或多次。你可以在每一个Android.mk file中定义一个或多个模块,你也可以在几个模块中使用同一个源代码文件。选项参考以下文件:build/cor