通过CodeName One移动应用通过HTML5 <video>元素访问Android设备的相机

时间:2018-09-03 18:02:29

标签: android camera codenameone

我们正在使用CodeName One构建适用于Android的移动应用。作为要求的一部分,我们已经有一个HTML5页面,其中包含一个视频元素,可使用设备的相机捕获照片。我们需要从CodeName One应用程序中调用此HTML5页面。但是此代码无法按预期工作,但是当直接从chrome浏览器中调用时,html文件URL可以正常工作。从chrome浏览器查看时,出现提示询问相机许可的提示,如CodeName One应用中的一样,我看不到许可提示。

我最近看到了类似的问题 html video control is not working in codenameone app for android

我已经尝试过那里给出的建议,但是对我没有用。也许我在这里做错了什么?

在我的CodeName一类中,我有以下代码。该代码基本上创建了一个新的BrowserComponent并在该组件中设置了camera1.html URL。 camera1.html包含HTML5代码,该代码显示用于拍照的视频元素。 `

private Form current;
private Resources theme;

public void init(Object context) {
    theme = UIManager.initFirstTheme("/theme");

    // Enable Toolbar on all Forms by default
    Toolbar.setGlobalToolbar(true);    
}

public void start(){
    if(current != null) {
        current.show();
        return;
    }

    Form f = new Form("Camera", new BorderLayout());
    try {
        CN1NativeCameraInterface cnci = NativeLookup.create(CN1NativeCameraInterface.class);
        if (cnci != null && cnci.isSupported()) {

            boolean cameraPerm = cnci.promptCameraPermission();
            if (cameraPerm) {
               f.setTitle("Camera Permission true");
            }              
        }

        BrowserComponent browser = new BrowserComponent();

        browser.setURL("https://8c10ec77.ngrok.io/app/camera1.html");
        f.add(BorderLayout.CENTER, browser);
    }
    catch (Exception ex) {
        ex.printStackTrace();
    }
    finally {
        f.show();
    }
}

public void stop(){
    current = Display.getInstance().getCurrent();
}

public void destroy(){
}

` 我定义了一个看起来像这样的本机接口

public interface CN1NativeCameraInterface extends NativeInterface {
    public boolean promptCameraPermission();
}

在CodeName One中为此接口生成了本机访问代码,并且Android的实现如下

import com.codename1.impl.android.AndroidImplementation;
import android.Manifest;

public class CN1NativeCameraInterfaceImpl {
    public boolean promptCameraPermission() {
        return AndroidImplementation.checkForPermission(Manifest.permission.CAMERA, "This is required to take a picture");
    }

    public boolean isSupported() {
        return true;
    }

}

当我在Android设备上运行此代码时,可以看到HTML页面正在加载,但是video元素没有加载,我所看到的只是video元素的占位符。 该应用程序也不要求相机许可,这是其他相关问题中提出的建议。

我在这里做错了什么?

更新1:

我在使用以下命令从设备获取的日志中看到以下错误 亚行外壳程序“ logcat --pid = $(pidof -s com.codename1)”命令:

错误1-

09-06 11:48:42.530 10172 10202 I WebViewFactory:正在加载com.android.chrome版本69.0.3497.76(代码349707652) 09-06 11:48:42.595 10172 10202我zygote64:拒绝对先前失败的类java.lang.Class:java.lang.NoClassDefFoundError重新初始化:错误的解析度:Landroid / webkit / TracingController; 09-06 11:48:42.595 10172 10202我zygote64:at java.lang.Class java.lang.Class.classForName(java.lang.String,boolean,java.lang.ClassLoader)(Class.java:-2) 09-06 11:48:42.595 10172 10202我zygote64:at java.lang.Class java.lang.Class.forName(java.lang.String,boolean,java.lang.ClassLoader)(Class.java:453) 09-06 11:48:42.595 10172 10202我zygote64:在java.lang.Class android.webkit.WebViewFactory.getWebViewProviderClass(java.lang.ClassLoader)(WebViewFactory.java:151) 09-06 11:48:42.595 10172 10202我zygote64:在java.lang.Class android.webkit.WebViewFactory.getProviderClass()(WebViewFactory.java:472) 09-06 11:48:42.595 10172 10202我zygote64:在android.webkit.WebViewFactoryProvider android.webkit.WebViewFactory.getProvider()(WebViewFactory.java:212) 09-06 11:48:42.595 10172 10202我zygote64:在android.webkit.CookieManager上android.webkit.CookieManager.getInstance()(CookieManager.java:39) 09-06 11:48:42.595 10172 10202我zygote64:在android.webkit.CookieManager com.codename1.impl.android.AndroidImplementation.getCookieManager()(AndroidImplementation.java:4511) 09-06 11:48:42.595 10172 10202我zygote64:在java.util.Vector com.codename1.impl.android.AndroidImplementation.getCookiesForURL(java.lang.String)(AndroidImplementation.java:4528) 09-06 11:48:42.595 10172 10202我zygote64:在无效com.codename1.io.ConnectionRequest.performOperation()(ConnectionRequest.java:633) 09-06 11:48:42.595 10172 10202我zygote64:在无效com.codename1.io.NetworkManager $ NetworkThread.run()(NetworkManager.java:282) 09-06 11:48:42.595 10172 10202我zygote64:at com.codename1.impl.CodenameOneThread $ 1.run()(CodenameOneThread.java:60) 09-06 11:48:42.595 10172 10202我zygote64:在void java.lang.Thread.run()(Thread.java:764) 09-06 11:48:42.595 10172 10202我zygote64:原因:java.lang.ClassNotFoundException:在路径:DexPathList [[zip file“ /data/app/com.com]中找不到类” android.webkit.TracingController“。 android.chrome-fq476TNc_btk-Vb0sBbuyw == / base.apk“],nativeLibraryDirectories = [/ data / app / com.android.chrome-fq476TNc_btk-Vb0sBbuyw == / lib / arm64,/data/app/com.android.chrome -fq476TNc_btk-Vb0sBbuyw == / base.apk!/ lib / arm64-v8a,/ system / lib64,/ system / vendor / lib64]]] 09-06 11:48:42.595 10172 10202我zygote64:在java.lang.Class dalvik.system.BaseDexClassLoader.findClass(java.lang.String)(BaseDexClassLoader.java:93) 09-06 11:48:42.595 10172 10202我zygote64:at java.lang.Class java.lang.ClassLoader.loadClass(java.lang.String,boolean)(ClassLoader.java:379) 09-06 11:48:42.595 10172 10202我zygote64:at java.lang.Class java.lang.ClassLoader.loadClass(java.lang.String)(ClassLoader.java:312) 09-06 11:48:42.595 10172 10202我zygote64:at java.lang.Class java.lang.Class.classForName(java.lang.String,boolean,java.lang.ClassLoader)(Class.java:-2) 09-06 11:48:42.595 10172 10202我zygote64:在java.lang.Class java.lang.Class.forName(java.lang.String,boolean,java.lang.ClassLoader)(Class.java:453) 09-06 11:48:42.595 10172 10202我zygote64:在java.lang.Class android.webkit.WebViewFactory.getWebViewProviderClass(java.lang.ClassLoader)(WebViewFactory.java:151) 09-06 11:48:42.595 10172 10202我zygote64:在java.lang.Class android.webkit.WebViewFactory.getProviderClass()(WebViewFactory.java:472) 09-06 11:48:42.595 10172 10202我zygote64:在android.webkit.WebViewFactoryProvider android.webkit.WebViewFactory.getProvider()(WebViewFactory.java:212) 09-06 11:48:42.595 10172 10202我zygote64:在android.webkit.CookieManager上android.webkit.CookieManager.getInstance()(CookieManager.java:39) 09-06 11:48:42.595 10172 10202我zygote64:在android.webkit.CookieManager com.codename1.impl.android.AndroidImplementation.getCookieManager()(AndroidImplementation.java:4511) 09-06 11:48:42.595 10172 10202我zygote64:在java.util.Vector com.codename1.impl.android.AndroidImplementation.getCookiesForURL(java.lang.String)(AndroidImplementation.java:4528) 09-06 11:48:42.595 10172 10202我zygote64:at com.codename1.io.ConnectionRequest.performOperation()(ConnectionRequest.java:633)

错误2-

09-06 11:48:44.506 10172 10266 W VideoCapabilities:视频/ AVC无法识别的配置文件2130706433 09-06 11:48:44.506 10172 10266 W VideoCapabilities:无法识别的配置文件2130706434用于视频/ AVC 09-06 11:48:44.536 10172 10266 W VideoCapabilities:无法识别的配置文件2130706433用于视频/ AVC 09-06 11:48:44.536 10172 10266 W VideoCapabilities:无法识别的配置文件2130706434用于视频/ AVC 09-06 11:48:44.542 10172 10266 W VideoCapabilities:无法识别的配置文件2130706433用于视频/ AVC 09-06 11:48:44.542 10172 10266 W VideoCapabilities:无法识别的配置文件2130706434用于视频/ AVC 09-06 11:48:44.551 10172 10266 W VideoCapabilities:无法识别的配置文件/视频/ mpeg2的0/3级 09-06 11:48:44.555 10172 10266 W VideoCapabilities:无法识别的配置文件/视频/ mpeg2的0/3级 09-06 11:48:44.564 10172 10266 W VideoCapabilities:不支持的mime video / x-ms-wmv 09-06 11:48:44.568 10172 10266 W VideoCapabilities:不支持的mime video / x-ms-wmv 09-06 11:48:44.574 10172 10266 W VideoCapabilities:不支持的mime video / divx 09-06 11:48:44.577 10172 10177 I zygote64:执行部分代码缓存收集,代码= 55KB,数据= 56KB 09-06 11:48:44.580 10172 10266 W VideoCapabilities:不支持的mime video / divx311 09-06 11:48:44.584 10172 10266 W VideoCapabilities:不支持的mime video / divx4 09-06 11:48:44.585 10172 10177 I zygote64:代码缓存收集后,代码= 55KB,数据= 56KB 09-06 11:48:44.585 10172 10177 I zygote64:将代码缓存容量增加到256KB 09-06 11:48:44.605 10172 10266 W VideoCapabilities:不支持的mime video / mp4v-esdp 09-06 11:48:44.633 10172 10266 I VideoCapabilities:video / mp4v-es不受支持的配置文件4 09-06 11:48:44.867 10172 10282 W cr_CrashFileManager:/data/user/0/com.codename1/cache/WebView/Crash报告不存在或不是目录

错误3-

09-06 11:48:47.715 10172 10253 I CameraManagerGlobal:连接到摄像头服务 09-06 11:48:47.799 10172 10172 D代号一:onPermissionRequest 09-06 11:48:50.952 10172 10172 E AndroidImplementation:null 09-06 11:48:50.952 10172 10172 E AndroidImplementation:java.io.FileNotFoundException:/data/user/0/com.codename1/files/CN1$AndroidServiceProperties(没有这样的文件或目录) 09-06 11:48:50.952 10172 10172 E AndroidImplementation:at java.io.FileInputStream.open0(本机方法) 09-06 11:48:50.952 10172 10172 E AndroidImplementation:在java.io.FileInputStream.open(FileInputStream.java:200) 09-06 11:48:50.952 10172 10172 E AndroidImplementation:at java.io.FileInputStream。(FileInputStream.java:150) 09-06 11:48:50.952 10172 10172 E AndroidImplementation:在android.app.ContextImpl.openFileInput(ContextImpl.java:515) 09-06 11:48:50.952 10172 10172 E AndroidImplementation:在android.content.ContextWrapper.openFileInput(ContextWrapper.java:190) 09-06 11:48:50.952 10172 10172 E AndroidImplementation:位于com.codename1.impl.android.AndroidImplementation.getServiceProperties(AndroidImplementation.java:6081) 09-06 11:48:50.952 10172 10172 E AndroidImplementation:位于com.codename1.impl.android.AndroidImplementation.writeServiceProperties(AndroidImplementation.java:6107) 09-06 11:48:50.952 10172 10172 E AndroidImplementation:at com.codename1.CameraDemoStub.onStop(CameraDemoStub.java:166) 09-06 11:48:50.952 10172 10172 E AndroidImplementation:位于android.app.Instrumentation.callActivityOnStop(Instrumentation.java:1486) 09-06 11:48:50.952 10172 10172 E AndroidImplementation:在android.app.Activity.performStop(Activity.java:7178) 09-06 11:48:50.952 10172 10172 E AndroidImplementation:在android.app.ActivityThread.performStopActivityInner(ActivityThread.java:4274) 09-06 11:48:50.952 10172 10172 E AndroidImplementation:在android.app.ActivityThread.handleStopActivity(ActivityThread.java:4333) 09-06 11:48:50.952 10172 10172 E AndroidImplementation:at android.app.ActivityThread.-wrap24(Unknown Source:0) 09-06 11:48:50.952 10172 10172 E AndroidImplementation:在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1722) 09-06 11:48:50.952 10172 10172 E AndroidImplementation:在android.os.Handler.dispatchMessage(Handler.java:105) 09-06 11:48:50.952 10172 10172 E AndroidImplementation:at android.os.Looper.loop(Looper.java:164) 09-06 11:48:50.952 10172 10172 E AndroidImplementation:在android.app.ActivityThread.main(ActivityThread.java:6798) 09-06 11:48:50.952 10172 10172 E AndroidImplementation:at java.lang.reflect.Method.invoke(本机方法) 09-06 11:48:50.952 10172 10172 E AndroidImplementation:at com.android.internal.os.Zygote $ MethodAndArgsCaller.run(Zygote.java:240) 09-06 11:48:50.952 10172 10172 E AndroidImplementation:在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)

错误4-

09-06 11:48:51.005 10172 10172 D CameraDemo:[main] 0:0:0,3-[LOG]发生以下错误:NotAllowedError:https://8319bf05.ngrok.io/ventasys/venta/code/camera1.html第55行上的权限被拒绝

此上面的错误(错误4)来自从CodeName One调用的.html。 这是HTML的代码:

<!DOCTYPE HTML>
<html>
  <head>
    <script>
        function copyPhoto() {
            var imgName;
            imgName = getParamValues("imgNm");        
            window.opener.document.getElementById(imgName).src = document.getElementById("theimage1").src;
            window.close();
        }

        function getParamValues(paramNm) {
            var qstring = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
            for (var i = 0; i < qstring.length; i++) {
                var urlparam = qstring[i].split('='); 
                if (urlparam[0] == paramNm) {                        
                    return urlparam[1];
                }
            }
        }

        function snapshot() {

            var canvas = document.getElementById("thecanvas");

            var ctx = canvas.getContext("2d");

            //var constraints = { video: { facingMode: "user" }, audio: false };
            //var constraints = {
            //    video: {
            //        width: { min: 640, ideal: 1920 },
            //        height: { min: 400, ideal: 1080 },
            //        aspectRatio: { ideal: 1.7777777778 },
            //        facingMode: "environment"
            //    },
            //    audio: false
            //};

            var constraints = {
                video: { facingMode: "user" },
                audio: false
            };

            try {

                navigator.mediaDevices.getUserMedia(constraints).then(                        
                    // successCallback
                    function (localMediaStream) {
                        document.getElementById("mylog").innerHTML = document.getElementById("mylog").innerHTML + '1.3 ';
                        video = document.querySelector('video');
                        video.srcObject = localMediaStream;
                    })
                    .catch(function (err) {
                        console.log("The following error occured: " + err);
                        document.getElementById("mylog").innerHTML = document.getElementById("mylog").innerHTML + ' <br> 5 Err' + err;
                    });

            }
            catch (e)
            {
                document.getElementById("mylog").innerHTML = document.getElementById("mylog").innerHTML + ' 6 catch ##** ' + e;
            }
            document.getElementById("mylog").innerHTML = document.getElementById("mylog").innerHTML + '10 ';
            canvas.width = 640;
            canvas.height = video.videoHeight / (video.videoWidth / canvas.width);

            // Draws current image from the video element into the canvas               
            //ctx.drawImage(video, 0,0, video.videoWidth, video.videoHeight);
            ctx.drawImage(video, 0, 0, canvas.width, canvas.height);

            var canvas = document.getElementById("thecanvas");
            var dataUrl = canvas.toDataURL('image/jpeg', 0.6);
        }
    </script>
</head>
<body onload="snapshot();">
    <div id='mylog'></div>
    <p><video id="video" controls autoplay></video></p>
    <p><button onclick="snapshot();">Take Snapshot</button></p>
    <div id='item' hidden>
        <p><canvas id="thecanvas"></canvas></p>
    </div>        
    <p><image id="theimage1"></image></p>
    <p><button onclick="copyPhoto()">Done</button></p>
    <p><button onclick="copyPhoto()">Reload</button></p>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

尝试将其添加到您的init(Object)方法中:

Display.getInstance().setProperty("android.WebView.grantPermissionsFrom", url_of_your_video_page);

您可以添加多个以空格分隔的URL。如果这不起作用,我们可能还需要其他东西。