How to turn on front flash light programmatically in Android?

后端 未结 11 1595
执笔经年
执笔经年 2020-11-22 00:17

I want to turn on front flash light (not with camera preview) programmatically in Android. I googled for it but the help i found referred me to this page

Does anyo

11条回答
  •  不思量自难忘°
    2020-11-22 00:49

    There's different ways to access Camera Flash in different Android versions. Few APIs stopped working in Lollipop and then it got changed again in Marshmallow. To overcome this, I have created a simple library that I have been using in few of my projects and it's giving good results. It's still incomplete, but you can try to check the code and find the missing pieces. Here's the link - NoobCameraFlash.

    If you just want to integrate in your code, you can use gradle for that. Here's the instructions (Taken directly from the Readme) -

    Step 1. Add the JitPack repository to your build file. Add it in your root build.gradle at the end of repositories:

    allprojects {
            repositories {
                ...
                maven { url "https://jitpack.io" }
            }
    }
    

    Step 2. Add the dependency

    dependencies {
            compile 'com.github.Abhi347:NoobCameraFlash:0.0.1'
      }
    

    Usage

    Initialize the NoobCameraManager singleton.

    NoobCameraManager.getInstance().init(this);
    

    You can optionally set the Log Level for debug logging. Logging uses LumberJack library. The default LogLevel is LogLevel.None

    NoobCameraManager.getInstance().init(this, LogLevel.Verbose);
    

    After that you just need to call the singleton to turn on or off the camera flash.

    NoobCameraManager.getInstance().turnOnFlash();
    NoobCameraManager.getInstance().turnOffFlash();
    

    You have to take care of the runtime permissions to access Camera yourself, before initializing the NoobCameraManager. In version 0.1.2 or earlier we used to provide support for permissions directly from the library, but due to dependency on the Activity object, we have to remove it.

    It's easy to toggle Flash too

    if(NoobCameraManager.getInstance().isFlashOn()){
        NoobCameraManager.getInstance().turnOffFlash();
    }else{
        NoobCameraManager.getInstance().turnOnFlash();
    }
    

提交回复
热议问题