Reading QRCode with Zxing in Java

前端 未结 3 1896
春和景丽
春和景丽 2020-12-13 21:05

Some questions about using Zxing...

I write the following code to read barcode from an image:

public class BarCodeDecode 
{
    /**
     * @param arg         


        
相关标签:
3条回答
  • 2020-12-13 21:42

    Curiously your code works for me, but I had to remove the follow hint.

    tmpHintsMap.put(DecodeHintType.PURE_BARCODE, Boolean.FALSE);

    When my image is not pure barcode, this hint broke my result.

    Thank you!

    0 讨论(0)
  • 2020-12-13 21:44

    Please go through this link for complete Tutorial. The author of this code is Joe. I have not developed this code, so I am just doing Copy paste to make sure this is available in case link is broken.

    The author is using ZXing(Zebra Crossing Library) you can download it from here, for this tutorial.

    QR Code Write and Read Program in Java:

    package com.javapapers.java;
    
    
    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 {
    
      public static void main(String[] args) throws WriterException, IOException,
          NotFoundException {
        String qrCodeData = "Hello World!";
        String filePath = "QRCode.png";
        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));
    
      }
    
      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, hintMap);
        MatrixToImageWriter.writeToFile(matrix, filePath.substring(filePath
            .lastIndexOf('.') + 1), new File(filePath));
      }
    
      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();
      }
    }
    

    Maven dependency for the ZXing QR Code library:

    <dependency>
      <groupId>com.google.zxing</groupId>
      <artifactId>core</artifactId>
      <version>2.2</version>
    </dependency>
    
    <dependency>
      <groupId>com.google.zxing</groupId>
      <artifactId>javase</artifactId>
      <version>2.2</version>
    </dependency>
    
    0 讨论(0)
  • 2020-12-13 22:03

    This Code worked for me.

    public static List<string> ScanForBarcodes(string path)
    {
      return ScanForBarcodes(new Bitmap(path));
    }
    
    public static List<string> ScanForBarcodes(Bitmap bitmap)
    {
      // initialize a new Barcode reader.
      BarcodeReader reader = new BarcodeReader
      {
        TryHarder = true, // TryHarder is slower but recognizes more Barcodes
        PossibleFormats = new List<BarcodeFormat> // in the ZXing There is an Enum where all supported barcodeFormats were contained
        {
          BarcodeFormat.CODE_128, 
          BarcodeFormat.QR_CODE,
          //BarcodeFormat. ... ;
        }
      };
      return reader.DecodeMultiple(bitmap).Select(result => result.Text).ToList(); // return only the barcode string.
                                                                                   // If you want the full Result use: return reader.DecodeMultiple(bitmap);
    }
    

    Did you use this (ZXing.Net) Lib?

    0 讨论(0)
提交回复
热议问题