网站建设资讯

NEWS

网站建设资讯

AndroidCamera实现毫秒级拍照实例

我们知道自定义Camera需要以下几步

创新互联建站是一家专业提供松桃企业网站建设,专注与成都网站制作、网站设计、H5开发、小程序制作等业务。10年已为松桃众多企业、政府机构等服务。创新互联专业网站建设公司优惠进行中。

  1. 打开相机,即实例化Camera对象,Camera camera = Camera.open();
  2. 设置Camera的相关参数,Camera.Parameters parameters = camera.getParameters();
  3. 打开预览,camera.setPreviewDisplay(surfaceholder); camera.startPreview();
  4. 获取图片,这里只是从预览中获取因此使用,camera.setPreviewCallback(new Camera.PreviewCallback(){……..});
  5. 停止预览,释放相机,camera.stopPreview();camera.release();

1、打开相机

public static Camera openFacingBackCamera() {
    Camera cam = null;
    Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
    for (int camIdx = 0, cameraCount = Camera.getNumberOfCameras(); camIdx < cameraCount; camIdx++) {
      Camera.getCameraInfo(camIdx, cameraInfo);
      if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
        try {
          //打开相应的摄像头,目前是前置摄像头
          cam = Camera.open(camIdx);
        } catch (RuntimeException e) {
          e.printStackTrace();
          if (cam!=null){
            cam.stopPreview();
            cam.release();
          }
          cam = null;
        }
      }
    }
    return cam;
  }

2、设置相关参数和预览

//定义SurfaceView
private SurfaceView localSurface;
 // 为surfaceHolder添加一个回调监听器
    localSurface.getHolder().addCallback(new SurfaceHolder.Callback() {
      @Override
      public void surfaceCreated(SurfaceHolder holder) {

      }

      @Override
      public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        camera = CameraUtil.openFacingBackCamera();
        //设置相机预览旋转角度
        camera.setDisplayOrientation(180);
        if (camera != null) {
          Camera.Parameters parameters = camera.getParameters();// 获得相机参数
          List sizes = parameters.getSupportedPreviewSizes();
          List sizes_pic = parameters.getSupportedPictureSizes();
          Camera.Size cs = sizes.get(0);
          int mWidth = cs.width;
          int mHeight = cs.height;
          parameters.setPreviewSize(mWidth, mHeight);
          parameters.setPictureSize(sizes_pic.get(0).width, sizes_pic.get(0).height);
          parameters.setJpegQuality(85);//
          parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
          parameters.setPictureFormat(ImageFormat.JPEG);
          camera.setParameters(parameters);
          try {
            camera.setPreviewDisplay(localSurface.getHolder());
            camera.startPreview(); // 开始预览
            camera.autoFocus(null); // 自动对焦
          } catch (IOException e) {
            e.printStackTrace();
            camera.stopPreview();
            camera.release();
            camera = null;
          }
        }
      }

      @Override
      public void surfaceDestroyed(SurfaceHolder holder) {
        if (camera!=null){
          camera.stopPreview();
          camera.release();
          camera = null;
        }
      }
    } );
    // 设置该SurfaceView自己不维护缓冲
    localSurface.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

3、获取图片

 private void getViewImage() {
   //设置监听
    camera.setPreviewCallback(new Camera.PreviewCallback(){

      @Override
      public void onPreviewFrame(byte[] data, Camera camera) {
        Camera.Size size = camera.getParameters().getPreviewSize();
        try{
          YuvImage image = new YuvImage(data, ImageFormat.NV21, size.width, size.height, null);
          if(image!=null){
            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            image.compressToJpeg(new Rect(0, 0, size.width, size.height), 80, stream);
            Bitmap bmp = BitmapFactory.decodeByteArray(stream.toByteArray(), 0, stream.size());
            //因为图片会放生旋转,因此要对图片进行旋转到和手机在一个方向上
            rotateBitmap(bmp);
            stream.close();
          }
        }catch(Exception ex){
          Log.e("Sys","Error:"+ex.getMessage());
        }
      }
    });
  }

旋转图片的方法

public void rotateMyBitmap(Bitmap bmp){
    Matrix matrix = new Matrix();
    matrix.postScale(-1, 1); // 镜像水平翻转(如果左右颠倒)
    matrix.postRotate(180);
    Bitmap nbmp = Bitmap.createBitmap(bmp, 0,0, bmp.getWidth(), bmp.getHeight(), matrix, true);
    imageView.setImageBitmap(nbmp);
  };

自定义时间拍照

  private Handler mHandler = new Handler() {
    public void handleMessage(Message msg) {
      switch (msg.what) {
        case 101:
          getViewImage();
          Log.e(TAG, "handleMessage: 拍照" );
          mHandler.sendEmptyMessageDelayed(102, 100);
          break;
        case 102:
          camera.setPreviewCallback(null);
          break ;
      }
    }
  };

然后点击按钮调用

mHandler.sendEmptyMessageDelayed(101, 100);

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持创新互联。


当前题目:AndroidCamera实现毫秒级拍照实例
文章源于:http://njwzjz.com/article/pigpcp.html