javascript实现QR code二维码扫描

本例基于instascan : https://github.com/schmich/instascan


1. html

<video id="preview" width="500" height="500"></video>
<input type="text" class="form-control" id="txtScannedQR" style="display: none" placeholder="Scan Result" name="AssetTag"/>


2. javascript 

<script src="~/Scripts/instascan.min.js"></script>
 
 
    <script type="text/javascript">
        function checkQR() {
            return $("#txtScannedQR").val() != "";
        }
 
 
        var scanner = new Instascan.Scanner(
            {
                video: document.getElementById('preview') 
            });
        scanner.addListener('scan', function (content) {
            $("#txtScannedQR").val(content);
            alert(content);
            
        });
        Instascan.Camera.getCameras().then(function (cameras) {
            
            // on mobile pick the rear camera
            if (cameras.length > 1) {
                scanner.start(cameras[1]);
            }
            else if (cameras.length > 0) {
                scanner.start(cameras[0]);
            } else {
                console.error('No cameras found.');
            }
        }).catch(function (e) {
            console.error(e);
        });
    </script>
}
 

3. 如果运行遇到错误:
getusermedia() no longer works on insecure origins. to use this feature, you should consider switching your application to a secure origin, such as https


是因为这个功能需要https,要在web服务器安装ssl证书并打开443端口就可以了。
---------------------
作者:_iorilan
来源:CSDN
原文:https://blog.csdn.net/lan_liang/article/details/78276153
版权声明:本文为博主原创文章,转载请附上博文链接!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值