How do I open the “front camera” on the Android platform?

前端 未结 9 2141
挽巷
挽巷 2020-11-22 15:00

More generally, if a device has more than one embedded camera, is there a way to initialize one of them in particular?

I didn\'t find it in Android reference documen

相关标签:
9条回答
  • 2020-11-22 15:50

    For API 21 (5.0) and later you can use the CameraManager API's

    try {
        String desiredCameraId = null;
        for(String cameraId : mCameraIDsList) {
            CameraCharacteristics chars =  mCameraManager.getCameraCharacteristics(cameraId);
            List<CameraCharacteristics.Key<?>> keys = chars.getKeys();
            try {
                if(CameraCharacteristics.LENS_FACING_FRONT == chars.get(CameraCharacteristics.LENS_FACING)) {
                   // This is the one we want.
                   desiredCameraId = cameraId;
                   break;
                }
            } catch(IllegalArgumentException e) {
                // This key not implemented, which is a bit of a pain. Either guess - assume the first one
                // is rear, second one is front, or give up.
            }
        }
    }
    
    0 讨论(0)
  • 2020-11-22 15:59
    private Camera openFrontFacingCameraGingerbread() {
        int cameraCount = 0;
        Camera cam = null;
        Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
        cameraCount = Camera.getNumberOfCameras();
        for (int camIdx = 0; camIdx < cameraCount; camIdx++) {
            Camera.getCameraInfo(camIdx, cameraInfo);
            if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
                try {
                    cam = Camera.open(camIdx);
                } catch (RuntimeException e) {
                    Log.e(TAG, "Camera failed to open: " + e.getLocalizedMessage());
                }
            }
        }
    
        return cam;
    }
    

    Add the following permissions in the AndroidManifest.xml file:

    <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" />
    

    Note: This feature is available in Gingerbread(2.3) and Up Android Version.

    0 讨论(0)
  • 2020-11-22 16:02

    To open the back camera:-

    val cameraIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
    startActivityForResult(cameraIntent, REQUEST_CODE_CAMERA)
    

    To open the front camera:-

    val cameraIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
    when {
         Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1 && Build.VERSION.SDK_INT < Build.VERSION_CODES.O -> {
             cameraIntent.putExtra("android.intent.extras.CAMERA_FACING", CameraCharacteristics.LENS_FACING_FRONT)  // Tested on API 24 Android version 7.0(Samsung S6)
         }
         Build.VERSION.SDK_INT >= Build.VERSION_CODES.O -> {
             cameraIntent.putExtra("android.intent.extras.CAMERA_FACING", CameraCharacteristics.LENS_FACING_FRONT) // Tested on API 27 Android version 8.0(Nexus 6P)
             cameraIntent.putExtra("android.intent.extra.USE_FRONT_CAMERA", true)
         }
         else -> cameraIntent.putExtra("android.intent.extras.CAMERA_FACING", 1)  // Tested API 21 Android version 5.0.1(Samsung S4)
    }
    startActivityForResult(cameraIntent, REQUEST_CODE_CAMERA)
    

    I could not make it work for API 28 and above. Also, opening the front camera directly is not possible in some devices(depends on the manufacturer).

    0 讨论(0)
提交回复
热议问题