0

Code:

import zxing
from PIL import Image

reader = zxing.BarCodeReader()
path = 'C:/Users/UI UX/Desktop/Uasa.png'
im = Image.open(path)

barcode = reader.decode(path)
print(barcode)

when i use code above work fine and return result: BarCode(raw='P<E....

i need to use this code:

import zxing
import cv2

reader = zxing.BarCodeReader()
path = 'C:/Users/UI UX/Desktop/Uasa.png'

img = cv2.imread (path)
cv2.imshow('img', img)
cv2.waitKey(0)

barcode = reader.decode(img)
print(barcode)

but this code return an error: TypeError: expected str, bytes or os.PathLike object, not numpy.ndarray

In another program i have image at base64 could help me somewhere here?

any body could help me with this?

4

3 回答 3

1

You cannot. if you look at the source code you will see that what it does is call a java app with the provided path (Specifically com.google.zxing.client.j2se.CommandLineRunner).

If you need to pre-process your image then you will have to save it somewhere and pass the path to it to your library

于 2021-01-19T11:49:39.763 回答
1

ZXing does not support passing an image directly as it is using an external application to process the barcode image. If you're not locked into using the ZXing library for decoding PDF417 barcodes you can take a look at the PyPI package pdf417decoder.

If you're starting with a Numpy array like in your example then you have to convert it to a PIL image first.

import cv2
import pdf417decoder
from PIL import Image

npimg = cv2.imread (path)
cv2.imshow('img', npimg)
cv2.waitKey(0)

img = Image.fromarray(npimg)
decoder = PDF417Decoder(img)

if (decoder.decode() > 0):
    print(decoder.barcode_data_index_to_string(0))
else:
    print("Failed to decode barcode.")
于 2021-04-05T16:49:54.300 回答
0

I fix this by:

path = os.getcwd()
# print(path)
writeStatus = cv2.imwrite(os.path.join(path, 'test.jpg'), pdf_image)
if writeStatus is True:
    print("image written")
else:
    print("problem")  # or raise exception, handle problem, etc.
sss = (os.path.join(path, 'test.jpg'))
# print(sss)
pp = sss.replace('\\', '/')
# print(pp)
reader = zxing.BarCodeReader()
barcode = reader.decode(pp)
于 2021-02-02T10:51:00.413 回答