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

前端 未结 9 2298
挽巷
挽巷 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: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:

    
    
    
    

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

提交回复
热议问题