android中相机的简单调用

1.生命周期方法中初始化和释放Camera
@TargetApi(Build.VERSION_CODES.GINGERBREAD)
 @Override
 public void onResume() {
  super.onResume();
  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
   mCamera = Camera.open(0);
  } else {
   mCamera = Camera.open();
  }
 }

 @Override
 public void onPause() {
  super.onPause();
  if (mCamera != null) {
   mCamera.release();
   mCamera = null;
  }
 }

2. 设置SurfaceView
mSurfaceView = (SurfaceView) view
    .findViewById(R.id.crime_camera_surfaceView);
  SurfaceHolder holder = mSurfaceView.getHolder();
  holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
  holder.addCallback(new SurfaceHolder.Callback() {

   @Override
   public void surfaceDestroyed(SurfaceHolder holder) {
    if (mCamera != null) {
     mCamera.stopPreview();
    }
   }

   @Override
   public void surfaceCreated(SurfaceHolder holder) {
    if (mCamera != null) {
     try {
      mCamera.setPreviewDisplay(holder);
     } catch (IOException e) {
      e.printStackTrace();
     }
    }
   }

   @Override
   public void surfaceChanged(SurfaceHolder holder, int format,
     int width, int height) {
    Camera.Parameters parameters = mCamera.getParameters();
    //设置预览图片大小
    Size size = getBestSupportSize(
      parameters.getSupportedPreviewSizes(), width, height);
    parameters.setPreviewSize(size.width, size.height);
    //设置实际生成的图片大小
    size = getBestSupportSize(parameters.getSupportedPictureSizes(),
      width, height);
    parameters.setPictureSize(size.width, size.height);
    mCamera.setParameters(parameters);
    try {
     mCamera.startPreview();
    } catch (Exception e) {
     mCamera.release();
     mCamera = null;
    }
   }
  });
//获得最佳的支持尺寸
private Size getBestSupportSize(List<Size> sizes, int width, int height) {
  Size bestSize = sizes.get(0);
  int largestArea = bestSize.width * bestSize.height;
  for (Size size : sizes) {
   int area = size.width * size.height;
   if (area > largestArea) {
    bestSize = size;
    largestArea = area;
   }
  }
  return bestSize;
 }

3. 点击按钮时调用takePicture API,与在回调中保存图和片
    .takePicture(mShutterCallback shutter, PictureCallback raw, PictureCallback jpg); 方法
    ShutterCallback回调方法会在相机捕获图像时调用,但此时,图像数据还未处理完成。
    第  一个PictureCallback回调方法是在原始图像数据可用时调用,通常来说,是在加工处理原始图 
        像数据且没有存储之前。
    第二个PictureCallback回调方法是在JPEG版本的图像可用时调用

Button takePicbButton = (Button) view
    .findViewById(R.id.crime_camera_takePictureButton);
  takePicbButton.setOnClickListener(new OnClickListener() {
   @Override
   public void onClick(View v) {
    //getActivity().finish();
    if (mCamera != null) {
     mCamera.takePicture(mShutterCallback, null, mPictureCallback);
    }
   }
  });

// 相机的回调
 private Camera.ShutterCallback mShutterCallback = new Camera.ShutterCallback() {
  @Override
  public void onShutter() {
   mProgressContainer.setVisibility(View.VISIBLE);
  }
 };

 private Camera.PictureCallback mPictureCallback = new Camera.PictureCallback() {
  @Override
  public void onPictureTaken(byte[] data, Camera camera) {
   String fileName = UUID.randomUUID().toString() + ".jpg";
   FileOutputStream fos = null;
   boolean success = true;

   try {
    fos = getActivity().openFileOutput(fileName,
      Context.MODE_PRIVATE);
    fos.write(data);
   } catch (Exception e) {
    e.printStackTrace();
    success = false;
   } finally {
    if (fos != null) {
     try {
      fos.close();
     } catch (IOException e) {
      e.printStackTrace();
      success = false;
     }

     if (success) {
      Log.i(TAG, "JPEG save at " + fileName);
      //把路径返回给CrimeActivity
      Intent intent = new Intent();
      intent.putExtra(EXTRAL_PHOTO_FILENAME, fileName);
      getActivity().setResult(Activity.RESULT_OK, intent);
     } else {
      getActivity().setResult(Activity.RESULT_CANCELED);
     }
    }
   
    getActivity().finish();
   }
  }
 };


4. 设置图片缩放显示
public static BitmapDrawable getScaledDrawable(Activity activity,
   String path) {
  Display display = activity.getWindowManager().getDefaultDisplay();
  int destWidth = display.getWidth();
  int destHeight = display.getHeight();

  BitmapFactory.Options options = new BitmapFactory.Options();
  options.inJustDecodeBounds = true;
  BitmapFactory.decodeFile(path, options);

  float srcWidth = options.outWidth;
  float srcHeight = options.outHeight;

  int inSampleSize = 1;
  if (srcWidth > destWidth || srcHeight > destHeight) {
   if (srcWidth > srcHeight) {
    inSampleSize = Math.round(srcHeight / destHeight);
   } else {
    inSampleSize = Math.round(srcWidth / destWidth);
   }
  }

  options = new BitmapFactory.Options();
  options.inSampleSize = inSampleSize;

  Bitmap bitmap = BitmapFactory.decodeFile(path, options);
  return new BitmapDrawable(activity.getResources(), bitmap);

 }

你可能感兴趣的:(android中相机的简单调用)