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

Discussion in 'Tutorials & Examples' started by Johan Schoeman, Dec 18, 2014.

  1. Johan Schoeman

    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.
     

    Attached Files:

    Last edited: Jan 2, 2015
  2. adrianstanescu85

    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
     
  3. adrianstanescu85

    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.

    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
     
  4. Johan Schoeman

    Johan Schoeman Expert Licensed User

    Try this one...
     

    Attached Files:

    DonManfred likes this.
  5. adrianstanescu85

    adrianstanescu85 Active Member 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
     
  6. Johan Schoeman

    Johan Schoeman Expert Licensed User

    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

    Code:
    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());
         }
    }
     

    Attached Files:

  7. adrianstanescu85

    adrianstanescu85 Active Member Licensed User

    Well... this time magic happened! Whatever you changed in the mean time it worked!
     
    Johan Schoeman likes this.
  8. Johan Schoeman

    Johan Schoeman Expert Licensed User

    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: Dec 19, 2014
  9. adrianstanescu85

    adrianstanescu85 Active Member Licensed User

    Great! Where do you use the startPreview? With what object?
     
  10. Johan Schoeman

    Johan Schoeman Expert Licensed User


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

    static Camera m_Camera;

    and further down


    Code:
    // 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: Dec 28, 2014
  11. stari

    stari Active Member Licensed User

    Just what i need. Now i can switch LED from service. Tested. OK.
     
    Johan Schoeman likes this.
  12. adrianstanescu85

    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?
     
  13. Johan Schoeman

    Johan Schoeman Expert Licensed User

    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
     
  14. Johan Schoeman

    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.
     

    Attached Files:

    Last edited: Feb 8, 2015
  15. DavideV

    DavideV Active Member 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 ;)
     

    Attached Files:

    Johan Schoeman likes this.
  16. Johan Schoeman

    Johan Schoeman Expert Licensed User

    It is more or less the drift of the code in JHSTORCH_JAVA_V2. Great addition to JHSTORCH_JAVA. Thanks!
     
  17. Johan Schoeman

    Johan Schoeman Expert Licensed User

    Just for fun....Java code inside the B4A project. It is also posted here but as a B4A library
     

    Attached Files:

    xpectmore and ilan like this.
  18. Inman

    Inman Well-Known Member Licensed User

    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.
    Code:
    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
    Code:
    _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: May 17, 2015
  19. DonManfred

    DonManfred Expert Licensed User

    Can you please edit the thread title? All caps is like SHOUTING
     
  20. susu

    susu Well-Known Member Licensed User

    @Johan Schoeman My phone have front flash, can I use your code to turn it on? Thanks.
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice