RedApparat / Fotoapparat

Making Camera for Android more friendly. 📸
Apache License 2.0
3.81k stars 405 forks source link

Turn on/off flashlight #325

Closed bnk120 closed 5 years ago

bnk120 commented 5 years ago

Hi, I would like to turn on/off flashlight manually? How can i achieve that?

What are you trying to achieve or the steps to reproduce?

I have a button on my View with above function attached to Click event:

private bool isFlashLight = false;
        private void TurnFlashLight(object sender, EventArgs e)
        {
            if (!isFlashLight)
            {
                aparat.UpdateParameters(TurnOnFlash());
                isFlashLight = true;
            }
            else
            {
                aparat.UpdateParameters(TurnOffFlash());
                isFlashLight = false;
            }
        }

        private UpdateRequest TurnOffFlash()
        {
            return UpdateRequest.InvokeBuilder().Flash(FlashSelectors.Off()).Build();
        }

        private UpdateRequest TurnOnFlash()
        {
            return UpdateRequest.InvokeBuilder().Flash(FlashSelectors.Torch()).Build();
        }

At runtime i've got this exception:

java.lang.IllegalArgumentException: The selected parameter is not in the supported set of values.

How did you initialize FA?

var  aparat = CreateFotoapparat(LensPosition.Back, activity);
private Fotoapparat CreateFotoapparat(LensPosition position, Activity activity)
        {
            return Fotoapparat
                .With(activity)
                .CameraProvider(CameraProviders.V1())
                .Into(cameraView)
                .PreviewScaleType(ScaleType.CenterCrop)
                .PhotoSize(AspectRatioSelectors.StandardRatio(SizeSelectors.BiggestSize()))                
                .LensPosition(LensPositionSelectors.LensPosition(position))
                .FocusMode(Selectors.FirstAvailable(
                    FocusModeSelectors.ContinuousFocus(),
                    FocusModeSelectors.AutoFocus(),
                    FocusModeSelectors.Fixed()
                ))
                 .Flash(Selectors.FirstAvailable(
                    FlashSelectors.Torch(),
                    FlashSelectors.On(),                     
                    FlashSelectors.Off()))
                .PreviewFpsRange(PreviewFpsRangeSelectors.RangeWithHighestFps())
                .SensorSensitivity(SensorSensitivitySelectors.HighestSensorSensitivity())
                .FrameProcessor(this)
                .JpegQuality(100)
                .Build();
        }

What did you expect?

Expect that i can turn flashlight ON when i click button without taking photo and turn flashlight OFF when click again button.

bnk120 commented 5 years ago

I found solution by my own. Below working code:

private bool isFlashLight = false;
private void TurnFlashLight(object sender, EventArgs e)
{
    if (!isFlashLight)
    {
        aparat.UpdateParameters(TurnOnFlash());
        isFlashLight = true;
    }
    else
    {
        aparat.UpdateParameters(TurnOffFlash());
        isFlashLight = false;
    }
}

private UpdateRequest TurnOnFlash()
{
    return new UpdateRequest.Builder()
        .Flash(FlashSelectors.Torch())
        .FocusMode(FocusModeSelectors.ContinuousFocus())  //you can use other focus selector
        .Build();
}

private UpdateRequest TurnOffFlash()
{
    return new UpdateRequest.Builder()
        .Flash(FlashSelectors.Off())
        .FocusMode(FocusModeSelectors.ContinuousFocus()) //you can use other focus selector
        .Build();
}