Skip to main content

How to create a live Barcode scanner using the webcam in JavaScript

There are many good and bad things about QR Codes, they have cropped up everywhere from billboards to water bottles, and have become a must have for some marketers. The opinion about whether these codes are good or not, i'll leave it to your own opinion. Most of the QRCodes that you will see are very tipical, black and white and therefore boring which ends up with little atention from the customer.

Be original and create a fabulous (or maybe creepy) QR Code merging an image on it using the JavaScript library Qart.js

Download and include qart.js

Qart.js is an awesome QR Code generator with a custom functionality that allows you to merge an image into the QR Code. In this way the generated QR Codes will have an original and artistic touch.

To install qart.js, you will need to download a copy of the source code. You can install the plugin directly with NPM executing the following command in your terminal:

npm install qartjs

Or if you are willing to the test the plugin, use it from the CDN:

<script src="//cdnjs.cloudflare.com/ajax/libs/qartjs/1.0.2/qart.min.js"></script>

Alternatively, if you don't use a package manager, get a copy of the qart.min.js file from the repository in the dist folder and include it with a script tag in your document. Once the plugin is included in your project with any of the mentioned ways, you will be able to access or require the plugin in your code as follows:

// With ES6
import QArt from 'qartjs';
// Browserify
var QArt = require('qartjs');

// If you are using qart.js in the browser
// the global variable QArt will be available in the window.

With this plugin, you will say NO to the boring QR Codes that you see everyday and everywhere. For more information about the plugin, please visit the official repository in Github here.

Create QR Code with an image

The QArt function expects an object with the properties shown in the following table:

FIELDTYPEDESCRIPTIONDEFAULT
valueStringThe data of the QR code.Required
imagePathStringThe path of the combined image.Required
filterStringDefine an image filter. threshold or colorthreshold
versionIntegerQRCode version (1 <= version <= 40)10

The QArt function returns an object, from the returned object you need to use the make method to append it into a DOM element (preferably a div). See the following example:

<!-- A div container element that qart will use to insert the Canvas-->
<div id="area"></div>
<!-- The qart.js script -->
<script src="qart.js"></script>

<script>
    var QR_CONTENT = "This is the content of the QR Code";

    //var QR_IMAGE = "/path/to/image_png_or.jpg";
    // Important: the image needs to be hosted in the same domain of the script
    // otherwise you won't be able to make the plugin works (tainted canvas issue)
    var QR_IMAGE = "hqdefault.jpg";

    // Mode of the image in the QR code Possible values: "threshold" or "color"
    var QR_FILTER = "color";

    // The element of the document in which the qr code will be drawn
    var QR_CONTAINER = document.getElementById("area");

    // Render the QR Code
    new QArt({
        value: QR_CONTENT,
        imagePath: QR_IMAGE,
        filter: QR_FILTER
    }).make(QR_CONTAINER);
</script>

Or if you're working with EcmaScript 2016:

import QArt from 'qartjs';

const qart = new QArt({
	value: "QR Code Content",
	imagePath: './example.png',
	filter: "color"
});

qart.make(document.getElementById('qart'));

Using qart.js with famous frameworks

If you use a framework like React, Angular or Vue, then you don't need to create a custom component for qarts as they already exist:

React

The React component of qart.js is available as a NPM module written by @BatuhanK. To install it in your project, execute the following command in your terminal:

npm install react-qart

And you can use it like:

import React, { Component } from 'react';

// Import QArt
import QArt from 'react-qart';

export default class App extends Component {
    render() {
        return (
            <div>
                <h1>Hello QR Custom</h1>
                <QArt
                    value="QR Content"
                    imagePath="./image.jpg"
                    filter="color"
                />
            </div>
        )
    }
}

Angular

The Angular directive of qart.js is available as a NPM module written by @isonet. To install it in your project, execute the following command in your terminal:

npm install angular-qart

And use it like:

angular.module('MyExampleApp', ['angular-qart']);

Vue.js

The Vue.js component of qart.js is available as a NPM module written by @superman66. To install it in your project, execute the following command in your terminal:

npm install vue-qart

Then, import the component:

import VueQArt from 'vue-qart'

new Vue({
    components: {VueQArt}
})

Once the component is loaded, you can use it in your template:

<vue-q-art :config="config"></vue-q-art>

Don't forget to add the required data:

data () {
    return {
        msg: 'Welcome to Your Vue.js App',
        config: {
            value: 'https://www.baidu.com',
            imagePath: './examples/assets/logo.png',
            filter: 'color'
        },
    }
}

Limitations

Till the date of publication of this article, you cannot change the size of the generated qr. The dimensions of the generated canvas will be always of 195x195.

From the usage of the plugin, you will obtain a totally readable QR Code for any app or device that allows to read QR Codes. You can see a live example of how qart.js works in the browser here. The demo allows you to select any image from your computer or mobile device and it will immediately convert it into an artistic QR Code with or without colors.

Happy coding !

Comments

Popular posts from this blog

How to use Ngx-Charts in Angular ?

Charts helps us to visualize large amount of data in an easy to understand and interactive way. This helps businesses to grow more by taking important decisions from the data. For example, e-commerce can have charts or reports for product sales, with various categories like product type, year, etc. In angular, we have various charting libraries to create charts.  Ngx-charts  is one of them. Check out the list of  best angular chart libraries .  In this article, we will see data visualization with ngx-charts and how to use ngx-charts in angular application ? We will see, How to install ngx-charts in angular ? Create a vertical bar chart Create a pie chart, advanced pie chart and pie chart grid Introduction ngx-charts  is an open-source and declarative charting framework for angular2+. It is maintained by  Swimlane . It is using Angular to render and animate the SVG elements with all of its binding and speed goodness and uses d3 for the excellent math functio...

Understand Angular’s forRoot and forChild

  forRoot   /   forChild   is a pattern for singleton services that most of us know from routing. Routing is actually the main use case for it and as it is not commonly used outside of it, I wouldn’t be surprised if most Angular developers haven’t given it a second thought. However, as the official Angular documentation puts it: “Understanding how  forRoot()  works to make sure a service is a singleton will inform your development at a deeper level.” So let’s go. Providers & Injectors Angular comes with a dependency injection (DI) mechanism. When a component depends on a service, you don’t manually create an instance of the service. You  inject  the service and the dependency injection system takes care of providing an instance. import { Component, OnInit } from '@angular/core'; import { TestService } from 'src/app/services/test.service'; @Component({ selector: 'app-test', templateUrl: './test.component.html', styleUrls: ['./test.compon...

How to solve Puppeteer TimeoutError: Navigation timeout of 30000 ms exceeded

During the automation of multiple tasks on my job and personal projects, i decided to move on  Puppeteer  instead of the old school PhantomJS. One of the most usual problems with pages that contain a lot of content, because of the ads, images etc. is the load time, an exception is thrown (specifically the TimeoutError) after a page takes more than 30000ms (30 seconds) to load totally. To solve this problem, you will have 2 options, either to increase this timeout in the configuration or remove it at all. Personally, i prefer to remove the limit as i know that the pages that i work with will end up loading someday. In this article, i'll explain you briefly 2 ways to bypass this limitation. A. Globally on the tab The option that i prefer, as i browse multiple pages in the same tab, is to remove the timeout limit on the tab that i use to browse. For example, to remove the limit you should add: await page . setDefaultNavigationTimeout ( 0 ) ;  COPY SNIPPET The setDefaultNav...