使用JS調用手機本地攝像頭或者相冊圖片識別二維碼/條形碼


接着昨天的需求,不過這次不依賴微信,使用純js喚醒手機本地攝像頭或者選擇手機相冊圖片,識別其中的二維碼或者是條形碼.昨天,我使用微信掃一掃識別,效果超棒的.不過如果依賴微信的話,又怎么實現呢,這里介紹兩個js庫,都很牛掰,感謝大神,獻上鏈接.

倉庫:https://github.com/serratus/quaggaJS

官網: https://serratus.github.io/quaggaJS/

識別二維碼

條形碼解析:

我也是在網上找了一大堆,都失敗了,別人博主寫的都沒頭沒尾的,無法實現其功能,既然有工具就一梭子的事情了,花費了一晚上,終於弄出來了,下面就是設置參數的問題了

甩出一個倉庫,大家下載這個倉庫就可以實現此功能的,不過條形碼識別的准確性有點問題.需要設置對應的參數,其中需要修改代碼

........

//          , 'code_39_reader','code_128_reader',"ean_reader", 
             decoder: {
                 readers: [{
                     format: ['ean_reader'], //條形碼種類很多,選擇適合自己的
                     config: {}
                 }]
             },

   ........

 

 

二維碼解析:

這個沒找到開源倉庫,不過找到一個可以實現的DMEO,大家喜歡的話可以自己深究,我要去擼代碼了.

我的倉庫實例地址: https://gitee.com/guyandog/QR_BR 傳送門

收集不易,點贊鼓勵,謝謝

 

PS: 我這里是識別靜態圖片,如果是需要手機打開攝像頭的,將input中添加這樣的屬性,如下:

 

<!DOCTYPE html>
< html>
< head>
     <meta charset="UTF-8"/>
     <title>index</title>
     <meta name="viewport"
           content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no"/>
     <meta name="apple-mobile-web-app-capable" content="yes"/>
     <meta name="apple-mobile-web-app-status-bar-style" content="black"/>
     <meta name="mobile-web-app-capable" content="yes"/>
     <meta name="format-detection" content="telephone=no">
     <meta http-equiv="pragma" content="no-cache">
     <meta http-equiv="Cache-Control" content="no-store, must-revalidate">
< /head>
< body>
< style>
     input{
         display: block;
         width: 100px;
         height: 60px;
         background: red;
         color: white;
         font-size: 16px;
         line-height: 60px;
         outline: none;
         border: 0;
     }
< /style>

<input id="pictureB_file" accept="image/*" type="file" capture="camera" name="pictureA_file"/>
< input id="pictureC_file" type="file" name="pictureA_file"/>
< script>
     var u = navigator.userAgent, app = navigator.appVersion;
     var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Linux') > -1; //android終端或者uc瀏覽器
     var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios終端
     //alert('是否是Android:'+isAndroid);
     //alert('是否是iOS:'+isiOS);
     if (isAndroid) {
         //如果是安卓手機,就彈框是選擇圖片還是拍照
         $(".tankuang").css("display", "block");
     } else {
         //如果是蘋果系統,就還照之前的樣子去操作即可
         //$($("#pictureA_file")[0]).click();
     }
< /script>
< /body>
< /html>

參考網站:(感謝這些博客的博主)

https://www.zhangshengrong.com/p/2EaE0WdO1M/

http://www.cnblogs.com/yaotome/p/9450274.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM