Google开源项目ZXing(二维条码编解码)简单使用(Java版)

ZXing (pronounced "zebra crossing") is an open-source, multi-format 1D/2D barcode image processing library implemented in Java, with ports to other languages. Our focus is on using the built-in camera on mobile phones to scan and decode barcodes on the device, without communicating with a server. However the project can be used to encode and decode barcodes on desktops and servers as well. 

ZXing项目是google code上面提供的一个关于条码编解码的开源项目。

要对本项目进行二次开发,首先你需要在源码下载列表中下载ZXing1.7.zip源代码

解压文件,你会看到里面有很多文件夹,包括J2SE的,Android的,J2ME,C#等等。我们以J2SE为例。

你需要使用里面的Core和J2SE文件夹。然后导入到你在Eclipse中创建的工程,目录形式如下:

 

J2SE包中,提供了编码和解码的.Java文件,但是由于写的过于复杂,还没弄清楚这个开源项目源码之前,还是不推荐使用。(You can download jar file from my attachment)

自己在j2se包中创建两个Java文件Encoder和Decoder。代码如下:

package com.google.zxing.client.j2se;  
  
import java.io.File;  
import java.io.IOException;  
import java.util.Hashtable;  
  
import com.google.zxing.BarcodeFormat;  
import com.google.zxing.EncodeHintType;  
import com.google.zxing.MultiFormatWriter;  
import com.google.zxing.WriterException;  
import com.google.zxing.common.BitMatrix;  
  
public class Encoder {  
  
    public static void main(String[] args) {  
  
        String contents = "今天,我们来简单聊聊google开源项目——ZXing(二维条码编解码)";  
  
        Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();  
  
        hints.put(EncodeHintType.CHARACTER_SET, "GBK");  
  
        BitMatrix matrix = null;  
  
        try {  
  
            matrix = new MultiFormatWriter().encode(contents,  
                    BarcodeFormat.QR_CODE, 300, 300, hints);  
  
        } catch (WriterException e) {  
  
            e.printStackTrace();  
  
        }  
  
        File file = new File("D://qrcodeImage.png");  
  
        try {  
  
            MatrixToImageWriter.writeToFile(matrix, "png", file);  
  
        } catch (IOException e) {  
  
            e.printStackTrace();  
  
        }  
  
    }  
}  
package com.google.zxing.client.j2se;  
  
import java.awt.image.BufferedImage;  
import java.io.File;  
import java.io.IOException;  
import java.util.Hashtable;  
  
import javax.imageio.ImageIO;  
  
import com.google.zxing.BinaryBitmap;  
import com.google.zxing.DecodeHintType;  
import com.google.zxing.LuminanceSource;  
import com.google.zxing.MultiFormatReader;  
import com.google.zxing.NotFoundException;  
import com.google.zxing.Result;  
import com.google.zxing.common.HybridBinarizer;  
  
public class Decoder {  
      
    public static void main(String[] args) {  
  
        File file = new File("D://qrcodeImage.png");  
  
        BufferedImage bufferedImage = null;  
  
        try {  
  
            bufferedImage = ImageIO.read(file);  
  
        } catch (IOException e) {  
  
            e.printStackTrace();  
  
        }  
  
        LuminanceSource source = new BufferedImageLuminanceSource(bufferedImage);  
  
        BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));  
  
        Hashtable<DecodeHintType, String> hints = new Hashtable<DecodeHintType, String>();  
  
        hints.put(DecodeHintType.CHARACTER_SET, "GBK");  
  
        Result result = null;  
  
        try {  
  
            result = new MultiFormatReader().decode(bitmap, hints);  
  
        } catch (NotFoundException e) {  
  
            e.printStackTrace();  
  
        }  
  
        System.out.println(result.toString());  
  
    }  
  
}  

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当然可以!ZXing(Zebra Crossing)是一个功能强大的开源条形码和二维码扫描库,它提供了多种程语言的本,包括C++。以下是使用C++ZXing库进行条形码识别的简单示例: 首先,您需要从ZXing的官方GitHub仓库(https://github.com/nu-book/zxing-cpp)下载并安装C++ZXing库。 接下来,您可以使用以下示例代码来扫描和识别条形码: ```cpp #include <iostream> #include <zxing/BarcodeReader.h> #include <zxing/BarcodeFormat.h> #include <zxing/DecodeHints.h> #include <zxing/GenericLuminanceSource.h> #include <zxing/MultiFormatReader.h> #include <zxing/common/HybridBinarizer.h> #include <opencv2/opencv.hpp> int main() { // 读取图像 cv::Mat image = cv::imread("barcode_image.jpg", cv::IMREAD_GRAYSCALE); // 创建ZXing解码zxing::MultiFormatReader reader; zxing::Ref<zxing::GenericLuminanceSource> source = zxing::GenericLuminanceSource::create(image.data, image.cols, image.rows, image.cols, image.rows); zxing::Ref<zxing::HybridBinarizer> binarizer(new zxing::HybridBinarizer(source)); zxing::Ref<zxing::BinaryBitmap> bitmap(new zxing::BinaryBitmap(binarizer)); // 设置解码参数 zxing::DecodeHints hints; hints.setTryHarder(true); // 扫描条形码 zxing::Ref<zxing::Result> result = reader.decode(bitmap, hints); // 处理扫描结果 std::cout << "扫描结果: " << result->getText()->getText() << std::endl; return 0; } ``` 请将代码中的`barcode_image.jpg`替换为您要扫描的实际图像路径。确保您已经安装了OpenCV和C++ZXing库,并将相关头文件和库文件正确链接到您的项目中。 这只是一个简单的示例,您可以根据自己的需求进行更多的定制和优化。希望对您有所帮助!如有任何问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值