Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Android Torch only works with front face camera on

Discussion in 'Scripting' started by Aldo, Jan 26, 2017.

  1. Aldo

    Aldo

    Joined:
    Aug 10, 2012
    Posts:
    173
    Unity 5.5.0p1
    I'm working with webcam textures to play with some kind of AR.

    Code (CSharp):
    1. //Front Camera
    2. mCamera = new WebCamTexture(devices[devices.Length - 1].name);
    3.  
    4. //Back Camera
    5. mCamera2 = new WebCamTexture();
    Whenever I have the "mCamera2" in Play(); I can't get the torch to be on.

    (I just have 1 camera in Play() at a time)

    Here my torch code:

    Code (CSharp):
    1. #if UNITY_ANDROID
    2.     private AndroidJavaObject cameraAnd = null;
    3. #endif
    4.  
    5.     void FlashLightOn()
    6.     {
    7. #if UNITY_ANDROID
    8.         if (cameraAnd == null)
    9.         {
    10.             AndroidJavaClass cameraClass = new AndroidJavaClass("android.hardware.Camera");
    11.             cameraAnd = cameraClass.CallStatic<AndroidJavaObject>("open");
    12.         }
    13.         AndroidJavaObject cameraParameters = cameraAnd.Call<AndroidJavaObject>("getParameters");
    14.         cameraParameters.Call("setFlashMode", "torch");
    15.         cameraAnd.Call("setParameters", cameraParameters);
    16.         cameraAnd.Call("startPreview");
    17. #endif
    18.     }
    19.  
    20. // the off for the torch if anyone is searching for it
    21.  
    22.     void FlashLightOff()
    23.     {
    24. #if UNITY_ANDROID
    25.         if (cameraAnd != null)
    26.         {
    27.             AndroidJavaObject cameraParameters = cameraAnd.Call<AndroidJavaObject>("getParameters");
    28.             cameraParameters.Call("setFlashMode", "off");
    29.             cameraAnd.Call("setParameters", cameraParameters);
    30.             cameraAnd.Call("release");
    31.             cameraAnd = null;
    32.         }
    33. #endif
    34.     }
    The code works great, except for the fact that only works when there is no active webcam texture OR the front camera webcam is on.

    Also while testing I had the torch on, swapped camera and had a crash...
     
  2. Aldo

    Aldo

    Joined:
    Aug 10, 2012
    Posts:
    173
    Bump
     
  3. Aldo

    Aldo

    Joined:
    Aug 10, 2012
    Posts:
    173
    Second and last bump