从java上传的图像中读取条形码
问题描述:
我必须从上传的图像文件中读取基于java的web应用程序的条形码。我已经尝试过Zxing和其他类似的库,但只有在我们上传精确图像时才能使用。任何人都可以建议从图像读取条码的api?该图像可以是随机点击的图像,也可能包含其他数据。由此,我们必须识别条码并对其进行解码。从java上传的图像中读取条形码
答
由于您没有提供大量信息,我会尽力帮助您解决问题。
我觉得ZXing应该是正确的路要走。条形码解码器在默认情况下搜索图像中的条形码。如果您的图像仅包含条形码本身,您可以设置提示PURE_BARCODE以加速解码。如果您在单个图像中有多个条形码,则只会出现问题。
你的问题似乎是上传的图像不是ZXing。我会建议检查图像是否正确上传。
以下是使用任何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));
}