How can i pass image itself (np.array) not path of it to zxing library for decode pdf417

1.2k Views Asked by At

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

There are 4 best solutions below

0
Brett Allen On BEST ANSWER

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.")
3
Nullman On

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

0
Abdullah Md On

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)
0
yushulx On

The zxing package is not recommended. It is just a command line tool to invoke Java ZXing libraries.

You should use zxing-cpp, which is a Python module built with ZXing C++ code. Here is the sample code:

import cv2
import zxingcpp

img = cv2.imread('myimage.png')
results = zxingcpp.read_barcodes(img)
for result in results:
    print("Found barcode:\n Text:    '{}'\n Format:   {}\n Position: {}"
        .format(result.text, result.format, result.position))
if len(results) == 0:
    print("Could not find any barcode.")