qq14017152 2015-01-22 02:27 采纳率: 50%
浏览 2318

java用zxing读取图片当中的条形码

请教个问题:一张图片当中在不固定位置包含有条形码,怎么读取条形码出来呢?怎么读取一张图片中的多个条形码?用zxing读取图片当中的条形码,报错com.google.zxing.NotFoundException。有大神知道的吗?谢谢

  • 写回答

1条回答 默认 最新

  • yushulx 2020-03-16 10:36
    关注

    首先把图像读到BufferedImage

    import java.awt.image.*;
    import javax.imageio.ImageIO;
    BufferedImage image = null;
            try {
                image = ImageIO.read(new File(filename));
            } catch (IOException e) {
                System.out.println(e);
                return;
            }
    
    

    然后转换成BinaryBitmap传给ZXing

    BinaryBitmap bitmap = null;
    int[] pixels = image.getRGB(0, 0, image.getWidth(), image.getHeight(), null, 0, image.getWidth());
    RGBLuminanceSource source = new RGBLuminanceSource(image.getWidth(), image.getHeight(), pixels);
    bitmap = new BinaryBitmap(new HybridBinarizer(source));
    
    MultiFormatReader reader = new MultiFormatReader();  
    GenericMultipleBarcodeReader multiReader = new GenericMultipleBarcodeReader(reader);
            try {
                Result[] zxingResults = multiReader.decodeMultiple(bitmap);
            } catch (NotFoundException e) {
                e.printStackTrace();
            }
            pixels = null;
            bitmap = null;
    
    

    完整代码
    https://github.com/yushulx/java-barcode-command-gui-web

    评论

报告相同问题?