Samsung Galaxy S5 Camera Torch not working

2019-02-20 05:16发布

问题:

We have an app that works with all our supported Android phones "except Samsung Galaxy S5". Our app uses the camera to take pictures at close range. We need torch mode ON the entire time we are focusing to take the picture. We check for supported parameters and set the values if supported.

The params get set but the event either never gets fired or the camera is ignoring my settings. I tested using OpenCamera and their app is able to turn the torch on yet I cannot find the difference between how I set my params vs. how they set theirs.

Here is our code:

//Set all camera parameters(flash, focus, white balance, etc)
private void setCameraParameters()
{
    //Rotate the orientation of the preview to match orientation of device
    camera.setDisplayOrientation(getCameraRotation());

    //A Parameters object must be used to set the other parameters.
    Parameters params = camera.getParameters();

        //Flash Mode to Torch if supported
        if(params.getSupportedFlashModes().contains("torch"))
        {
            // Torch mode
            params.setFlashMode(Parameters.FLASH_MODE_TORCH);
        }

        //Focus Mode to Macro if supported, Auto if not
        if(params.getSupportedFocusModes().contains("macro"))
        {
            //Macro focus mode
            params.setFocusMode(Parameters.FOCUS_MODE_MACRO);
        }
        else
        {
            //Auto focus mode
            params.setFocusMode(Parameters.FOCUS_MODE_AUTO);
        }


        //White Balance mode to Auto if available.
        List<String> supported_white = params.getSupportedWhiteBalance();
        if(supported_white!=null)
        {
            if(supported_white.contains("auto"))
            {
                params.setWhiteBalance(Parameters.WHITE_BALANCE_AUTO);
            }
        }

        // Auto Exposure Lock to false if available 
        if(params.isAutoExposureLockSupported())
        {
            params.setAutoExposureLock(false);
        }

        // Auto White Balance Lock if available. 
        if(params.getAutoWhiteBalanceLock())
        {
            params.setAutoWhiteBalanceLock(false);
        }

        //JPEG quality set to 100(highest)
        {
            params.setJpegQuality(100);
        }

        //Set focus area and metering area
        List<Camera.Area> focusArea = calculateFocusArea();
        params.setFocusAreas(focusArea);
        params.setMeteringAreas(focusArea);
        Camera.Size size = pickCameraSize(params.getSupportedPictureSizes());
        params.setPictureSize(size.width, size.height);

    //Set new parameters for camera
    camera.setParameters(params);

    boolean torch = getTorchState(camera);
}

// Added this method from zxing github to see if the value is being set
boolean getTorchState(Camera camera) {
    if (camera != null) {
        Camera.Parameters parameters = camera.getParameters();
        if (parameters != null) {
            String flashMode = camera.getParameters().getFlashMode();
            return flashMode != null
                    && (Camera.Parameters.FLASH_MODE_ON.equals(flashMode) || Camera.Parameters.FLASH_MODE_TORCH
                            .equals(flashMode));
        }
    }
    return false;
}

回答1:

I'm doing it slightly different.. maybe it will help you!

                params = getCamera().getParameters();
    ...

                //Check if device supports torch mode, If YES then enable
                List<String> supportedFlashModes = params.getSupportedFlashModes();
                if (supportedFlashModes != null && supportedFlashModes.contains(Parameters.FLASH_MODE_TORCH)){
                    params.setFlashMode(Parameters.FLASH_MODE_TORCH);
                    torchModeOn  = true;
                }
...

                getCamera().setParameters(params);

In comparison I'm using Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE and NOT using setFocusAreas, setMeteringArea, setAutoWhiteBalanceLock, setWhiteBalance or setAutoExposureLock.

After seeing your code, i tried to incorporate each one individually to see if it would better affect my pictures, with no luck. (My app requires close-up pictures as well.

Parameters.FOCUS_MODE_MACRO was not working well for me at all on any of the devices i tried it with..

EDIT:

Here is the order i am setting up my camera incase it helps...

        setCameraDisplayRotation();

        params = getCamera().getParameters();

        setFocusMode();

        //Check if device supports torch mode, If you YES then set on
        List<String> supportedFlashModes = params.getSupportedFlashModes();
        if (supportedFlashModes != null && supportedFlashModes.contains(Parameters.FLASH_MODE_TORCH)){
            params.setFlashMode(Parameters.FLASH_MODE_TORCH);
            torchModeOn  = true;
        }

        setImageResolution();

        getCamera().setParameters(params); // update params before preview.setCamera
        preview.setCamera(getCamera());   

        //... some custom code for determining the current screens available space for the preview

        params.setPreviewSize(size.width, size.height);

        if(setHiddenParameter(params, "zsl-values", "zsl", "on")){
            setUsingZsl(true);
        };

        getCamera().setParameters(params); //update params after preview init


回答2:

You have to use new camera2 API in Android Lollipop

Sample Code github

and developer site