Java 如何在Windows 8计算机上设置zxing库?

Java 如何在Windows 8计算机上设置zxing库?,java,zxing,Java,Zxing,我有我想要解码的代码图像。我如何使用zxing来指定图像位置并获取解码后的文本,如果解码失败(对于某些图像,这是项目),它会给我一个错误 如何在Windows计算机上设置zxing?我下载了jar文件,但我不知道从哪里开始。我知道我必须创建一个代码来读取图像,并将其提供给library reader方法,但是一个如何执行的指南将非常有用。我能够做到这一点。下载源代码并添加以下代码。有点土气,但能完成工作 import com.google.zxing.NotFoundException; imp

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


如何在Windows计算机上设置zxing?我下载了jar文件,但我不知道从哪里开始。我知道我必须创建一个代码来读取图像,并将其提供给library reader方法,但是一个如何执行的指南将非常有用。

我能够做到这一点。下载源代码并添加以下代码。有点土气,但能完成工作

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");
        }
    }
}
import com.google.zxing.NotFoundException;
导入com.google.zxing.ChecksumException;
导入com.google.zxing.FormatException;
导入com.google.zxing.BarcodeFormat;
导入com.google.zxing.DecodeHintType;
导入com.google.zxing.Reader;
导入com.google.zxing.BinaryBitmap;
导入com.google.zxing.Result;
导入com.google.zxing.LuminanceSource;
导入com.google.zxing.client.j2se.BufferedImageLuminanceSource;
导入com.google.zxing.common.HybridBinarizer;
导入java.awt.image.buffereImage;
导入javax.imageio.imageio;
导入java.io.File;
导入java.io.IOException;
导入java.util.*;
导入com.google.zxing.qrcode.QRCodeReader;
类qr
{
公共静态void main(字符串参数[])
{
Reader xReader=新的QRCodeReader();
BuffereImage dest=null;
尝试
{
dest=ImageIO.read(新文件(args[0]);
}
捕获(IOE异常)
{
System.out.println(“无法加载输入图像”);
}
亮度源=新的缓冲区图像亮度源(dest);
BinaryBitmap位图=新的BinaryBitmap(新的混合二进制程序(源));
矢量条码格式=新矢量();
添加(BarcodeFormat.QR_代码);
HashMap decodeHits=新的HashMap(3);
decodeHints.put(DecodeHintType.mables_格式,条形码格式);
DecodeHintType.TRY_,Boolean.TRUE);
结果=空;
尝试
{
结果=xReader.decode(位图、解码提示);
System.out.println(“代码解码”);
String text=result.getText();
System.out.println(文本);
}
捕获(未发现异常)
{
System.out.println(“解码失败”);
}
捕获(检查异常)
{
System.out.println(“校验和错误”);
}
捕获(格式化异常)
{
System.out.println(“格式错误”);
}
}
}

该项目包括一个名为
CommandLineRunner
的类,您只需从命令行调用该类即可。您还可以查看它的源代码,了解它是如何工作的并重用它

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


如果你只想发送一张图像进行解码,那么使用

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