Note - Double Click to Copy Code Contact Us!
Posts

Bar and QR Code Reader using Python

Tech Doubility

 Bar and QR Code Reader using Python 



To create a bar and QR code reader using Python, we'll use the pyzbar library for decoding barcodes and QR codes and the opencv-python library for capturing and processing images.F

irst, you need to install the required libraries. Open your terminal or command prompt and run the following commands:

  1. pip install pyzbar
  2. pip install opencv-python
Once the libraries are installed, you can use the following code:
  1. import cv2
  2. from pyzbar import pyzbar
  3.  
  4. def read_barcodes(image):
  5. # Convert the image to grayscale
  6. gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
  7.  
  8. # Use the pyzbar library to decode the barcodes
  9. barcodes = pyzbar.decode(gray)
  10.  
  11. for barcode in barcodes:
  12. # Extract the barcode's bounding box coordinates and draw a rectangle around it
  13. (x, y, w, h) = barcode.rect
  14. cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2)
  15.  
  16. # Extract the barcode data and type from the barcode object
  17. barcode_data = barcode.data.decode("utf-8")
  18. barcode_type = barcode.type
  19.  
  20. # Display the barcode data and type on the image
  21. text = "{} ({})".format(barcode_data, barcode_type)
  22. cv2.putText(image, text, (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
  23.  
  24. # Print the barcode data and type
  25. print("Found {} barcode: {}".format(barcode_type, barcode_data))
  26.  
  27. # Display the image
  28. cv2.imshow("Barcode Reader", image)
  29. cv2.waitKey(0)
  30.  
  31. # Open the camera for video capture
  32. cap = cv2.VideoCapture(0)
  33.  
  34. while True:
  35. # Read a frame from the camera
  36. _, frame = cap.read()
  37.  
  38. # Call the function to read barcodes in the frame
  39. read_barcodes(frame)
  40.  
  41. # Break the loop if the 'q' key is pressed
  42. if cv2.waitKey(1) & 0xFF == ord('q'):
  43. break
  44.  
  45. # Release the camera
  46. cap.release()
  47.  
  48. # Close all OpenCV windows
  49. cv2.destroyAllWindows()
  50.  
Save the code in a Python file (e.g., barcode_reader.py), and then run it. It will open your computer's camera and start reading barcodes and QR codes in real-time. When a barcode or QR code is detected, it will draw a rectangle around it and display the decoded information on the image.

To stop the program, press the 'q' key.

Please note that you'll need a webcam or a camera connected to your computer to run this code.

Post a Comment

Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.
Site is Blocked
Sorry! This site is not available in your country.