Cordova Android端前置摄像头视频流展示简易App无法运行
问题描述
我在多个网站查找资料,始终没找到可行的解决方案。尝试开发一款简易Android App,打开后能展示前置或后置摄像头的视频画面,但我在Cordova及Android App开发方面经验不足。
复现步骤
cordova create my-camera-app com.example.mycameraapp MyCameraApp cd my-camera-app cordova platform add android cordova plugin add cordova-plugin-camera
代码实现
修改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() { // 获取视频元素 var videoElement = document.getElementById('videoElement'); // 将视频源设置为设备前置摄像头 navigator.camera.getPicture( function successCallback(imageData) { videoElement.src = "data:image/jpeg;base64," + imageData; videoElement.play(); }, function errorCallback(error) { alert('无法获取摄像头: ' + 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" />
运行命令
cordova run android
当前问题
App仅显示播放器图标,无任何反应也无法操作。已在联想平板和三星手机两款Android设备上测试,了解到设备兼容性可能是问题原因之一。
完整
config.xml内容 <?xml version='1.0' encoding='utf-8'?> <widget id="com.example.mycameraapp" version="1.0.0" xmlns="http://www.w3.org/n> <name>My Camera App</name> <description>A simple app that displays the front camera video stream</descri> <author email="youremail@example.com" href="http://example.com/">Your Name</a> <content src="index.html" /> <access origin="*" /> <platform name="android"> <preference name="android-minSdkVersion" value="21" /> <preference name="android-targetSdkVersion" value="31" /> <preference name="android-build-tool" value="29.0.2" /> <preference name="android-enableHybridWebView" value="true" /> <preference name="android-useLegacyWhitelist" value="true" /> <allow-intent href="market:*" /> <uses-permission android:name="android.permission.CAMERA" /> <uses-feature android:name="android.hardware.camera" /> <uses-feature android:name="android.hardware.camera.front" /> </platform> </widget>
内容的提问来源于stack exchange,提问作者M.K
相关产品推荐
相关产品推荐

