Search code examples
pythonbarcode

Read data from a barcode reade using Python in Linux Ubuntu


I need just to read data from a barcode plugged in usb port. I am using Linux Ubuntu 24.04 with Python but I have no idea how to do this. I read a lot of docs but no one is straight forward enough or doesn't work for my application.

I will be very grateful for help.


Solution

  • I do not know what barcode you mean but I will try to help. To read a barcode as shown below:

    Barcode Image

    you can use many libraries to do that:

    You can refer to this linked article:

    Recommended Package

    zxing-cpp is the best Python package for reading barcodes.

    the article advises not to use pure Python to read barcodes

    Don’t use Python for Barcode Decoding!

    You really shouldn’t be using pure Python to decode barcodes from images or videos or, worse, from camera streams.

    and, the article talks about a lot of libraries and recommends using zxing-cpp

    You can install it using pip

    pip install zxing-cpp

    You should also install openCV

    pip install opencv-python

    after that, you can use code similar to following to read a barcode:

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

    the output:

    Found barcode:
     Text:    "1234567890128"
     Format:   BarcodeFormat.EAN13
     Content:  ContentType.Text
     Position: 76x11 446x11 446x188 76x188