在图像中查找QR码并使用Zxing对其进行解码 [英] Find QR code in image and decode it using Zxing

查看:81
本文介绍了在图像中查找QR码并使用Zxing对其进行解码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我通读了所有有关如何在Java中使用Zxing的主题,但始终会因缺少com.google.zxing.client.j2se而出错.*(我在eclipse中加载了zxing core-3.2.1.jar以及所有其他zxing软件包都可以使用,除非使用j2se)或刚刚找到了创建qr图像的解决方案...

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

,并应返回网址;如果失败,则返回".

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

该代码应与Zxing 3.2.1兼容.

The code should be compatible with Zxing 3.2.1.

编辑:问题已解决.对于对此感兴趣的其他人,我想说的是,同时添加外部jars core-3.2.1.jar javase-3.2.1非常重要.jar 到外部jar.我的答案在没有后者的情况下有效,但取决于android图像库.

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.

推荐答案

这是用于创建Qr代码并从Qr代码读取消息的代码

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

  1. 您需要构建zxing库

  1. you need the build the zxing library

主要描述二维码的创建和二维码提取

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();
  }
}

这篇关于在图像中查找QR码并使用Zxing对其进行解码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆