Turn on android flashlight Qt6
-
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 foundQCamera
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.
-
@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...
-
@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.
-
@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. -
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 returnsfalse
!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?
-
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
-
@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.pdfand simply use the QAndroidJniObject class to call those functions from c++ side
https://doc.qt.io/qt-5/qandroidjniobject.htmlI 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 :)
-
@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 lineCameraManager 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. -
@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;