4

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();
}
fadden
  • 51,356
  • 5
  • 116
  • 166
Wayne Johnson
  • 214
  • 3
  • 18
  • 1
    Not a duplicate... different question entirely. I don't want to use the Camera methods and am trying to determine if there's a way to turn the LED on/off WITHOUT accessing the Camera methods – Wayne Johnson Jan 01 '16 at 00:40
  • Why are you trying to do something other than the way that's provided for you? – Louis Wasserman Jan 01 '16 at 00:51
  • Again...if you read through the post, you will see that I can't do this because the image recognition software is using the camera window as a scanner and if I try to implement the flashlight methods, it takes over the preview window and the image scanner doesn't function. The flashlight works fine, but the scanner doesn't, so it basically cripples the app. – Wayne Johnson Jan 01 '16 at 00:59
  • What scanner do you use? Is it open source? – Alex Cohn Jan 01 '16 at 18:11
  • It's from Moodstocks API... https://moodstocks.com/static/doc/android/index.html – Wayne Johnson Jan 01 '16 at 23:36
  • Hello did you figure it out? Running into the same thing – Snake Jan 25 '19 at 07:50
  • Did you figure it out? Iam running into the same thing – Snake Jan 25 '19 at 07:51
  • Nope, never was able to use the LED without using the camera methods and if another function uses the camera methods, then you can't access the LED directly. – Wayne Johnson Jan 26 '19 at 01:07

3 Answers3

1

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().

Alex Cohn
  • 56,089
  • 9
  • 113
  • 307
  • Okay, thank you for your response. I'm trying to figure out a way to Override a method from the image recognition API and incorporate the flashlight so I can turn it on/off while the image recognition scanner (which uses the camera) is running. Perhaps I can "share" the camera object inside one of those methods. Any idea on how I would start that process if the API method is already accessing the camera? – Wayne Johnson Jan 01 '16 at 16:41
0

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();
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
  • 2
    I already have this code...if you read the question, I'm trying to access the Torch/Flashlight WITHOUT using the Camera Methods. – Wayne Johnson Jan 01 '16 at 00:38
0

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.

Chitholian
  • 432
  • 1
  • 10
  • 19