【问题标题】:How to setup zxing library on Windows 8 machine?如何在 Windows 8 机器上设置 zxing 库?
【发布时间】:2014-07-28 00:53:44
【问题描述】:

我有想要解码的代码图像。如何使用 zxing 来指定图像位置并取回解码后的文本,以防解码失败(对于某些图像,这就是项目),它会给我一个错误。

如何在我的 Windows 机器上设置 zxing?我下载了jar文件,但我不知道从哪里开始。我知道我必须创建一个代码来读取图像并将其提供给库阅读器方法,但是如何做到这一点的指南会非常有帮助。

【问题讨论】:

  • 我在Android上也找到了很多帖子,但我只是想要一个简单的解码器。我不想用我的相机。只需将图像指定给方法并获取解码结果

标签: java zxing


【解决方案1】:

我能够做到。下载源代码并添加以下代码。有点质朴,但可以完成工作。

import com.google.zxing.NotFoundException;
import com.google.zxing.ChecksumException;
import com.google.zxing.FormatException;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.DecodeHintType;
import com.google.zxing.Reader;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.Result;
import com.google.zxing.LuminanceSource;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.HybridBinarizer;

import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.io.File;
import java.io.IOException;
import java.util.*;

import com.google.zxing.qrcode.QRCodeReader;

class qr
{
    public static void main(String args[])
    {
        Reader xReader = new QRCodeReader();
        BufferedImage dest = null;

        try
        {
            dest = ImageIO.read(new File(args[0]));
        }
        catch(IOException e)
        {
            System.out.println("Cannot load input image");
        }
        LuminanceSource source = new BufferedImageLuminanceSource(dest);

        BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
        Vector<BarcodeFormat> barcodeFormats = new Vector<BarcodeFormat>();
        barcodeFormats.add(BarcodeFormat.QR_CODE);

        HashMap<DecodeHintType, Object> decodeHints = new HashMap<DecodeHintType, Object>(3);
        decodeHints.put(DecodeHintType.POSSIBLE_FORMATS, barcodeFormats);

        decodeHints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);

        Result result = null;

        try
        {
            result = xReader.decode(bitmap, decodeHints);
            System.out.println("Code Decoded");
            String text = result.getText();
            System.out.println(text);
        }
        catch(NotFoundException e)
        {
            System.out.println("Decoding Failed");
        }
        catch(ChecksumException e)
        {
            System.out.println("Checksum error");
        }
        catch(FormatException e)
        {
            System.out.println("Wrong format");
        }
    }
}

【讨论】:

    【解决方案2】:

    该项目包括一个名为CommandLineRunner 的类,您可以简单地从命令行调用它。您还可以查看其源代码以了解其工作原理并重复使用。

    无需安装或设置。这是一个图书馆。通常,您不会下载 jar,而是将其声明为基于 Maven 的项目中的依赖项。

    如果您只想发送图像进行解码,请使用http://zxing.org/w/decode.jspx

    【讨论】:

      猜你喜欢
      • 2020-05-22
      • 1970-01-01
      • 2016-11-03
      • 2013-07-02
      • 2017-02-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多