调用相机的前置摄像头 两点触屏缩放

 

前置摄像头

 

Camera openFrontFacingCamera() {
  Camera camera = null;
    try {
   Class<?> cameraClass = Class.forName("android.hardware.Camera");
   Object cameraInfo = null;
   Field field = null;
   int cameraCount = 0;
   Method getNumberOfCamerasMethod = cameraClass
     .getMethod("getNumberOfCameras");
   if (getNumberOfCamerasMethod != null) {
    cameraCount = (Integer) getNumberOfCamerasMethod.invoke(null,
      (Object[]) null);
   }
   Class<?> cameraInfoClass = Class
     .forName("android.hardware.Camera$CameraInfo");
   if (cameraInfoClass != null) {
    cameraInfo = cameraInfoClass.newInstance();
   }
   if (cameraInfo != null) {
    field = cameraInfo.getClass().getField("facing");
   }

   Method getCameraInfoMethod = cameraClass.getMethod("getCameraInfo",
     Integer.TYPE, cameraInfoClass);
   if (getCameraInfoMethod != null && cameraInfoClass != null
     && field != null) {
    for (int camIdx = 0; camIdx < cameraCount; camIdx++) {
     getCameraInfoMethod.invoke(null, camIdx, cameraInfo);
     int facing = field.getInt(cameraInfo);
     if (facing == 1) {
      // Camera.CameraInfo.CAMERA_FACING_FRONT
      try {
       Method cameraOpenMethod = cameraClass.getMethod(
         "open", Integer.TYPE);
       if (cameraOpenMethod != null) {
        camera = (Camera) cameraOpenMethod.invoke(null,
          camIdx);
       }
      } catch (RuntimeException e) {

      }
     }
    }
   }
  } catch (ClassNotFoundException e) {
  } catch (NoSuchMethodException e) {
  } catch (NoSuchFieldException e) {
  } catch (IllegalAccessException e) {
  } catch (InvocationTargetException e) {
  } catch (InstantiationException e) {
  } catch (SecurityException e) {
  }
  if (camera == null) {
   // Try using the pre-Gingerbread APIs to open the camera.
   try {
    camera = Camera.open();
   } catch (RuntimeException e) {

   }
  }
  return camera;

 }

 

 

两点触屏缩放

 Matrix matrix = new Matrix();
Matrix savedMatrix = new Matrix();

static final int NONE = 0;
static final int DRAG = 1;
static final int ZOOM = 2;
int mode = NONE;
PointF start = new PointF();
PointF mid = new PointF();
float oldDist = 1f;

 public boolean onTouch(View v, MotionEvent event) {
  // TODO Auto-generated method stub
  ImageView view = (ImageView) v;

  switch (event.getAction() & MotionEvent.ACTION_MASK) {
  case MotionEvent.ACTION_DOWN:
   savedMatrix.set(matrix);
   //初始位置

   start.set(event.getX(), event.getY());

   mode = DRAG;
   break;
  case MotionEvent.ACTION_POINTER_DOWN:
   oldDist = spacing(event);

   if (oldDist > 10f) {
    savedMatrix.set(matrix);
    midPoint(mid, event);
    mode = ZOOM;

   }
   break;
  case MotionEvent.ACTION_UP:
  case MotionEvent.ACTION_POINTER_UP:
   mode = NONE;

   break;
  case MotionEvent.ACTION_MOVE:
   if (mode == DRAG) {

    matrix.set(savedMatrix);
    matrix.postTranslate(event.getX() - start.x, event.getY()
      - start.y);
   } else if (mode == ZOOM) {
    float newDist = spacing(event);

    if (newDist > 10f) {
     matrix.set(savedMatrix);
     float scale = newDist / oldDist;
     matrix.postScale(scale, scale, mid.x, mid.y);
    }
   }
   break;
  }

  view.setImageMatrix(matrix);
  return true;
 }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值