云钦
云钦
  • 发布:2017-06-28 09:03
  • 更新:2017-06-29 08:41
  • 阅读:1584

红米NOTE4.4.4打开闪光灯异常

分类:Native.js

不是完全不能用,单纯的打开闪光灯可以,
如果在扫描二维码的同时再打开闪光灯,
就提示“初始化错误”,返回,
再进入此页面,直接提示“未获取摄像头权限”

代码参考这里的:http://ask.dcloud.net.cn/question/19379
IOS的能用,安卓的不行。

第一次打开如下图:

开启手电筒后,如下图:

返回,再次进入,如下图:

具体代码在这里:

            var ws = null,  
                wo = null;  
            var scan = null,  
                domready = false;  
            var isOn=true;  
            // H5 plus事件处理  
            function plusReady() {  
                if (ws || !window.plus || !domready) {  
                    return;  
                }  
                // 获取窗口对象  
                ws = plus.webview.currentWebview();  
//              wo = ws.opener();  
                // 开始扫描  
                setTimeout(function() {  
                    scan = new plus.barcode.Barcode('bcid');  
                    scan.onmarked = onmarked;  
                    scan.start({  
                        conserve: false  
                    }); //不需要保存扫描到的图片,若需要保存设置为true  
                }, 300);  
                // 显示页面并关闭等待框  
                ws.show("slide-in-right", 300);  
                plus.nativeUI.closeWaiting();  
//              wo.evalJS("closeWaiting()");  
                var opensdt=document.getElementById("opensdt");  
                opensdt.addEventListener("tap",function(){  
                    turnonLight();  
                });  
            }  

            if (window.plus) {  
                plusReady();  
            } else {  
                document.addEventListener("plusready", plusReady, false);  
            }  
            // 监听DOMContentLoaded事件  
            document.addEventListener("DOMContentLoaded", function() {  
                domready = true;  
                plusReady();  
            }, false);  
            // 二维码扫描成功  
            function onmarked(type, result, file) {  
                switch (type) {  
                    case plus.barcode.QR:  
                        type = "QR";  
                        break;  
                    case plus.barcode.EAN13:  
                        type = "EAN13";  
                        break;  
                    case plus.barcode.EAN8:  
                        type = "EAN8";  
                        break;  
                    default:  
                        type = "其它";  
                        break;  
                }  
                result = result.replace(/\n/g, '');  
                wo.evalJS("scaned('" + type + "','" + result + "','" + file + "');");  
                isOn=false;  
                turnonLight();  
                back();  
            }  
            // 从相册中选择二维码图片   
            function scanPicture() {  
                plus.gallery.pick(function(path) {  
                    plus.barcode.scan(path, onmarked, function(error) {  
                        plus.nativeUI.alert("无法识别此图片");  
                    });  
                }, function(err) {  
                    plus.nativeUI.alert("Failed: " + error.message);  
                });  
            }  

            function turnonLight() {  
                if(isOn){  
                    $(".iconfont").removeClass("icon-shoudiantong");  
                    $(".iconfont").addClass("icon-shoudiantongdianliang");  
                }  
                else{  
                    $(".iconfont").removeClass("icon-shoudiantongdianliang");  
                    $(".iconfont").addClass("icon-shoudiantong");  
                }  
                console.log("os_name:"+plus.os.name);  
                if(plus.os.name == "iOS") {  
                    var avcaptClass = plus.ios.importClass("AVCaptureDevice");  
                    if(avcaptClass) {  
                        var device = avcaptClass.defaultDeviceWithMediaType("vide");  
                        plus.ios.invoke(device, "lockForConfiguration:", null);  
                        if(isOn) {  
                            plus.ios.invoke(device, "setTorchMode:", 1);  
                            plus.ios.invoke(device, "setFlashMode:", 1);  
                            isOn=false;  
                        } else {  
                            plus.ios.invoke(device, "setTorchMode:", 0);  
                            plus.ios.invoke(device, "setFlashMode:", 0);  
                            isOn=true;  
                        }  
                        plus.ios.invoke(device, "unlockForConfiguration");  
                    }  
                }  
                else{  
//                  if(FlashControl)FlashControl.open();  
                    if(isOn){  
                        if(FlashControl){  
                            FlashControl.open();  
                            isOn=false;  
                        }  
                    }  
                    else{  
                        if(FlashControl){  
                            isOn=true;  
                            FlashControl.close();  
                            FlashControl.release();  
                        }  
                    }  
                }  

            }  
            function back(){  
                if(plus.os.name == "iOS") {  

                }  
                else{  
                    if(FlashControl){  
                        FlashControl.close();  
                        FlashControl.release();  
                    }  
                }  
                ws.close();  
//              mui.back();  
            }
2017-06-28 09:03 负责人:无 分享
已邀请:
云钦

云钦 (作者) - 学习中

OPPO Y33 5.0.0能打开闪光灯,返回后再次进入,软件直接崩溃退出。

Neil_HL

Neil_HL

参考示例 http://ask.dcloud.net.cn/article/12598,提示“未获取摄像头权限”:重新进入扫码页面得重新调用barcode,至于应用崩溃,你新开扫码页面的webview是否有动画效果,有动画的话会偶发崩溃。

云钦

云钦 (作者) - 学习中

页面重新加载了,barcode方法肯定是重新调用了哦,
用了几年H5了,才发现,官方已经封装了打开闪光灯的功能啊,太感谢你们了。

云钦

云钦 (作者) - 学习中

经过反复测试,红米note打开扫码页面,返回,再打开,总是提示“未获取摄像头权限”,
苹果手机就不存在此问题。
应该不是重新调用的问题。

云钦

云钦 (作者) - 学习中

是不是skd针对安卓部分版本的系统,没有释放摄像头的独占权。

Neil_HL

Neil_HL

其他安卓机是否有此问题?我发的那个链接里面的demo你试试,会不会出现类似的问题

云钦

云钦 (作者) - 学习中

经过测试demo,发现红米note依然存在这个问题,
并且,我的代码,在oppo,iphone上都是可以正常使用的。

该问题目前已经被锁定, 无法添加新回复