Google Zxing API无法解码条形码

时间:2018-09-04 10:51:49

标签: java zxing bufferedimage

我正在使用Java的google zxing api解码条形码png图像。代码如下。new MultiFormatReader()。decode(bitmap).getText())函数不适用于所有条形码图像。如果需要其他任何详细信息,以及是否有更好的方法使用Google zxing解码条形码图像,请告诉我。

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.imageio.ImageIO;

import org.apache.commons.codec.BinaryDecoder;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.LuminanceSource;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.NotFoundException;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.QRCodeReader;

public class DecodeBarcode {

    public static void main(String[] args) throws IOException {
        BufferedImage qrCodeImage = null;
        DateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        Date date = new Date();
        String imgname = "img_file_name.png";
        qrCodeImage = ImageIO.read(new File("C:/Users/pathToTheFile/" + imgname));

        try {
            LuminanceSource source = new BufferedImageLuminanceSource(qrCodeImage);
            BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
            System.out.println("Decoded text===>" + new MultiFormatReader().decode(bitmap).getText());


        } catch (NotFoundException e) {
            System.out.println("barcode was not decoded successfully");
        }
    }

}

0 个答案:

没有答案