• Android调用摄像头(二)


    第六步,我们必须对按键事件进行监听,如是拍照还是聚焦,代码如下
    public boolean onKeyDown(intkeyCode, KeyEvent event) {//处理按键事件
    if(camera!=null&&event.getRepeatCount()==0)//代表只按了一下
    {
    switch(keyCode){
    caseKeyEvent.KEYCODE_BACK://如果是搜索键
              camera.autoFocus(null);//自动对焦
        break;
          caseKeyEvent.KEYCODE_DPAD_CENTER://如果是中间键
              camera.takePicture(null,null, newTakePictureCallback());//将拍到的照片给第三个对象中,这里的TakePictureCallback()是自己定义的,在下面的代码中
      break;
    }
    }
    returntrue;//阻止事件往下传递,否则按搜索键会变成系统默认的
    }
    ------------------------------------------------------------------------------------------
    private final classTakePictureCallback implements PictureCallback{
    public void onPictureTaken(byte[]data, Camera camera) {
    try {
    Bitmap bitmap =BitmapFactory.decodeByteArray(data, 0, data.length);
    File file = newFile(Environment.getExternalStorageDirect ory(),System.currentTimeMillis()+".jpg");
    FileOutputStream outputStream = newFileOutputStream(file);
    bitmap.compress(CompressFormat.JPEG,100, outputStream);
    outputStream.close();
    camera.stopPreview();
    camera.startPreview();//处理完数据之后可以预览
    } catch (Exception e) {
    Log.e(TAG, e.toString());
    }
    }
    }
    注意,代码中有两个回调类,一个是SurfaceCallback(),另外一个是TakePictureCallback(),初学者可能一时难以理解,通俗地讲,前者是用来监视surficeView这个暂时存放图片数据的显示控件的,根据它的显示情况调用不同的方法,包括surfaceCreated(),surfaceChanged(),surfaceDestroyed(),也就不难理解为什么会有这三个回调方法了(注意,在surfaceDestroyed()方法中必须释放摄像头,详细代码参见上方)。TakePictureCallback()是为了监视是否拍照而设计的接口,期中也仅有一个方法,camera将拍照得到的数据传入方法,我们便可以对拍照得到的数据进行进一步处理了。
    至此,简单的拍照功能介绍完毕!
    packagecn.camera.rxm;
     
    importjava.io.File;
    importjava.io.FileOutputStream;
    importjava.io.IOException;
    importjava.util.Calendar;
    importjava.util.Locale;
     
    importorg.apache.commons.logging.Log;
     
    importandroid.text.format.DateFormat;
    importandroid.util.*;
    importandroid.app.Activity;
    importandroid.content.Context;
    importandroid.graphics.Bitmap;
    importandroid.graphics.Bitmap.CompressFormat;
    importandroid.graphics.BitmapFactory;
    importandroid.graphics.PixelFormat;
    importandroid.hardware.Camera;
    importandroid.hardware.Camera.PictureCallback;
    importandroid.os.Bundle;
    importandroid.os.Environment;
    importandroid.view.LayoutInflater;
    importandroid.view.Menu;
    importandroid.view.MenuItem;
    importandroid.view.SurfaceHolder;
    importandroid.view.SurfaceView;
    importandroid.view.View;
    importandroid.view.View.OnClickListener;
    importandroid.widget.Button;
    //importandroid.widget.Toast;
     
     
     
     
    public classMycamActivity extends Activity {
       
    privatePreview mPreview;
    privateCamera mCamera;
    BitmapCameraBitmap;
    SurfaceHolder mHolder;
    privatestatic final int OPTION_SNAPSHOT = 0;
    privatestatic final int OPTION_STOPCAMERA = 1;
    private ViewviewStart;
        @Override
         
         
        public void onCreate(BundlesavedInstanceState) {
            super.onCreate(savedInstanceState);
            LayoutInflater flater =this.getLayoutInflater();
            viewStart = flater.inflate(R.layout.main,null);
            setContentView(viewStart);
             
            Button btn1 =(Button)findViewById(R.id.button1);
             
            btn1.setOnClickListener(
            new OnClickListener(){
            public void onClick(View v){
           
            mPreview = new Preview(getBaseContext());
            setContentView(mPreview); 
            };
            }
            );
        }
         
         
        public booleanonCreateOptionsMenu(Menu menu){
    //
    menu.add(0,OPTION_SNAPSHOT, 0, R.string.take);
    //
    menu.add(0,OPTION_STOPCAMERA, 1, R.string.back);
    //
    returntrue;//super.onCreateOptionsMenu(menu);
    }
    publicboolean onOptionsItemSelected(MenuItem item) {
    //
     
    int itemId =item.getItemId();
    //
    switch(itemId){
    caseOPTION_SNAPSHOT:
    //拍摄照片
    mCamera.takePicture(null, null, jpegCallback);
    try {
    Thread.sleep(4000);
    } catch(InterruptedException e) {
    // TODOAuto-generated catch block
    e.printStackTrace();
    }
    mCamera.startPreview();
    break;
    caseOPTION_STOPCAMERA:
     
    mPreview =null;  
    setContentView(viewStart); 
    break;
    }
     
    returntrue;
    }
    privatePictureCallback jpegCallback = new PictureCallback(){
     
    //
    public voidonPictureTaken(byte[] data, Camera camera) {
     
    try {
    String name = newDateFormat().format("yyyyMMdd_hhmmss",
    Calendar.getInstance(Locale.CHINA)) +".jpg";
    FileOutputStream fileout = newFileOutputStream("/mnt/sdcard/sdcard/DCIM/"+ name);
    System.out.println(name);
    fileout.write(data,0,data.length);
    fileout.flush();
    fileout.close();
    } catch (IOException e) {
    // TODO: handle exception
    System.out.println(e);
    }
     
    }
    };
    classPreview extends SurfaceView implements SurfaceHolder.Callback
    {
    Preview(Context context)
    {
    super(context);
    mHolder=getHolder();
    mHolder.addCallback(this);
    mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    }
    public voidsurfaceCreated(SurfaceHolder holder)
    {
    mCamera=Camera.open();
    try
    {
    mCamera.setPreviewDisplay(holder);
    }
    catch(IOException exception)
    {
    mCamera.release();
    mCamera=null;
    }
    }
    public voidsurfaceDestroyed(SurfaceHolder holder)
    {
    mCamera.stopPreview();
    mCamera.release();
    }
     
     
    @Override
    public voidsurfaceChanged(SurfaceHolder holder, int format, int w, int h){
    // TODOAuto-generated method stub
    Camera.Parameters parameters=mCamera.getParameters();
    parameters.setPictureFormat(PixelFormat.JPEG);
    parameters.setPreviewSize(1024, 1024);
     
    mCamera.setParameters(parameters);
    mCamera.setDisplayOrientation(90);
    mCamera.startPreview();
    }
    }
    }
  • 相关阅读:
    viewmodel
    jQuery.each的function中有哪些参数
    JS,Jquery获取各种屏幕的宽度和高度
    解决点击空<a>标签返回页面顶部的问题
    JS使用getComputedStyle()方法获取CSS属性值
    html5中画布和SVG的比较
    HTML中的SVG
    Canvas绘制时钟
    用Canvas实现动画效果
    绘制文字
  • 原文地址:https://www.cnblogs.com/wangdayang/p/14913211.html
Copyright © 2020-2023  润新知