Wayne Johnson
Wayne Johnson

Reputation: 224

Is there a way to use flashlight on in Android NOT using the Camera methods?

Please read the question before marking this as a duplicate. I'm trying to access the LED/Flashlight WITHOUT using the Camera methods shown in other code on this site. Thank you.

I'm trying to use the flashlight/torch in Android. I have found and implemented code that works for doing this. The problem I have is that I'm using an image recognition API that uses the camera as an image scanner and they don't have a light on/off function. When I try to override their methods and use the Camera methods to turn the torch on/off, this works, however, the Camera methods now control the window and their Scanner no longer has priority on the screen.

So what I'm trying to determine is... Is there another way to turn on the flashlight/torch without using the Camera methods and preview functions. Anyone have an idea how to bypass the Camera to use the flashlight/torch? Any information would be greatly appreciated.

Here is the code that I currently use, which is working to turn the flashlight on/off, but like I said...this overrides the scanner, and I need the camera/scanner to operate at the same time to recognize my images.

public class Flashlight extends AutoScannerSession {

Camera cam;
private CameraManager mCameraManager;
private Context context;
private Scanner scanner;
private AutoScannerSession.Listener listener;
private boolean advancedListener = false;


public Flashlight(Activity parent, Scanner scanner, Listener listener, SurfaceView preview) {
    super(parent, scanner, listener, preview);
    cam = Camera.open();
    cam.setErrorCallback(new Camera.ErrorCallback() {
        @Override
        public void onError(int error, Camera camera) {
            Log.e("erro", error +"");
        }
    });
    this.context = parent;
    this.mCameraManager = new CameraManager(parent, this, preview);
    this.scanner = scanner;
    this.listener = listener;
    if(listener instanceof AutoScannerSession.AdvancedListener) {
        this.advancedListener = true;
    }

}

@Override
public void start() {
    super.start();
    //flashOn();

}

public void flashOn() {
    android.hardware.Camera.Parameters p = cam.getParameters();
    p.setFlashMode(android.hardware.Camera.Parameters.FLASH_MODE_TORCH);
    cam.setParameters(p);
    //cam.startPreview();
}

public void flashOff() {
    cam.stopPreview();
    cam.release();
}

@Override
public void stop() {
    super.stop();
    flashOff();
}

Upvotes: 4

Views: 3332

Answers (3)

Chitholian
Chitholian

Reputation: 472

I have a Symphony H20 smartphone officially running Android 4.4.2, Kernel 3.4.67, MT6582. Setting Parameters.FLASH_MODE_TORCH as a camera parameter did not work. After trying a few hours I decided to decompile its pre-installed FlashLight app. There, I found that they write 1 in file /sys/class/torch/torch/torch_level to turn on the Camera LED and 0 to turn it off.

Then I tried the same thing and voilla! It worked.

But the same technique did not work in my Winmax W800 Plus with Android 4.4.2, Kernel 3.4.67, MT6572. It even does not have a file like /sys/class/torch/torch/torch_level at all.

Upvotes: 0

Alex Cohn
Alex Cohn

Reputation: 57173

No there is no alternative way to work with flash. But probably you can "share" the camera object with the Scanner.

At any rate, Camera.open() in Activity.onCreate() and turning on flashlight in Activity.onStart() do not look correct. To be a good citizen among other apps, your app should not obtain camera before onResume() and release it no later than onPause().

Upvotes: 1

Code Snippet to turn on camera flash light.

Camera cam = Camera.open();     
Parameters p = cam.getParameters();
p.setFlashMode(Parameters.FLASH_MODE_TORCH);
cam.setParameters(p);
cam.startPreview();

Code snippet to turn off camera led light.

 cam.stopPreview();
  cam.release();

Upvotes: 0

Related Questions