How to turn on flashlight and front camera at the same time in android

前端 未结 3 1290
花落未央
花落未央 2020-12-11 21:22

In one of the requirement in my app I need to pop up an activity containing the front camera preview,at this same time I need to turn on the flashlight as well.However I obs

3条回答
  •  情深已故
    2020-12-11 22:03

    The following code checks for the availability of a front camera:

    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;
    }
    

    Then add the following permissions in the AndroidManifest.xml file:

    
    
    
    

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

    I recommend using surfaceView like in this example to implement a working flashlight.

    Hope this helps :)

提交回复
热议问题