如何强制设置后置摄像头并禁用Android中的前置摄像头

时间:2016-04-20 11:32:58

标签: android

  Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            intent.putExtra(MediaStore.EXTRA_OUTPUT,
                    MediaStore.Images.Media.EXTERNAL_CONTENT_URI.toString());
            intent.putExtra("android.intent.extras.CAMERA_FACING", 0);
            // ******** code for crop image
            intent.putExtra("crop", "true");
            intent.putExtra("aspectX", 0);
            intent.putExtra("aspectY", 0);
            intent.putExtra("outputX", 200);
            intent.putExtra("outputY", 150);


            try {

                intent.putExtra("return-data", true);
                startActivityForResult(intent, PICK_FROM_CAMERA);

            } catch (ActivityNotFoundException e) {
                // Do nothing for now
            }

此代码我用于Open Camera:

  @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {

        if (requestCode == PICK_FROM_CAMERA) {
            try {
                Bundle extras = data.getExtras();
                if (extras != null) {
                    photo = extras.getParcelable("data");                

                }

            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        }

此代码用于返回位图数据请告诉我如何设置相机永远不会总是在Android前面,当我打开相机时请建议我解决此问题。

2 个答案:

答案 0 :(得分:1)

您无法使用ACTION_IMAGE_CAPTURE强制使用特定相机。不需要任何相机应用程序来兑现您在Intent上打包的许多未记录的附加内容。

如果您需要这种程度的控制,请直接使用相机API(例如android.hardware.Camera)并编写您自己的相机代码。

答案 1 :(得分:0)

您可以尝试使用以下代码打开Back Camera

   Camera cam = null;
  Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
  for (int camNo = 0; camNo < Camera.getNumberOfCameras(); camNo++) {
 CameraInfo camInfo = new CameraInfo();
  Camera.getCameraInfo(camNo, camInfo);
 if (camInfo.facing.equals(Camera.CameraInfo.CAMERA_FACING_BACK)) {
    cam = Camera.open(camNo);
}
}
   if (cam == null) {
  // no front-facing camera, use the first back-facing camera instead.
     // you may instead wish to inform the user of an error here...
   cam = Camera.open();
          }