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. QML Camera not working on Android
QtWS25 Last Chance

QML Camera not working on Android

Scheduled Pinned Locked Moved Solved Mobile and Embedded
2 Posts 2 Posters 426 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.
  • MesrineM Offline
    MesrineM Offline
    Mesrine
    wrote on last edited by
    #1

    Hello everyone,
    I have a custom qml type defined as:

    import QtQuick 2.0
    import QtMultimedia
    import QtQrDec
    import MyDesigns
    Item
    {
        id:root
        signal gotdata(string data);
        function stop() {
            timer.stop();
            camera.stop();
            startcamera.visible=true;
            }
    
        VideoOutput {
            id: videoOutput
            anchors.fill: root
        }
        MediaDevices {
            id: devices
        }
        Camera {
            id: camera
            active: false
            cameraDevice: devices.defaultVideoInput
            onErrorOccurred: (error,errorString)=> {
                                 console.log( devices.videoInputs);
                                 console.log(errorString)
                                 console.log(error)
                             }
    
        }
        Connections {
            target: QRImageDecoder
            function onText_changed(boo) {
                root.gotdata(QRImageDecoder.text)
            }
        }
        CaptureSession {
            id: capturesession
            camera: camera
            videoOutput: videoOutput
            imageCapture: ImageCapture {
                id: imageCapture
                onImageCaptured: {
                    QRImageDecoder.source = imageCapture.preview
                }
    
            }
        }
        Timer {
            id: timer
            interval: 500; running: false; repeat: true
            onTriggered: imageCapture.capture()
        }
        MyButton
        {
            id:startcamera
            anchors.centerIn: root
            text:qsTr("ScanQr")
            onClicked:
            {
    
                camera.start();
                timer.start();
                startcamera.visible=false;
            }
            width:100
            height:width*0.5
        }
    }
    

    This work for desktop platforms and show the camera video.
    The only problem with desktop applications is this one.

    I have compiled my app also for android, but when deploying the app from qtCreator to my physical device
    qml returns :

    qml     : : []
    qml     : : Failed to open camera
    qml     : : 1
    

    The devices.videoInputs list is empty and the camera can not be opened. I am using USB debugging to deploy to my physical device.
    Also I have check and the AndroidManifest.xml has the camera permission.

    <?xml version="1.0"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="org.qtproject.example.nftminter"
        android:installLocation="auto"
        android:versionCode="1"
        android:versionName="1.0">
        <uses-permission android:name="android.permission.INTERNET" />
        <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
        <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
        <uses-permission android:name="android.permission.CAMERA" />
        <uses-permission android:name="android.permission.RECORD_AUDIO" />
        <uses-permission android:name="android.permission.BLUETOOTH" />
        <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
        
        <supports-screens
            android:anyDensity="true"
            android:largeScreens="true"
            android:normalScreens="true"
            android:smallScreens="true" />
        <application
            android:name="org.qtproject.qt.android.bindings.QtApplication"
            android:hardwareAccelerated="true"
            android:label="nftminter"
            android:requestLegacyExternalStorage="true"
            android:allowNativeHeapPointerTagging="false"
            android:allowBackup="true"
            android:fullBackupOnly="false">
            <activity
                android:name="org.qtproject.qt.android.bindings.QtActivity"
                android:configChanges="orientation|uiMode|screenLayout|screenSize|smallestScreenSize|layoutDirection|locale|fontScale|keyboard|keyboardHidden|navigation|mcc|mnc|density"
                android:label="nftminter"
                android:launchMode="singleTop"
                android:screenOrientation="unspecified"
                android:exported="true">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
    
              <meta-data
                    android:name="android.app.lib_name"
                    android:value="nftminter" />
    
              <meta-data
                    android:name="android.app.arguments"
                    android:value="" />
    
                <meta-data
                    android:name="android.app.extract_android_style"
                    android:value="minimal" />
            </activity>
    
            <provider
                android:name="androidx.core.content.FileProvider"
                android:authorities="${applicationId}.qtprovider"
                android:exported="false"
                android:grantUriPermissions="true">
                <meta-data
                    android:name="android.support.FILE_PROVIDER_PATHS"
                    android:resource="@xml/qtprovider_paths"/>
            </provider>
        </application>
    </manifest>
    

    Any help on how to use the camera on Android will be appreciated.
    Many thanks for your time.

    MesrineM 1 Reply Last reply
    0
    • MesrineM Mesrine

      Hello everyone,
      I have a custom qml type defined as:

      import QtQuick 2.0
      import QtMultimedia
      import QtQrDec
      import MyDesigns
      Item
      {
          id:root
          signal gotdata(string data);
          function stop() {
              timer.stop();
              camera.stop();
              startcamera.visible=true;
              }
      
          VideoOutput {
              id: videoOutput
              anchors.fill: root
          }
          MediaDevices {
              id: devices
          }
          Camera {
              id: camera
              active: false
              cameraDevice: devices.defaultVideoInput
              onErrorOccurred: (error,errorString)=> {
                                   console.log( devices.videoInputs);
                                   console.log(errorString)
                                   console.log(error)
                               }
      
          }
          Connections {
              target: QRImageDecoder
              function onText_changed(boo) {
                  root.gotdata(QRImageDecoder.text)
              }
          }
          CaptureSession {
              id: capturesession
              camera: camera
              videoOutput: videoOutput
              imageCapture: ImageCapture {
                  id: imageCapture
                  onImageCaptured: {
                      QRImageDecoder.source = imageCapture.preview
                  }
      
              }
          }
          Timer {
              id: timer
              interval: 500; running: false; repeat: true
              onTriggered: imageCapture.capture()
          }
          MyButton
          {
              id:startcamera
              anchors.centerIn: root
              text:qsTr("ScanQr")
              onClicked:
              {
      
                  camera.start();
                  timer.start();
                  startcamera.visible=false;
              }
              width:100
              height:width*0.5
          }
      }
      

      This work for desktop platforms and show the camera video.
      The only problem with desktop applications is this one.

      I have compiled my app also for android, but when deploying the app from qtCreator to my physical device
      qml returns :

      qml     : : []
      qml     : : Failed to open camera
      qml     : : 1
      

      The devices.videoInputs list is empty and the camera can not be opened. I am using USB debugging to deploy to my physical device.
      Also I have check and the AndroidManifest.xml has the camera permission.

      <?xml version="1.0"?>
      <manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="org.qtproject.example.nftminter"
          android:installLocation="auto"
          android:versionCode="1"
          android:versionName="1.0">
          <uses-permission android:name="android.permission.INTERNET" />
          <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
          <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
          <uses-permission android:name="android.permission.CAMERA" />
          <uses-permission android:name="android.permission.RECORD_AUDIO" />
          <uses-permission android:name="android.permission.BLUETOOTH" />
          <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
          
          <supports-screens
              android:anyDensity="true"
              android:largeScreens="true"
              android:normalScreens="true"
              android:smallScreens="true" />
          <application
              android:name="org.qtproject.qt.android.bindings.QtApplication"
              android:hardwareAccelerated="true"
              android:label="nftminter"
              android:requestLegacyExternalStorage="true"
              android:allowNativeHeapPointerTagging="false"
              android:allowBackup="true"
              android:fullBackupOnly="false">
              <activity
                  android:name="org.qtproject.qt.android.bindings.QtActivity"
                  android:configChanges="orientation|uiMode|screenLayout|screenSize|smallestScreenSize|layoutDirection|locale|fontScale|keyboard|keyboardHidden|navigation|mcc|mnc|density"
                  android:label="nftminter"
                  android:launchMode="singleTop"
                  android:screenOrientation="unspecified"
                  android:exported="true">
                  <intent-filter>
                      <action android:name="android.intent.action.MAIN" />
                      <category android:name="android.intent.category.LAUNCHER" />
                  </intent-filter>
      
                <meta-data
                      android:name="android.app.lib_name"
                      android:value="nftminter" />
      
                <meta-data
                      android:name="android.app.arguments"
                      android:value="" />
      
                  <meta-data
                      android:name="android.app.extract_android_style"
                      android:value="minimal" />
              </activity>
      
              <provider
                  android:name="androidx.core.content.FileProvider"
                  android:authorities="${applicationId}.qtprovider"
                  android:exported="false"
                  android:grantUriPermissions="true">
                  <meta-data
                      android:name="android.support.FILE_PROVIDER_PATHS"
                      android:resource="@xml/qtprovider_paths"/>
              </provider>
          </application>
      </manifest>
      

      Any help on how to use the camera on Android will be appreciated.
      Many thanks for your time.

      MesrineM Offline
      MesrineM Offline
      Mesrine
      wrote on last edited by
      #2

      @Mesrine
      The problem is related to Permissions.
      I was thinking that the camera start() function would do that but no.

      So I have reimplemented the qml type adding a function to ask for permission like:

      void QRImageDecoder::requestPermision()
      {
      #if QT_CONFIG(permissions)
          QCameraPermission cPermission;
          switch (qApp->checkPermission(cPermission)) {
          case Qt::PermissionStatus::Undetermined:
              qApp->requestPermission(cPermission, this,
                                      &QRImageDecoder::requestPermision);
              return;
          case Qt::PermissionStatus::Denied:
              emit permissionRequested(false);
              return;
          case Qt::PermissionStatus::Granted:
              emit permissionRequested(true);
              return;
          }
      #endif
      }
      

      then
      the qml element

      import QtQuick 2.0
      import QtMultimedia
      import QtQrDec
      import MyDesigns
      Item
      {
          id:root
          signal gotdata(string data);
          function stop() {
              timer.stop();
              camera.stop();
              startcamera.visible=true;
              }
      
          VideoOutput {
              id: videoOutput
              anchors.fill: root
          }
          MediaDevices {
              id: devices
          }
          Camera {
              id: camera
              active: false
              cameraDevice: devices.defaultVideoInput
              onErrorOccurred: (error,errorString)=> {
                                   console.log( devices.videoInputs);
                                   console.log(errorString)
                                   console.log(error)
                               }
      
          }
          Connections {
              target: QRImageDecoder
              function onText_changed(boo) {
                  root.gotdata(QRImageDecoder.text)
              }
          }
          Connections {
              target: QRImageDecoder
              function onPermissionRequested(boo) {
                  if(boo)
                  {
                      camera.start();
                      timer.start();
                      startcamera.visible=false;
                  }
              }
          }
      
          CaptureSession {
              id: capturesession
              camera: camera
              videoOutput: videoOutput
              imageCapture: ImageCapture {
                  id: imageCapture
                  onImageCaptured: {
                      QRImageDecoder.source = imageCapture.preview
                  }
      
              }
          }
          Timer {
              id: timer
              interval: 500; running: false; repeat: true
              onTriggered: imageCapture.capture()
          }
          MyButton
          {
              id:startcamera
              anchors.centerIn: root
              text:qsTr("ScanQr")
              onClicked:
              {
                  QRImageDecoder.requestPermision();
              }
              width:100
              height:width*0.5
          }
      }
      
      1 Reply Last reply
      0
      • MesrineM Mesrine has marked this topic as solved on

      • Login

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