HTML5 實現掃描識別二維碼 生成二維碼
阿新 • • 發佈:2019-01-31
掃描識別二維碼
思路 1. 操作攝像頭,獲取圖片。HTML5 WEBRTC的navigator.getUserMedia方法去實時獲取攝像頭資源。 2. 利用canvas使用相關演算法分析圖片識別圖片得出結果,可用jquery.qrcode分析二維碼 。(技術點:getImageData)侷限性 移動平臺支援getUserMedia/Stream API的瀏覽器比較少,支援列表http://caniuse.com/#feat=stream (目前沒發現有IOS是支援的) IOS替代方案筆記: 呼叫攝像頭拍照,把圖片傳到h5頁面。然後在解析。12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 | <!DOCTYPE HTML> < html > < head > < title >上傳圖片</ title > < meta charset = "utf-8" > </ head > < body > < iframe name = "uploadfrm" id = "uploadfrm" style = "display: none;" ></ iframe > < form name = "formHead" method = "post" action = "" id = "formHead" enctype = "multipart/form-data" target = "uploadfrm" > < div > < div > < input type = "file" name = "file_head" id = "file_head" onchange = "javascript:setImagePreview();" /> </ div > < div > < div id = "DivUp" style = "display: none" > < input type = "submit" data-inline = "true" id = "BtnUp" value = "確認上傳" data-mini = "true" /> </ div > </ div > </ div > </ form > < div data-role = "fieldcontain" > < div id = "localImag" > < img id = "preview" width = "-1" height = "-1" style = "display: none" /> </ div > </ div > < script type = "text/javascript" > function setImagePreview() { var preview, img_txt, localImag, file_head = document.getElementById("file_head"), picture = file_head.value; if (!picture.match(/.jpg|.gif|.png|.bmp/i)) return alert("您上傳的圖片格式不正確,請重新選擇!"), !1; if (preview = document.getElementById("preview"), file_head.files && file_head.files[0]) preview.style.display = "block", preview.style.width = "63px", preview.style.height = "63px", preview.src = window.navigator.userAgent.indexOf("Chrome") >= 1 || window.navigator.userAgent.indexOf("Safari") >= 1 ? window.webkitURL.createObjectURL(file_head.files[0]) : window.URL.createObjectURL(file_head.files[0]); else { file_head.select(), file_head.blur(), img_txt = document.selection.createRange().text, localImag = document.getElementById("localImag"), localImag.style.width = "63px", localImag.style.height = "63px"; try { localImag.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale)", localImag.filters.item("DXImageTransform.Microsoft.AlphaImageLoader").src = img_txt } catch(f) { return alert("您上傳的圖片格式不正確,請重新選擇!"), !1 } preview.style.display = "none", document.selection.empty() } return document.getElementById("DivUp").style.display = "block", !0 } </ script > </ body > </ html > |
程式碼實現 感謝github,有現成的演算法 具體實現程式碼: