Android Example Turn your camera flash light into a torch - also with Java code inside the B4A project

Johan Schoeman

Expert
Licensed User
See attached project. I have created this simple torch (on / off) by making use of java code found on the web and compiling it with Simple Library Compiler. Library files inside the /files folder. Copy it to you additional library folder (fiddleAround.jar and FiddleAround.xml) and refresh libs --> select/enable FiddleAround in the libs tab

EDIT: JHSTORCH_v1.zip should hopefully address the issues raised in the posts below. Copy / replace fiddleAround.jar and fiddleAround.xml to/in your additional library folder. (23)

EDIT: JHSTORCH_v2.zip - just tidied up the buttons a bit. The library files are in the /files folder of the project.

You can also try this. It is a torch but also includes the ability to generate Morse codes via the camera flash.
 

Attachments

Last edited:

adrianstanescu85

Active Member
Licensed User
Hi

I've just tried the project you mentioned, it compiled fine, but... it doesn't light up the flash. I've tested it on a Samsung Galaxy Note 3.

Adrian
 

adrianstanescu85

Active Member
Licensed User
Hi Johan,

I've downloaded the updated project, replaced the jar and xml files, but...it won't even compile.

It does not recognize the "flash1" type.

Dim mf As flash1
However, I've changed that to "flash", but then it won't compile the two Logs, so I've deactivated those as well. It compiled this time, but no flash was on. I also tested the app on Samsung Grand Neo, same results (not working).

Adrian
 

Johan Schoeman

Expert
Licensed User
Hi Johan,

I've downloaded the updated project, replaced the jar and xml files, but...it won't even compile.

It does not recognize the "flash1" type.



However, I've changed that to "flash", but then it won't compile the two Logs, so I've deactivated those as well. It compiled this time, but no flash was on. I also tested the app on Samsung Grand Neo, same results (not working).

Adrian
Try this one...
 

Attachments

adrianstanescu85

Active Member
Licensed User
Hi Johan,

The torch doesn't light up, but here is the log:

LogCat connected to: 7fcf4a06
--------- beginning of /dev/log/system
--------- beginning of /dev/log/main
** Activity (main) Pause, UserClosed = true **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
torch
Camera Flash - Camera failed: Fail to connect to camera service
Camera Flash - Camera failed: Fail to connect to camera service
Camera Flash - Camera failed: Fail to connect to camera service
Camera Flash - Camera failed: Fail to connect to camera service
Camera Flash - Camera failed: Fail to connect to camera service
Camera Flash - Camera failed: Fail to connect to camera service
Camera Flash - Flash Turned Off VERSION.SDK_INT=19
MODEL=SM-N9005
torch
Camera Flash - Camera failed: Fail to connect to camera service
Camera Flash - Camera failed: Fail to connect to camera service
Camera Flash - Camera failed: Fail to connect to camera service
Camera Flash - Flash Turned Off VERSION.SDK_INT=19
MODEL=SM-N9005
torch
Camera Flash - Flash Turned Off VERSION.SDK_INT=19
MODEL=SM-N9005
torch
Camera Flash - Camera failed: Fail to connect to camera service
Camera Flash - Camera failed: Fail to connect to camera service
Camera Flash - Camera failed: Fail to connect to camera service
Camera Flash - Camera failed: Fail to connect to camera service
Camera Flash - Flash Turned Off VERSION.SDK_INT=19
MODEL=SM-N9005
torch
Camera Flash - Flash Turned Off VERSION.SDK_INT=19
MODEL=SM-N9005
Camera Flash - Flash Turned Off VERSION.SDK_INT=19
MODEL=SM-N9005
Camera Flash - Flash Turned Off VERSION.SDK_INT=19
MODEL=SM-N9005
torch
Camera Flash - Camera failed: Fail to connect to camera service
Camera Flash - Camera failed: Fail to connect to camera service
Camera Flash - Camera failed: Fail to connect to camera service
Camera Flash - Flash Turned Off VERSION.SDK_INT=19
MODEL=SM-N9005
torch
Camera Flash - Camera failed: Fail to connect to camera service
I have to say that right after I click Torch off and then Torch on, the log says "torch" instead of "Camera Flash - Camera failed...", but the torch doesn't actually fire.

Adrian
 

Johan Schoeman

Expert
Licensed User
Hi Johan,

The torch doesn't light up, but here is the log:



I have to say that right after I click Torch off and then Torch on, the log says "torch" instead of "Camera Flash - Camera failed...", but the torch doesn't actually fire.

Adrian
Try the attached - different jar and xml in the zip file. Have added a startPreview() to the java code. Also edit the manifest file of the project and change the corresponding line in the manifest to

<uses-sdk android:minSdkVersion="4" android:targetSdkVersion="19"/>

I see Stackoverflow reports all kinds of issues wrt Samsung and using Torch mode. Strange that S4 is happy with whatever I throw at it. If this does not help we might have to call in the "doctors" to help find a cure to the problem.

This is the java code

B4X:
package com.johan.flash1;

import android.hardware.Camera;
import android.hardware.Camera.Parameters;
//import android.hardware.Camera.Autofocus;
import android.util.Log;
import android.content.Context;
import android.os.Build;
import android.annotation.TargetApi;

import java.io.IOException;
import java.util.List;

import anywheresoftware.b4a.BA.ShortName;
import anywheresoftware.b4a.BA.Version;


@ShortName("flash1")
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1)

public class flash1
{
    static Camera m_Camera;
    static Parameters cameraParameters;
   

    public String TurnFlashOn(boolean turnOn)
    {
               String flashMode;
               flashMode = "";
               try
               {
                        m_Camera = Camera.open();
                        cameraParameters = m_Camera.getParameters();
                        List<String> flashModes = cameraParameters.getSupportedFlashModes();
                        flashMode = cameraParameters.getFlashMode();

                        if(turnOn)
                        {
                                if (!Parameters.FLASH_MODE_TORCH.equals(flashMode))
                                {
                                        // Turn on the flash
                                        if (flashModes.contains(Parameters.FLASH_MODE_TORCH))
                                        {
                                                cameraParameters.setFlashMode(Parameters.FLASH_MODE_ON);
                                                cameraParameters.setFocusMode(Parameters.FOCUS_MODE_INFINITY);
                                                cameraParameters.setFlashMode(Parameters.FLASH_MODE_TORCH);
                                                m_Camera.setParameters(cameraParameters);
                                                m_Camera.startPreview();
                                                flashMode = cameraParameters.getFlashMode();
                                        }
                                }
                        }
                        else
                        {
                                if (!Parameters.FLASH_MODE_OFF.equals(flashMode))
                                {
                                        // Turn off the flash
                                        if (flashModes.contains(Parameters.FLASH_MODE_OFF))
                                        {
                                          cameraParameters.setFlashMode(Parameters.FLASH_MODE_OFF);
                                          m_Camera.setParameters(cameraParameters);
                                        } else
                                        {
                                            flashMode = "Camera Flash - FLASH_MODE_OFF not supported";
                                        }

                                }
                        }
                   }
                   catch (RuntimeException e)
                   {
                     flashMode = "Camera Flash - Camera failed: " + e.getMessage();
                   }
                   return flashMode;
     }

     public String TurnFlashOff()
     {
                StringBuilder result = new StringBuilder(1000);
                String flashMode;
                flashMode = "";
                TurnFlashOn(false);
                if (m_Camera != null)
                {
                        m_Camera.stopPreview();
                        m_Camera.release();
                        m_Camera = null;
                        flashMode = "Camera Flash - Flash Turned Off";
                        result.append("VERSION.SDK_INT=").append(Build.VERSION.SDK_INT).append('\n');
                        result.append("MODEL=").append(Build.MODEL).append('\n');
                }
                return (flashMode + " " + result.toString());
     }
}
 

Attachments

Johan Schoeman

Expert
Licensed User
Well... this time magic happened! Whatever you changed in the mean time it worked!
Added m_Camera.startPreview(); :) My S4 does not require this simple line of code...So, now you have your torch without having to add a panel.
 
Last edited:

Johan Schoeman

Expert
Licensed User
Great! Where do you use the startPreview? With what object?

The java code in post # 7 the exact code used for the last version

static Camera m_Camera;

and further down


B4X:
// Turn on the flash
if (flashModes.contains(Parameters.FLASH_MODE_TORCH))
{
     cameraParameters.setFlashMode(Parameters.FLASH_MODE_ON);
     cameraParameters.setFocusMode(Parameters.FOCUS_MODE_INFINITY);
     cameraParameters.setFlashMode(Parameters.FLASH_MODE_TORCH);
     m_Camera.setParameters(cameraParameters);
     m_Camera.startPreview();
     flashMode = cameraParameters.getFlashMode();
}
It happens in the library - not in the B4A project. You only need to DIM the library and then use the two methods in the library.
 
Last edited:

stari

Active Member
Licensed User
See attached project. I have created this simple torch (on / off) by making use of java code found on the web and compiling it with Simple Library Compiler. Library files inside the /files folder. Copy it to you additional library folder (fiddleAround.jar and FiddleAround.xml) and refresh libs --> select/enable FiddleAround in the libs tab

EDIT: JHSTORCH_v1.zip should hopefully address the issues raised in the posts below. Copy / replace fiddleAround.jar and fiddleAround.xml to/in your additional library folder.
Just what i need. Now i can switch LED from service. Tested. OK.
 

adrianstanescu85

Active Member
Licensed User
If I'm not mistaken, this basically turns on the camera with the flash on, but doesn't actually output the camera view, so only the flash does it thing.

I asked this because I was thinking to use a QR reader (which uses the camera with the flash not turned on) and at the same time turn on the flash with this. I'm thinking the camera would be already in use for QR. Do you think it would work this way?
 

Johan Schoeman

Expert
Licensed User
If I'm not mistaken, this basically turns on the camera with the flash on, but doesn't actually output the camera view, so only the flash does it thing.

I asked this because I was thinking to use a QR reader (which uses the camera with the flash not turned on) and at the same time turn on the flash with this. I'm thinking the camera would be already in use for QR. Do you think it would work this way?
See what I have posted here. Take out the code in QRCPV4 that you don't need. But the flash is already working via the Volume up/down button (but just for QRCPV4 - NOT for QRCPV3)

http://www.b4x.com/android/forum/threads/qr-code-library.41408/#post-259448
 

Johan Schoeman

Expert
Licensed User
The attached project (JHSTORCH_JAVA) was created with B4A (V4.3 Beta) that allows you to add Java code inside the B4A project. It is working on a Samsung S4 mini.

JHSTORCH_JAVA_V2 also attached - java code inside the B4A project is just a bit different.
 

Attachments

Last edited:

DavideV

Active Member
Licensed User
The attached project (JHSTORCH_JAVA) was created with B4A (V4.3 Beta) that allows you to add Java code inside the B4A project. It is working on a Samsung S4 mini.

JHSTORCH_JAVA_V2 also attached - java code inside the B4A project is just a bit different.
Hi Johan, i like your code with inline Java.
I've added some modifications to make it more crash-safe (in JHSTORCH_JAVA only). Tested on S4 mini too with api21 (Cyanogenmod 12 android 5.02) , LG G3mini api 19, S. advance api16 and S.ace2 api 10 all ok!

Note that you can't access the camera while the torch is on.
Best regards ;)
 

Attachments

Johan Schoeman

Expert
Licensed User
Hi Johan, i like your code with inline Java.
I've added some modifications to make it more crash-safe (in JHSTORCH_JAVA only). Tested on S4 mini too with api21 (Cyanogenmod 12 android 5.02) , LG G3mini api 19, S. advance api16 and S.ace2 api 10 all ok!

Note that you can't access the camera while the torch is on.
Best regards ;)
It is more or less the drift of the code in JHSTORCH_JAVA_V2. Great addition to JHSTORCH_JAVA. Thanks!
 

Inman

Well-Known Member
Licensed User
Just for fun....Java code inside the B4A project. It is also posted here but as a B4A library
Could you show me an example of how to use this java code in a Service instead of Activity? I tried but got an error.
B4X:
java.lang.reflect.InvocationTargetException

    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at anywheresoftware.b4j.object.JavaObject.RunMethod(JavaObject.java:130)
    at com.testapp.flashlight._service_start(flashlight.java:157)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:187)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:171)
flashlight.java:157 is
B4X:
_nativeme.RunMethod("TurnFlashOn",(Object[])(anywheresoftware.b4a.keywords.Common.Null));
EDIT:

I fixed it. I had to add NativeMe.RunMethod("IsTorchCapable",Null) before calling NativeMe.RunMethod("TurnFlashOn",Null)
 
Last edited:
Top