简单的应用程序显示前置摄像头视频反馈在Cordova Android上无法正常工作

huangapple go评论65阅读模式
英文:

Simple app showing front camera video feed not working in Cordova Android

问题

www/index.html 中:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>My Camera App</title>
  </head>
  <body>
    <video id="videoElement"></video>
    <script src="js/app.js"></script>
  </body>
</html>

www/js/app.js 中:

document.addEventListener('deviceready', onDeviceReady, false);

function onDeviceReady() {
  // Get the video element
  var videoElement = document.getElementById('videoElement');

  // Set the source of the video to the device's front camera
  navigator.camera.getPicture(
    function successCallback(imageData) {
      videoElement.src = "data:image/jpeg;base64," + imageData;
      videoElement.play();
    },
    function errorCallback(error) {
      alert('Unable to get the camera: ' + error);
    },
    {
      quality: 50,
      destinationType: Camera.DestinationType.DATA_URL,
      sourceType: Camera.PictureSourceType.CAMERA,
      cameraDirection: Camera.Direction.FRONT
    }
  );
}

config.xml 中添加:

<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" android:required="true" />
<uses-feature android:name="android.hardware.camera.front" android:required="true" />

希望能帮到你。

英文:

I have done some research in different sites but I have not seen an answer that works. Maybe this github issue might be leading to where I want. I have been trying to make a simple android app that when opened, it shows the front or back camera (it shouldn't matter) output (video). I do not have a lot of experience with cordova nor android apps in general.

To recreate this:

cordova create my-camera-app com.example.mycameraapp MyCameraApp
cd my-camera-app
cordova platform add android
cordova plugin add cordova-plugin-camera

And now in www/index.html:

&lt;!DOCTYPE html&gt;
&lt;html&gt;
  &lt;head&gt;
    &lt;meta charset=&quot;utf-8&quot;&gt;
    &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1&quot;&gt;
    &lt;title&gt;My Camera App&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
    &lt;video id=&quot;videoElement&quot;&gt;&lt;/video&gt;
    &lt;script src=&quot;js/app.js&quot;&gt;&lt;/script&gt;
  &lt;/body&gt;
&lt;/html&gt;

and www/js/app.js:

document.addEventListener(&#39;deviceready&#39;, onDeviceReady, false);

function onDeviceReady() {
  // Get the video element
  var videoElement = document.getElementById(&#39;videoElement&#39;);

  // Set the source of the video to the device&#39;s front camera
  navigator.camera.getPicture(
    function successCallback(imageData) {
      videoElement.src = &quot;data:image/jpeg;base64,&quot; + imageData;
      videoElement.play();
    },
    function errorCallback(error) {
      alert(&#39;Unable to get the camera: &#39; + error);
    },
    {
      quality: 50,
      destinationType: Camera.DestinationType.DATA_URL,
      sourceType: Camera.PictureSourceType.CAMERA,
      cameraDirection: Camera.Direction.FRONT
    }
  );
}

Finally adding to the config.xml file:

&lt;uses-permission android:name=&quot;android.permission.CAMERA&quot; /&gt;
&lt;uses-feature android:name=&quot;android.hardware.camera&quot; android:required=&quot;true&quot; /&gt;
&lt;uses-feature android:name=&quot;android.hardware.camera.front&quot; android:required=&quot;true&quot; /&gt;

And running with the device connected to the pc: cordova run android

As you can see, it's nothing fancy as I just want to have the baseline working before doing anything else. But it is not working. I just get the symbol of the videoplayer in the app, but nothing else is happening nor can I press anything. I have tried in two different android devices (Lenovo tablet and Samsung phone), as I saw in my research that compatibility with the device can be the issue sometimes.

In case you also want to see the complete config.xml file:

&lt;?xml version=&#39;1.0&#39; encoding=&#39;utf-8&#39;?&gt;
&lt;widget id=&quot;com.example.mycameraapp&quot; version=&quot;1.0.0&quot; xmlns=&quot;http://www.w3.org/n&gt;
  &lt;name&gt;My Camera App&lt;/name&gt;
  &lt;description&gt;A simple app that displays the front camera video stream&lt;/descri&gt;
  &lt;author email=&quot;youremail@example.com&quot; href=&quot;http://example.com/&quot;&gt;Your Name&lt;/a&gt;
  &lt;content src=&quot;index.html&quot; /&gt;
  &lt;access origin=&quot;*&quot; /&gt;
  &lt;platform name=&quot;android&quot;&gt;
    &lt;preference name=&quot;android-minSdkVersion&quot; value=&quot;21&quot; /&gt;
    &lt;preference name=&quot;android-targetSdkVersion&quot; value=&quot;31&quot; /&gt;
    &lt;preference name=&quot;android-build-tool&quot; value=&quot;29.0.2&quot; /&gt;
    &lt;preference name=&quot;android-enableHybridWebView&quot; value=&quot;true&quot; /&gt;
    &lt;preference name=&quot;android-useLegacyWhitelist&quot; value=&quot;true&quot; /&gt;
    &lt;allow-intent href=&quot;market:*&quot; /&gt;
    &lt;uses-permission android:name=&quot;android.permission.CAMERA&quot; /&gt;
    &lt;uses-feature android:name=&quot;android.hardware.camera&quot; /&gt;
    &lt;uses-feature android:name=&quot;android.hardware.camera.front&quot; /&gt;
  &lt;/platform&gt;
&lt;/widget&gt;

答案1

得分: 1

你不能通过拍照并将其放入视频元素来显示相机反馈。这不像你想象的那么简单。如果你想显示视频反馈,你需要使用

navigator.mediaDevices.getUserMedia

然后将流转换为 MediaStream,将 MediaStreamTrack 添加到 MediaStream,然后将轨道/流链接到视频元素。

查看 https://developer.mozilla.org/en-US/docs/Web/API/MediaStream

英文:

You cannot display the camera feed with taking a picture and stuffing it to the video element. It's not as straight forward as you think it is. If you are trying to display the video feed, you need to use

 navigator.mediaDevices.getUserMedia

Then convert the stream into a MediaStream and add MediaStreamTrack to the MediaStream then link the tracks/stream to the video element.

Check out https://developer.mozilla.org/en-US/docs/Web/API/MediaStream

答案2

得分: 0

我终于成功解决了这个问题。检查一下 platforms/android/app/src/main/AndroidManifest.xml 文件,添加以下内容可能是个好主意:

<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />

但我不确定是否必要。感谢 @Eric 的回答,我能够朝特定方向搜索到有效的代码。

我在 index.html 文件中完成所有操作:

let constraintObj = {
      audio: false,
      video: {
        facingMode: "user",
        width: { min: 640, ideal: 1280, max: 1920 },
        height: { min: 480, ideal: 720, max: 1080 },
      },
    };

    navigator.mediaDevices.getUserMedia(constraintObj)
      .then(function (mediaStreamObj) {
        let video = document.getElementById('video');
        video.srcObject = mediaStreamObj;

        video.onloadedmetadata = function (ev) {
          video.play();
        };
      })
      .catch(function (error) {
        console.error('Camera error: ' + error);
      });
英文:

I finally managed to solve this. It might be smart to check the platforms/android/app/src/main/AndroidManifest.xml file and add

&lt;uses-permission android:name=&quot;android.permission.CAMERA&quot; /&gt;
&lt;uses-feature android:name=&quot;android.hardware.camera&quot; /&gt;
&lt;uses-feature android:name=&quot;android.hardware.camera.autofocus&quot; /&gt;

But I am not sure if it's necessary. Code that worked, thanks to @Eric's answer, I was able to search in a specific direction.

I do everything in the index.html file:

let constraintObj = {
      audio: false,
      video: {
        facingMode: &quot;user&quot;,
        width: { min: 640, ideal: 1280, max: 1920 },
        height: { min: 480, ideal: 720, max: 1080 },
      },
    };

    navigator.mediaDevices.getUserMedia(constraintObj)
      .then(function (mediaStreamObj) {
        let video = document.getElementById(&#39;video&#39;);
        video.srcObject = mediaStreamObj;

        video.onloadedmetadata = function (ev) {
          video.play();
        };
      })
      .catch(function (error) {
        console.error(&#39;Camera error: &#39; + error);
      });

huangapple
  • 本文由 发表于 2023年4月17日 21:15:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/76035579.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定