简体   繁体   English

如何使用相机在js中构建条码阅读器?

[英]How to build a barcode reader in js with camera?

Im a beginer in js and jquery that's why i need your help .我是 js 和 jquery 的初学者,这就是为什么我需要你的帮助。 I would like to build a barcode reader in js thanks to the camera on my phone .由于我手机上的摄像头,我想用 js 构建一个条形码阅读器。 For the moment i do this :目前我这样做:

 <video id="video" width="640" height="480" autoplay></video> <button id="snap">Snap Photo</button> <canvas id="canvas" width="640" height="480"></canvas> <script type="text/javascript"> // Grab elements, create settings, etc. var video = document.getElementById('video'); // Get access to the camera! if(navigator.mediaDevices && navigator.mediaDevices.getUserMedia) { // Not adding `{ audio: true }` since we only want video now navigator.mediaDevices.getUserMedia({ video: true }).then(function(stream) { video.src = window.URL.createObjectURL(stream); video.play(); }); // Elements for taking the snapshot var canvas = document.getElementById('canvas'); var context = canvas.getContext('2d'); var video = document.getElementById('video'); // Trigger photo take document.getElementById("scan").addEventListener("click", function() { context.drawImage(video, 0, 0, 640, 480); }); }

but it doesn't work so im pretty lost x) thank you in advance to your help !但它不起作用,所以我很迷茫 x) 提前感谢您的帮助! (sorry for my english :D) (对不起我的英语:D)

Why not using some libs, eg.为什么不使用一些库,例如。

You could make use of the zbar C/C++ library and transpile it to WebAssembly (which is supported by all major browsers) using Emscripten .您可以使用zbar C/C++ 库并使用Emscripten 将其转换为WebAssembly (所有主要浏览器都支持)。

Emscripten is an LLVM/Clang-based compiler that compiles C and C++ source code to WebAssembly, primarily for execution in web browsers. Emscripten 是一个基于 LLVM/Clang 的编译器,可将 C 和 C++ 源代码编译为 WebAssembly,主要用于在 Web 浏览器中执行。

Here's an example app I have just published:这是我刚刚发布的示例应用程序:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM