Giter VIP home page Giter VIP logo

zxing-cpp-emscripten's Introduction

ZXing in Javascript via Emscripten

NOTE: This repository contains Kevin Bombino's tweaks to get this to work.

Most of the credit goes to the upstream authors.

I have updated this README to make clear how to build.

The repository is based on the ones from the upstream author.

Original README follows


Emscripten Installation

  1. Download emsdk-portable-64bit.zip

  2. Fetch the latest registry of available tools:

    emsdk update
    
  3. Download and install the latest SDK tools:

    emsdk install latest
    
  4. Make the "latest" SDK "active" for the current user:

    emsdk activate latest
    
  5. Activate PATH and other environment variables in the current terminal:

    emsdk_env
    

JavaScript ZXing

To build:

  1. cd build-js
  2. Run configure.bat
  3. Run build.bat
  4. Add the path of build-js folder to IIS.
  5. Open http://localhost:2588/test.html.

To use:

    <script>
		var tick = function () {
			if (window.ZXing) {
				ZXing = ZXing();
				testZXing();
			} else {
				setTimeout(tick, 10);
			}
		};
		tick();

		function testZXing() {
			var img = new Image;
			img.src = 'Qr-10.png';
			img.onload = function () {

				var width = Math.floor(this.width),
					height = Math.floor(this.height);

				var canvas = document.createElement('canvas');
				canvas.style.display = 'block';
				canvas.width = width;
				canvas.height = height;
				var ctx = canvas.getContext('2d');
				// ctx.rotate(Math.random()*0.1-0.05);
				ctx.drawImage(this, 0, 0, width, height);
				var imageData = ctx.getImageData(0, 0, width, height);
				var idd = imageData.data;
				document.body.appendChild(canvas);

				var decodeCallback = function (ptr, len, resultIndex, resultCount) {
					var result = new Uint8Array(ZXing.HEAPU8.buffer, ptr, len);
					window.resultString = String.fromCharCode.apply(null, result);
				};
				var decodePtr = ZXing.Runtime.addFunction(decodeCallback);

				var image = ZXing._resize(width, height);

				for (var i = 0, j = 0; i < idd.length; i += 4, j++) {
					ZXing.HEAPU8[image + j] = idd[i];
				}

				var err = ZXing._decode_qr(decodePtr);

				console.log("error code", err);
				console.log("result", window.resultString);

				document.body.appendChild(document.createTextNode(err ? ("error: " + err) : window.resultString));

				for (var k = 0; k < 50; k++) {
					for (var i = 0, j = 0; i < idd.length; i += 4, j++) {
						ZXing.HEAPU8[image + j] = idd[i];
					}
					err = ZXing._decode_qr_multi(decodePtr);
					err = ZXing._decode_qr(decodePtr);
				}

				console.time("decode QR");
				for (var i = 0, j = 0; i < idd.length; i += 4, j++) {
					ZXing.HEAPU8[image + j] = idd[i];
				}
				err = ZXing._decode_qr(decodePtr);
				console.timeEnd("decode QR");

				console.time("decode QR multi");
				for (var i = 0, j = 0; i < idd.length; i += 4, j++) {
					ZXing.HEAPU8[image + j] = idd[i];
				}
				err = ZXing._decode_qr_multi(decodePtr);
				console.timeEnd("decode QR multi");

				console.time("decode any");
				for (var i = 0, j = 0; i < idd.length; i += 4, j++) {
					ZXing.HEAPU8[image + j] = idd[i];
				}
				err = ZXing._decode_any(decodePtr);
				console.timeEnd("decode any");

				console.time("decode multi");
				for (var i = 0, j = 0; i < idd.length; i += 4, j++) {
					ZXing.HEAPU8[image + j] = idd[i];
				}
				err = ZXing._decode_multi(decodePtr);
				console.timeEnd("decode multi");

			};
		};
	</script>
  <script async src="zxing.js"></script>

WebAssembly ZXing

To build:

  1. cd build-wasm
  2. Run configure.bat
  3. Run build.bat
  4. Add the path of build-wasm folder to IIS.
  5. Open http://localhost:2588/test.html.

To use:

    <script>
		var ZXing;
		var Module = {
			onRuntimeInitialized: function () {
				ZXing = Module;
				testZXing();
			}
		};

		function testZXing() {
			var img = new Image;
			img.src = 'Qr-10.png';
			img.onload = function () {

				var width = Math.floor(this.width),
					height = Math.floor(this.height);

				var canvas = document.createElement('canvas');
				canvas.style.display = 'block';
				canvas.width = width;
				canvas.height = height;
				var ctx = canvas.getContext('2d');
				// ctx.rotate(Math.random()*0.1-0.05);
				ctx.drawImage(this, 0, 0, width, height);
				var imageData = ctx.getImageData(0, 0, width, height);
				var idd = imageData.data;
				document.body.appendChild(canvas);

				var decodeCallback = function (ptr, len, resultIndex, resultCount) {
					var result = new Uint8Array(ZXing.HEAPU8.buffer, ptr, len);
					window.resultString = String.fromCharCode.apply(null, result);
				};
				var decodePtr = ZXing.Runtime.addFunction(decodeCallback);

				var image = ZXing._resize(width, height);

				for (var i = 0, j = 0; i < idd.length; i += 4, j++) {
					ZXing.HEAPU8[image + j] = idd[i];
				}

				var err = ZXing._decode_qr(decodePtr);

				console.log("error code", err);
				console.log("result", window.resultString);

				document.body.appendChild(document.createTextNode(err ? ("error: " + err) : window.resultString));

				for (var k = 0; k < 50; k++) {
					for (var i = 0, j = 0; i < idd.length; i += 4, j++) {
						ZXing.HEAPU8[image + j] = idd[i];
					}
					err = ZXing._decode_qr_multi(decodePtr);
					err = ZXing._decode_qr(decodePtr);
				}

				console.time("decode QR");
				for (var i = 0, j = 0; i < idd.length; i += 4, j++) {
					ZXing.HEAPU8[image + j] = idd[i];
				}
				err = ZXing._decode_qr(decodePtr);
				console.timeEnd("decode QR");

				console.time("decode QR multi");
				for (var i = 0, j = 0; i < idd.length; i += 4, j++) {
					ZXing.HEAPU8[image + j] = idd[i];
				}
				err = ZXing._decode_qr_multi(decodePtr);
				console.timeEnd("decode QR multi");

				console.time("decode any");
				for (var i = 0, j = 0; i < idd.length; i += 4, j++) {
					ZXing.HEAPU8[image + j] = idd[i];
				}
				err = ZXing._decode_any(decodePtr);
				console.timeEnd("decode any");

				console.time("decode multi");
				for (var i = 0, j = 0; i < idd.length; i += 4, j++) {
					ZXing.HEAPU8[image + j] = idd[i];
				}
				err = ZXing._decode_multi(decodePtr);
				console.timeEnd("decode multi");

			};
		};
	</script>
  <script async src="zxing.js"></script>

Performance

JavaScript ZXing vs WebAssembly ZXing

Blog

zxing-cpp-emscripten's People

Contributors

benjamin-dobell avatar bentley avatar bombino avatar dushibaiyu avatar guoxiao avatar kenny1847 avatar kig avatar mdchaney avatar mojodna avatar sebgdev avatar skylook avatar thekvs avatar tpetazzoni avatar yushulx avatar zz85 avatar

Watchers

 avatar

Forkers

marcusds

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.