从java中上传的图像中读取条形码

时间:2021-05-14 07:08:18

I've to read barcode from an uploaded image file for a java based web application. I've tried Zxing and other similar libraries, but it will work only if we upload precise images. Could anyone suggest an api for reading barcode from an image? The image could be a randomly clicked one and it might contain other data also. From that, we have to identify the barcode and decode it.

我要从上传的图像文件中读取基于java的Web应用程序的条形码。我已经尝试过Zxing和其他类似的库,但只有在我们上传精确图像时它才会起作用。有人可以建议api从图像中读取条形码吗?图像可以是随机点击的图像,也可能包含其他数据。由此,我们必须识别条形码并对其进行解码。

1 个解决方案

#1


0  

As you did not provide a whole lot of information I will try to help you out with what I think what the problem is.

由于您没有提供大量信息,我会尽力帮助您解决问题所在。

I think ZXing should be the right way to go. The barcode decoder searches for the barcode within an image per default. If you have an image wich only contains the barcode itself you can set the hint PURE_BARCODE to speed up decoding. There only could be problems if you have multiple barcodes within a single image.

我认为ZXing应该是正确的选择。条形码解码器默认搜索图像内的条形码。如果您的图像只包含条形码本身,您可以设置提示PURE_BARCODE以加快解码速度。如果您在单个图像中有多个条形码,则只会出现问题。

Your problem seems to be the uploaded image not ZXing. I would suggest checking whether the image is uploaded correctly.

你的问题似乎是上传的图像不是ZXing。我建议检查图像是否正确上传。

Here is an example of decoding a BufferedImage with any ZXing reader.

以下是使用任何ZXing阅读器解码BufferedImage的示例。

public void decodeCode() throws IOException, NotFoundException, FormatException, ChecksumException {
    BufferedImage image = ImageIO.read(yourImage);
    BinaryBitmap bitmap = convertImageToBinaryBitmap(image);
    Result result = reader.decode(bitmap, hints);

    assertNotNull("DecoderResult must not be null", result);
    System.out.println(result.getText());
  }

 protected BinaryBitmap convertImageToBinaryBitmap(BufferedImage image) {
    int[] pixels = image.getRGB(0, 0,
                                image.getWidth(), image.getHeight(),
                                null, 0, image.getWidth());
    RGBLuminanceSource source = new RGBLuminanceSource(image.getWidth(),
                                                       image.getHeight(),
                                                       pixels);
    return new BinaryBitmap(new HybridBinarizer(source));
  }

#1


0  

As you did not provide a whole lot of information I will try to help you out with what I think what the problem is.

由于您没有提供大量信息,我会尽力帮助您解决问题所在。

I think ZXing should be the right way to go. The barcode decoder searches for the barcode within an image per default. If you have an image wich only contains the barcode itself you can set the hint PURE_BARCODE to speed up decoding. There only could be problems if you have multiple barcodes within a single image.

我认为ZXing应该是正确的选择。条形码解码器默认搜索图像内的条形码。如果您的图像只包含条形码本身,您可以设置提示PURE_BARCODE以加快解码速度。如果您在单个图像中有多个条形码,则只会出现问题。

Your problem seems to be the uploaded image not ZXing. I would suggest checking whether the image is uploaded correctly.

你的问题似乎是上传的图像不是ZXing。我建议检查图像是否正确上传。

Here is an example of decoding a BufferedImage with any ZXing reader.

以下是使用任何ZXing阅读器解码BufferedImage的示例。

public void decodeCode() throws IOException, NotFoundException, FormatException, ChecksumException {
    BufferedImage image = ImageIO.read(yourImage);
    BinaryBitmap bitmap = convertImageToBinaryBitmap(image);
    Result result = reader.decode(bitmap, hints);

    assertNotNull("DecoderResult must not be null", result);
    System.out.println(result.getText());
  }

 protected BinaryBitmap convertImageToBinaryBitmap(BufferedImage image) {
    int[] pixels = image.getRGB(0, 0,
                                image.getWidth(), image.getHeight(),
                                null, 0, image.getWidth());
    RGBLuminanceSource source = new RGBLuminanceSource(image.getWidth(),
                                                       image.getHeight(),
                                                       pixels);
    return new BinaryBitmap(new HybridBinarizer(source));
  }