Scan the QR code picture directly from camera with ZXing

Yusril Maulidan Raji

I developed a ASP website (the target is for mobile users) with the main functionality to decode the QR code. I decided to use ZXing as the decoder and if I input the good image of QR code, it works perfectly. But when I change the source of the QR code by taking the picture directly from the camera with:

<input type="file" name="file" capture="camera" accept="image/*" id="cameraInput" name="cameraInput">

It doesn't work. Even though I take the picture as stable as I can, ZXing still cannot decode that picture from the camera. The question is do you know what is the best way to decode QR code that is taken from camera? If this will take a lot of effort, then do you any other suggestion for decoding QR code (with the web platform of ASP.NET).

Yusril Maulidan Raji

So, I already have the solution. In fact, the problem is not with the ZXing, but with the result of the taken image. Because when I try to take the image, and directly convert and show it into canvas, the result of the picture is tremendous big. That is why ZXing failed to decode it. But when I try to manage the canvas image first (to be 400 x 400 px in my case), send the base64 image to the server, convert it into image again in the server, and decode it. It works perfect like a charm.

Edited: Here is the code that I used for the solution (in order to make it readable, I remove some unrelated code. hopefully I didn't remove the useful part though)

var JsIndexPage = new function (jsonData) {
    this.pageData = {
        maxWidth: 400,
        maxHeight: 400
    };

    this.pageUI = {
        canvas: null,
        blankCanvas: null,
        messageDiv: null,
        cameraInput: null,
        uploadInput: null
    };

    this.initUI = function () {
        ///<summary>
        ///Display ui on the page
        ///</summary>

        this.pageUI.canvas = document.getElementById('capturedPhoto');
        this.pageUI.blankCanvas = document.getElementById('blank');
        this.pageUI.cameraInput = $('#cameraInput');
        this.pageUI.messageDiv = $("#message");
        this.pageUI.uploadInput = $('#uploadInput');

        //add triggers to buttons
        this.pageUI.cameraInput.on('change', function (e) {
            JsIndexPage.onCameraInputChanged(e);
        });

        this.pageUI.uploadInput.on('click', function () {
            JsIndexPage.onUploadInputClick();
        });
    };

    this.onCameraInputChanged = function (e) {
        /// <summary>
        /// On camera input changed write image to canvas
        /// </summary>

        var reader = new FileReader();
        reader.onload = function (event) {
            JsIndexPage.onFileSelected(event);
        }

        //contains the data as a URL representing the file's data as a base64 encoded string
        reader.readAsDataURL(e.target.files[0]);
    };

    this.onFileSelected = function (event) {
        ///<summary>
        /// Html5 file readed onLoad event handler
        ///</summary>

        var context = this.pageUI.canvas.getContext('2d');

        //instantiates the Image() object
        var img = new Image();
        img.onload = function () {

            //converts the sizes of image into the proper size (400 x 400 at maximum)
            if (img.width > JsIndexPage.pageData.maxWidth && img.height <= JsIndexPage.pageData.maxHeight) {
                JsIndexPage.pageUI.canvas.width = JsIndexPage.pageUI.maxWidth;
                JsIndexPage.pageUI.canvas.height = (JsIndexPage.pageUI.canvas.width / img.width) * img.height;
            }
            else if (img.width <= JsIndexPage.pageData.maxWidth && img.height > JsIndexPage.pageData.maxHeight) {
                JsIndexPage.pageUI.canvas.height = JsIndexPage.pageData.maxHeight;
                JsIndexPage.pageUI.canvas.width = (JsIndexPage.pageUI.canvas.height / img.height) * img.width;
            }
            else if (img.width <= JsIndexPage.pageData.maxWidth && img.height <= JsIndexPage.pageData.maxHeight) {
                JsIndexPage.pageUI.canvas.width = img.width;
                JsIndexPage.pageUI.canvas.height = img.height;
            }
            else if (img.width > JsIndexPage.pageData.maxWidth && img.height > JsIndexPage.pageData.maxHeight) {
                if (img.width > img.height) {
                    JsIndexPage.pageUI.canvas.width = JsIndexPage.pageData.maxWidth;
                    JsIndexPage.pageUI.canvas.height = (JsIndexPage.pageUI.canvas.width / img.width) * img.height;
                }
                else {
                    JsIndexPage.pageUI.canvas.height = JsIndexPage.pageData.maxHeight;
                    JsIndexPage.pageUI.canvas.width = (JsIndexPage.pageUI.canvas.height / img.height) * img.width;
                }
            }

            //draws the context to the canvas
            context.drawImage(img, 0, 0, JsIndexPage.pageUI.canvas.width, JsIndexPage.pageUI.canvas.height);
        }

        //changes the image source into the new one
        img.src = event.target.result;
    };
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Possibilities to scan qr-code in flutter web

From Dev

Confirming Source Is From QR Code Scan

From Dev

ChecksumException in zxing QRCodeReader when NOT reading a QR-code with an URL

From Dev

ZXing.Net cannot decode the QR code captured by Camera

From Dev

Android Generate QR code and Barcode using Zxing

From Dev

ZXing QR Code/Barcode scanning

From Dev

Decode QR code from Byte[] using ZXing in C#

From Dev

Unity Zxing QR code scanner integration

From Dev

Add custom image or text to QR code generated by ZXing.Net

From Dev

ZXing QR code scanner embedded pressing back button during scan issue

From Dev

QR code scan from image file

From Dev

Android I use Zxing Qr code, how to scan local Qr code image?

From Dev

Misterious dialog pops up after to scan a qr code with zxing library

From Dev

Find QR code in image and decode it using Zxing

From Dev

Rotating picture from android camera

From Dev

Zxing java not reading valid qr code

From Dev

intermittent QR Scan results using ZXing IntentIntegrator

From Dev

Delete picture taken from camera

From Dev

zxing minimum size on screen of qr to scan

From Dev

Decode datamatrix out of picture taken by camera2 with zxing

From Dev

How can i scan the bar code and qr code via mobile camera?

From Dev

Reading DataMatrix/QR code zxing java

From Dev

QR Code is not recognized by ZXing

From Dev

QR scan in a half screen using zxing library xamarin android

From Dev

How to recognize QR code from image using ZXing library?

From Dev

Xamarin Forms ZXing QR Code Error

From Dev

Android Studio Zxing Scanner, how to redirect to web page if result from QR code is URL

From Dev

Animation with scan QR code

From Dev

How can I use scan QR Code with a camera inside the layout (like Whatsapp Web scanner)

Related Related

  1. 1

    Possibilities to scan qr-code in flutter web

  2. 2

    Confirming Source Is From QR Code Scan

  3. 3

    ChecksumException in zxing QRCodeReader when NOT reading a QR-code with an URL

  4. 4

    ZXing.Net cannot decode the QR code captured by Camera

  5. 5

    Android Generate QR code and Barcode using Zxing

  6. 6

    ZXing QR Code/Barcode scanning

  7. 7

    Decode QR code from Byte[] using ZXing in C#

  8. 8

    Unity Zxing QR code scanner integration

  9. 9

    Add custom image or text to QR code generated by ZXing.Net

  10. 10

    ZXing QR code scanner embedded pressing back button during scan issue

  11. 11

    QR code scan from image file

  12. 12

    Android I use Zxing Qr code, how to scan local Qr code image?

  13. 13

    Misterious dialog pops up after to scan a qr code with zxing library

  14. 14

    Find QR code in image and decode it using Zxing

  15. 15

    Rotating picture from android camera

  16. 16

    Zxing java not reading valid qr code

  17. 17

    intermittent QR Scan results using ZXing IntentIntegrator

  18. 18

    Delete picture taken from camera

  19. 19

    zxing minimum size on screen of qr to scan

  20. 20

    Decode datamatrix out of picture taken by camera2 with zxing

  21. 21

    How can i scan the bar code and qr code via mobile camera?

  22. 22

    Reading DataMatrix/QR code zxing java

  23. 23

    QR Code is not recognized by ZXing

  24. 24

    QR scan in a half screen using zxing library xamarin android

  25. 25

    How to recognize QR code from image using ZXing library?

  26. 26

    Xamarin Forms ZXing QR Code Error

  27. 27

    Android Studio Zxing Scanner, how to redirect to web page if result from QR code is URL

  28. 28

    Animation with scan QR code

  29. 29

    How can I use scan QR Code with a camera inside the layout (like Whatsapp Web scanner)

HotTag

Archive