【问题标题】:How to launch front camera with intent?如何有意识地启动前置摄像头?
【发布时间】:2012-03-29 23:34:03
【问题描述】:

我正在使用意图通过本机应用程序打开相机:

    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

    Uri photoUri = Uri.fromFile(getOutputPhotoFile());
    intent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);

    startActivityForResult(intent, CAMERA_PHOTO_REQUEST_CODE);

每次打开摄像头(前置/后置摄像头)就像上次打开本机摄像头应用程序一样。这意味着如果我上次关闭本机摄像头应用程序后置摄像头处于活动状态,那么当我启动摄像头意图时,后置摄像头将处于活动状态。

我想直接启动前置摄像头。有人知道怎么做吗?

【问题讨论】:

  • 此链接可能对您有帮助 stackoverflow.com/questions/2779002/…
  • 这是在没有任何意图的情况下完成的。还是谢谢
  • @Yaniv,你找到解决方案了吗?
  • 你找到解决办法了吗?

标签: android android-camera


【解决方案1】:

注意事项:这是一种黑客行为

将此添加到意图中

intent.putExtra("android.intent.extras.CAMERA_FACING", 1);

此解决方案不可持续,它使用相机应用的测试代码。有关详细信息,请查看 AOSP 相机项目的 Util 类中的“getCameraFacingIntentExtras”静态方法。

更新: 看起来它在L中被禁用了

【讨论】:

  • 我试过了,但它不起作用,你能帮帮我吗??
  • 不适用于 Google 相机、Nexus 4、Android 5.0 Lollipop。
  • 只能通过intent打开后置摄像头吗?我的意思是禁用前摄像头
  • 实际上,if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) { intent.putExtra("android.intent.extras.LENS_FACING_FRONT", 1); } else { intent.putExtra("android.intent.extras.CAMERA_FACING", 1); } 正在使用 Nexus 5 5.1(如果有)和三星 S4 5.0.1(其他情况),但不能使用 Honor 7...
  • 在三星设备上试过,总是显示后置摄像头
【解决方案2】:

取自 Google 相机的 Android 7.1 快捷方式(但应该适用于较旧的 Android)

intent.putExtra("android.intent.extra.USE_FRONT_CAMERA", true);

因此,结合之前的答案,这适用于我可以测试的所有手机

intent.putExtra("android.intent.extras.CAMERA_FACING", android.hardware.Camera.CameraInfo.CAMERA_FACING_FRONT);
intent.putExtra("android.intent.extras.LENS_FACING_FRONT", 1);
intent.putExtra("android.intent.extra.USE_FRONT_CAMERA", true);

【讨论】:

    【解决方案3】:

    在对华为和三星相机进行了一些逆向工程之后,以下代码适用于对我来说很重要的大多数设备:

    Intent(MediaStore.ACTION_IMAGE_CAPTURE).apply {
        putExtra(MediaStore.EXTRA_OUTPUT, photoUri)
    
        putExtra("com.google.assistant.extra.USE_FRONT_CAMERA", true)
        putExtra("android.intent.extra.USE_FRONT_CAMERA", true)
        putExtra("android.intent.extras.LENS_FACING_FRONT", 1)
        putExtra("android.intent.extras.CAMERA_FACING", 1)
    
        // Samsung
        putExtra("camerafacing", "front")
        putExtra("previous_mode", "front")
    
        // Huawei
        putExtra("default_camera", "1")
        putExtra("default_mode", "com.huawei.camera2.mode.photo.PhotoMode")
    }
    

    【讨论】:

    • 不幸的是三星它确实转向了前置摄像头,但使用的是图片模式,而不是预期的视频模式(不,我没有复制整个代码,只是与三星 cmets 的行)。知道出了什么问题吗?
    【解决方案4】:

    没有专门针对前置摄像头的意图 (AFAIK)。

    以编程方式进行:Android SDK

    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_FRONT)) {
            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();
    }
    // ... do stuff with Camera cam ...
    

    这个例子只是框架,并没有提供任何(非常需要的)错误处理。

    编辑:您还需要将这些添加到清单中:

    <uses-permission android:name="android.permission.CAMERA" />
    <uses-feature android:name="android.hardware.camera" android:required="false" />
    <uses-feature android:name="android.hardware.camera.front" android:required="false" />
    

    【讨论】:

    • 我知道你写的这段代码。但我必须有一个意图。还是谢谢。
    • 在三星设备(android 版本 4.04)中收到“相机无法打开:无法连接到相机服务”。
    【解决方案5】:
        pictureIntent.putExtra("android.intent.extra.USE_FRONT_CAMERA", true);
        pictureIntent.putExtra("android.intent.extras.LENS_FACING_FRONT", 1);
        pictureIntent.putExtra("android.intent.extras.CAMERA_FACING", 1);
    
    working on intex 
    

    【讨论】:

      【解决方案6】:

      以下代码将在Android 11 之前有效,包括Samsung 电话

      fun updateIntentForCameraFacing(cameraIntent: Intent, frontFacing: Boolean){
          if(Build.VERSION.SDK_INT < Build.VERSION_CODES.O){
              if(frontFacing)
                  cameraIntent.putExtra("android.intent.extras.CAMERA_FACING", CameraCharacteristics.LENS_FACING_BACK)
              else
                  cameraIntent.putExtra("android.intent.extras.CAMERA_FACING", CameraCharacteristics.LENS_FACING_FRONT)
      
          }
          else if(frontFacing){
              cameraIntent.putExtra("android.intent.extras.CAMERA_FACING", CameraCharacteristics.LENS_FACING_BACK)
              cameraIntent.putExtra("android.intent.extra.USE_FRONT_CAMERA", true)
      
              //samsung
              cameraIntent.putExtra("camerafacing", "front")
              cameraIntent.putExtra("previous_mode", "front")
          }
          else{
              cameraIntent.putExtra("android.intent.extras.CAMERA_FACING", CameraCharacteristics.LENS_FACING_FRONT)
              cameraIntent.putExtra("android.intent.extra.USE_FRONT_CAMERA", false)
      
              //samsung
              cameraIntent.putExtra("camerafacing", "rear")
              cameraIntent.putExtra("previous_mode", "rear")
          }
      }
      

      【讨论】:

        【解决方案7】:

        试试这个:

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
          intent.putExtra("android.intent.extras.LENS_FACING_FRONT", 1);
        } else {
          intent.putExtra("android.intent.extras.CAMERA_FACING", 1);
        }
        

        【讨论】:

          【解决方案8】:

          您是否尝试在原生相机应用程序中切换到前置摄像头时观看adb logcat?如果它确实是另一个活动,那么它将在那里显示,您可以简单地将意图复制到您的应用程序。如果它没有出现,我猜你很不走运。

          【讨论】:

          • logcat 上没有相关内容。还是谢谢。
          猜你喜欢
          • 2019-02-08
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-04-17
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多