Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. Turn on android flashlight Qt6
Forum Updated to NodeBB v4.3 + New Features

Turn on android flashlight Qt6

Scheduled Pinned Locked Moved Unsolved Mobile and Embedded
10 Posts 4 Posters 1.2k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    MysteriousBits
    wrote on last edited by MysteriousBits
    #1

    Hello! Currently I'm trying to make an mobile app and I need to turn on the flash light of a mobile device. Actually I want to use it as a torch(On/off/blink etc). I searched in the qt docs and found QCamera class. But I'm unable to turn the light on. I'm using Qt 6.3.1. Can anyone help?

    I'm doing something like this:
    Currently I'm trying to make an mobile app and I need to turn on the flash light of a mobile device. Actually I want to use it as a torch(On/off/blink etc). I searched in the qt docs and found QCamera class. But I'm unable to turn the light on. I'm using Qt 6.3.1. Can anyone help?

    I'm doing something like this:

    // In constructor of a widget class,
    cam = new QCamera(QCameraDevice::BackFace, this); //cam is declared in the header file
    
    // In a function, called after a button click,
    // cam->setFlashMode(QCamera::FlashOn);
    cam->setTorchMode(QCamera::TorchOn);
    cam->start();
    

    I added this code inside a function and called it after a button-click event. But when I click the button, nothing happens.

    jsulmJ J.HilkJ 2 Replies Last reply
    0
    • M MysteriousBits

      Hello! Currently I'm trying to make an mobile app and I need to turn on the flash light of a mobile device. Actually I want to use it as a torch(On/off/blink etc). I searched in the qt docs and found QCamera class. But I'm unable to turn the light on. I'm using Qt 6.3.1. Can anyone help?

      I'm doing something like this:
      Currently I'm trying to make an mobile app and I need to turn on the flash light of a mobile device. Actually I want to use it as a torch(On/off/blink etc). I searched in the qt docs and found QCamera class. But I'm unable to turn the light on. I'm using Qt 6.3.1. Can anyone help?

      I'm doing something like this:

      // In constructor of a widget class,
      cam = new QCamera(QCameraDevice::BackFace, this); //cam is declared in the header file
      
      // In a function, called after a button click,
      // cam->setFlashMode(QCamera::FlashOn);
      cam->setTorchMode(QCamera::TorchOn);
      cam->start();
      

      I added this code inside a function and called it after a button-click event. But when I click the button, nothing happens.

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @MysteriousBits said in Turn on android flash light Qt6:

      QCamera cam(QCameraDevice::BackFace, this);
      // cam->setFlashMode(QCamera::FlashOn);
      cam->setTorchMode(QCamera::TorchOn);
      cam->start();

      This will not even compile.

      It looks like you allocated cam on the stack. That means it will be destroyed as soon as the function terminates...

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      M 1 Reply Last reply
      0
      • jsulmJ jsulm

        @MysteriousBits said in Turn on android flash light Qt6:

        QCamera cam(QCameraDevice::BackFace, this);
        // cam->setFlashMode(QCamera::FlashOn);
        cam->setTorchMode(QCamera::TorchOn);
        cam->start();

        This will not even compile.

        It looks like you allocated cam on the stack. That means it will be destroyed as soon as the function terminates...

        M Offline
        M Offline
        MysteriousBits
        wrote on last edited by MysteriousBits
        #3

        @jsulm Oh sorry. I added that line while posting to make things short and easy to understand and made that mistake. Actually it is a pointer and I initialized in the constructor of another class. Edited the post, thanks.

        jsulmJ 1 Reply Last reply
        0
        • M MysteriousBits

          @jsulm Oh sorry. I added that line while posting to make things short and easy to understand and made that mistake. Actually it is a pointer and I initialized in the constructor of another class. Edited the post, thanks.

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @MysteriousBits Connect a slot to https://doc.qt.io/qt-5/qcamera.html#errorOccurred and see whether you get any errors.
          Also, does your app request the access rights for the camera? Mobile platforms are quite restrictive.

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          M 1 Reply Last reply
          1
          • jsulmJ jsulm

            @MysteriousBits Connect a slot to https://doc.qt.io/qt-5/qcamera.html#errorOccurred and see whether you get any errors.
            Also, does your app request the access rights for the camera? Mobile platforms are quite restrictive.

            M Offline
            M Offline
            MysteriousBits
            wrote on last edited by
            #5

            Hello @jsulm . First, sorry for the late reply. But I have found something interesting.

            Also, does your app request the access rights for the camera?

            Yes. When the app starts after installation it asks me for camera permission and of course, I allow.

            @MysteriousBits Connect a slot to https://doc.qt.io/qt-5/qcamera.html#errorOccurred and see whether you get any errors.

            I did exactly what you said. But no errors have been generated.

            What I have found interesting is, I've tried printing the return value of isFlashModeSupported() and it returns false!

            QString str = cam->isFlashModeSupported(QCamera::FlashOn) ? "Flash: Yes" : "Flash: No";
            ui->Flash->setText(str); // str = "Flash: No"
            

            I'm using a phone which has controllable flash light. So what can be the reason for this kind of behaviour?

            M 1 Reply Last reply
            0
            • M MysteriousBits

              Hello @jsulm . First, sorry for the late reply. But I have found something interesting.

              Also, does your app request the access rights for the camera?

              Yes. When the app starts after installation it asks me for camera permission and of course, I allow.

              @MysteriousBits Connect a slot to https://doc.qt.io/qt-5/qcamera.html#errorOccurred and see whether you get any errors.

              I did exactly what you said. But no errors have been generated.

              What I have found interesting is, I've tried printing the return value of isFlashModeSupported() and it returns false!

              QString str = cam->isFlashModeSupported(QCamera::FlashOn) ? "Flash: Yes" : "Flash: No";
              ui->Flash->setText(str); // str = "Flash: No"
              

              I'm using a phone which has controllable flash light. So what can be the reason for this kind of behaviour?

              M Offline
              M Offline
              mvuori
              wrote on last edited by
              #6

              Possible reasons: 1) Not implemented in Qt 6, 2) Bug.

              Note that even if Qt would claim that it is supported, it might not actually work. On my phone, on Qt 5.12.3, that mode fires prematurely causing a very underdeveloped image, while other modes, supposedly supported, don't work at all

              1 Reply Last reply
              0
              • M MysteriousBits

                Hello! Currently I'm trying to make an mobile app and I need to turn on the flash light of a mobile device. Actually I want to use it as a torch(On/off/blink etc). I searched in the qt docs and found QCamera class. But I'm unable to turn the light on. I'm using Qt 6.3.1. Can anyone help?

                I'm doing something like this:
                Currently I'm trying to make an mobile app and I need to turn on the flash light of a mobile device. Actually I want to use it as a torch(On/off/blink etc). I searched in the qt docs and found QCamera class. But I'm unable to turn the light on. I'm using Qt 6.3.1. Can anyone help?

                I'm doing something like this:

                // In constructor of a widget class,
                cam = new QCamera(QCameraDevice::BackFace, this); //cam is declared in the header file
                
                // In a function, called after a button click,
                // cam->setFlashMode(QCamera::FlashOn);
                cam->setTorchMode(QCamera::TorchOn);
                cam->start();
                

                I added this code inside a function and called it after a button-click event. But when I click the button, nothing happens.

                J.HilkJ Offline
                J.HilkJ Offline
                J.Hilk
                Moderators
                wrote on last edited by J.Hilk
                #7

                @MysteriousBits Sadly, mobile is theses days treaded like a step child by the QtCompany.

                What I would suggest:
                check this link for a simply java code to turn on/off the flashlight
                https://www.geeksforgeeks.org/how-to-build-a-simple-flashlight-torchlight-android-app/

                the only thing you'll really need is the java code in step 3

                extend your QtActivity with your own java class. I couldn't find anything step by step here. But I found this rather old, but still valid KDAB presentation:
                http://amin-ahmadi.com/wp-content/uploads/2015/04/bogdanvatra_extending_qt_android_apps_with_jni.pdf

                and simply use the QAndroidJniObject class to call those functions from c++ side
                https://doc.qt.io/qt-5/qandroidjniobject.html

                I know, this requires some toe dipping in Java code, no-one really likes that, but eventually you're forced to anyway, if you're developing for Android :)


                Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                Q: What's that?
                A: It's blue light.
                Q: What does it do?
                A: It turns blue.

                M 1 Reply Last reply
                1
                • J.HilkJ J.Hilk

                  @MysteriousBits Sadly, mobile is theses days treaded like a step child by the QtCompany.

                  What I would suggest:
                  check this link for a simply java code to turn on/off the flashlight
                  https://www.geeksforgeeks.org/how-to-build-a-simple-flashlight-torchlight-android-app/

                  the only thing you'll really need is the java code in step 3

                  extend your QtActivity with your own java class. I couldn't find anything step by step here. But I found this rather old, but still valid KDAB presentation:
                  http://amin-ahmadi.com/wp-content/uploads/2015/04/bogdanvatra_extending_qt_android_apps_with_jni.pdf

                  and simply use the QAndroidJniObject class to call those functions from c++ side
                  https://doc.qt.io/qt-5/qandroidjniobject.html

                  I know, this requires some toe dipping in Java code, no-one really likes that, but eventually you're forced to anyway, if you're developing for Android :)

                  M Offline
                  M Offline
                  MysteriousBits
                  wrote on last edited by
                  #8

                  @J-Hilk Thanks for your reply. I was trying to do what you said. But I never worked with android or java before :) So it is a bit hard for me. However I've managed to extend qt with java and wrote this java code reading from several sources

                  public class Torch {
                      static void toggleTorch(boolean condition)
                      {
                          CameraManager camManager = (CameraManager)getSystemService(Context.CAMERA_SERVICE);
                          String cameraId;
                          try {
                              cameraId = camManager.getCameraIdList()[0];
                              camManager.setTorchMode(cameraId, condition);
                          } catch (CameraAccessException e) {
                              e.getMessage();
                          }
                      }
                  }
                  

                  But this code isn't even compiling : ( and giving can not find symbol error in line CameraManager camManager = (CameraManager)getSystemService(Context.CAMERA_SERVICE);. As I've no knowledge in java I'm unable to understand it. Now I feel like I have to leave this project or look for other options.

                  J.HilkJ 1 Reply Last reply
                  0
                  • M MysteriousBits

                    @J-Hilk Thanks for your reply. I was trying to do what you said. But I never worked with android or java before :) So it is a bit hard for me. However I've managed to extend qt with java and wrote this java code reading from several sources

                    public class Torch {
                        static void toggleTorch(boolean condition)
                        {
                            CameraManager camManager = (CameraManager)getSystemService(Context.CAMERA_SERVICE);
                            String cameraId;
                            try {
                                cameraId = camManager.getCameraIdList()[0];
                                camManager.setTorchMode(cameraId, condition);
                            } catch (CameraAccessException e) {
                                e.getMessage();
                            }
                        }
                    }
                    

                    But this code isn't even compiling : ( and giving can not find symbol error in line CameraManager camManager = (CameraManager)getSystemService(Context.CAMERA_SERVICE);. As I've no knowledge in java I'm unable to understand it. Now I feel like I have to leave this project or look for other options.

                    J.HilkJ Offline
                    J.HilkJ Offline
                    J.Hilk
                    Moderators
                    wrote on last edited by
                    #9

                    @MysteriousBits did you add the correct imports ?

                    I think these are needed for camera access:

                    import android.hardware.camera2.CameraAccessException;
                    import android.hardware.camera2.CameraManager;


                    Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                    Q: What's that?
                    A: It's blue light.
                    Q: What does it do?
                    A: It turns blue.

                    M 1 Reply Last reply
                    0
                    • J.HilkJ J.Hilk

                      @MysteriousBits did you add the correct imports ?

                      I think these are needed for camera access:

                      import android.hardware.camera2.CameraAccessException;
                      import android.hardware.camera2.CameraManager;

                      M Offline
                      M Offline
                      MysteriousBits
                      wrote on last edited by
                      #10

                      @J-Hilk Yes I did.

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved