Find QR code in image and decode it using Zxing

user3646022

First of all, I read through all those topics how to use Zxing in Java but always got errors with missing com.google.zxing.client.j2se.* (I loaded the zxing core-3.2.1.jar in eclipse and all other zxing packages work unless j2se) or just found solutions for creating qr images...

My aim is to write one single method which gets an image file finds the qr code in this image, decodes the qr code and returns the string, basically it should be something like the following:

import com.google.zxing.*;

public class QRCode {

    /*
     * ...
     */

    public String getDecodedString(SomeStandardImageType photo){
        // detect the qr code in a photo
        // create qr image from detected area in photo
        // decode the new created qr image and return the string
        return "This is the decoded dataString from the qr code in the photo";
    }

}

To sum up the method should get an image file like the following

enter image description here

and should return the url or if failed just "".

The code should be compatible with Zxing 3.2.1.

Edit: The question is solved. For others who are interested in this I want to say that it is important to add both external jars core-3.2.1.jar and javase-3.2.1.jar to external jars. The answer by me works without the latter but depends on android image libs.

Sandip Bhoi

here is the code to create the Qr-Code and read Message from Qr-code

  1. you need the build the zxing library

  2. main describe the qr-code creation and qr-code extraction

    package com.attendance.mark;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.util.HashMap;
    import java.util.Map;
    
    import javax.imageio.ImageIO;
    
    import com.google.zxing.BarcodeFormat;
    import com.google.zxing.BinaryBitmap;
    import com.google.zxing.EncodeHintType;
    import com.google.zxing.MultiFormatReader;
    import com.google.zxing.MultiFormatWriter;
    import com.google.zxing.NotFoundException;
    import com.google.zxing.Result;
    import com.google.zxing.WriterException;
    import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
    import com.google.zxing.client.j2se.MatrixToImageWriter;
    import com.google.zxing.common.BitMatrix;
    import com.google.zxing.common.HybridBinarizer;
    import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
    
    public class QRCode {
    
        /**
         * 
         * @param args 
         * @throws WriterException
         * @throws IOException
         * @throws NotFoundException
         */
      public static void main(String[] args) throws WriterException, IOException,
          NotFoundException {
        String qrCodeData = "student3232_2015_12_15_10_29_46_123";
        String filePath = "F:\\Opulent_ProjectsDirectory_2015-2016\\.metadata\\.plugins\\org.eclipse.wst.server.core\\tmp0\\wtpwebapps\\AttendanceUsingQRCode\\QRCodes\\student3232_2015_12_15_10_29_46_123";
        String charset = "UTF-8"; // or "ISO-8859-1"
        Map<EncodeHintType, ErrorCorrectionLevel> hintMap = new HashMap<EncodeHintType, ErrorCorrectionLevel>();
        hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
    
        createQRCode(qrCodeData, filePath, charset, hintMap, 200, 200);
        System.out.println("QR Code image created successfully!");
    
        System.out.println("Data read from QR Code: "
            + readQRCode(filePath, charset, hintMap));
    
      }
    
      /***
       * 
       * @param qrCodeData
       * @param filePath
       * @param charset
       * @param hintMap
       * @param qrCodeheight
       * @param qrCodewidth
       * @throws WriterException
       * @throws IOException
       */
      public static void createQRCode(String qrCodeData, String filePath,
          String charset, Map hintMap, int qrCodeheight, int qrCodewidth)
          throws WriterException, IOException {
        BitMatrix matrix = new MultiFormatWriter().encode(
            new String(qrCodeData.getBytes(charset), charset),
            BarcodeFormat.QR_CODE, qrCodewidth, qrCodeheight);
        MatrixToImageWriter.writeToFile(matrix, filePath.substring(filePath
            .lastIndexOf('.') + 1), new File(filePath));
      }
    
      /**
       * 
       * @param filePath
       * @param charset
       * @param hintMap
       * 
       * @return Qr Code value 
       * 
       * @throws FileNotFoundException
       * @throws IOException
       * @throws NotFoundException
       */
      public static String readQRCode(String filePath, String charset, Map hintMap)
          throws FileNotFoundException, IOException, NotFoundException {
        BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(
            new BufferedImageLuminanceSource(
                ImageIO.read(new FileInputStream(filePath)))));
        Result qrCodeResult = new MultiFormatReader().decode(binaryBitmap, hintMap);
        return qrCodeResult.getText();
      }
    }
    

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

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

From Dev

How to create a QR code image using Zxing on Windows Phone 8.1

From Dev

How to recognize QR code from image using ZXing library?

From Dev

ZXing.Net cannot decode the QR code captured by Camera

From Dev

Null value in ZXing QR Code Reader decode function

From Dev

Android Generate QR code and Barcode using Zxing

From Dev

I am not able see generated QR Code Image in Xamarin using ZXing.NET.Mobile

From Dev

QR Code is not recognized by ZXing

From Dev

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

From Dev

QR-encode a String to Image in Android project using zxing

From Dev

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

From Dev

Android: Generated QR code using Zxing has margins (is not fit to the area)

From Dev

decode qrcode using zxing

From Dev

ZXing QR Code/Barcode scanning

From Java

How to decode a QR-code image in (preferably pure) Python?

From Dev

Xamarin Android QR-Image ZXing - Not working

From Dev

Unity Zxing QR code scanner integration

From Dev

Zxing java not reading valid qr code

From Dev

Reading DataMatrix/QR code zxing java

From Dev

Xamarin Forms ZXing QR Code Error

From Dev

intermittent QR Scan results using ZXing IntentIntegrator

From Dev

Scanning QR Image With Zxing from Windows Phone Image Library

From Dev

Scanning QR Image With Zxing from Windows Phone Image Library

From Dev

QR code to image

From Dev

QR Code image Size

From Dev

decode QR code from data URI

From Dev

how to embed a QR code with existing image using java script?

From Dev

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

From Dev

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

Related Related

  1. 1

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

  2. 2

    How to create a QR code image using Zxing on Windows Phone 8.1

  3. 3

    How to recognize QR code from image using ZXing library?

  4. 4

    ZXing.Net cannot decode the QR code captured by Camera

  5. 5

    Null value in ZXing QR Code Reader decode function

  6. 6

    Android Generate QR code and Barcode using Zxing

  7. 7

    I am not able see generated QR Code Image in Xamarin using ZXing.NET.Mobile

  8. 8

    QR Code is not recognized by ZXing

  9. 9

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

  10. 10

    QR-encode a String to Image in Android project using zxing

  11. 11

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

  12. 12

    Android: Generated QR code using Zxing has margins (is not fit to the area)

  13. 13

    decode qrcode using zxing

  14. 14

    ZXing QR Code/Barcode scanning

  15. 15

    How to decode a QR-code image in (preferably pure) Python?

  16. 16

    Xamarin Android QR-Image ZXing - Not working

  17. 17

    Unity Zxing QR code scanner integration

  18. 18

    Zxing java not reading valid qr code

  19. 19

    Reading DataMatrix/QR code zxing java

  20. 20

    Xamarin Forms ZXing QR Code Error

  21. 21

    intermittent QR Scan results using ZXing IntentIntegrator

  22. 22

    Scanning QR Image With Zxing from Windows Phone Image Library

  23. 23

    Scanning QR Image With Zxing from Windows Phone Image Library

  24. 24

    QR code to image

  25. 25

    QR Code image Size

  26. 26

    decode QR code from data URI

  27. 27

    how to embed a QR code with existing image using java script?

  28. 28

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

  29. 29

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

HotTag

Archive