========
Examples
========

Capturing video with OpenCV and using QRDecode to decode the QRs present each frame
===================================================================================

Here you will find some code examples that will guide you through the process of using the qrdecode package.

.. code-block:: python

   import cv2
   from qrdecode import Scanner, QRProcessor
   cap = cv2.VideoCapture(0)
   
   while True:
       ret, frame = cap.read()
       if frame is not None:
           scanner = Scanner(frame,
                          threshold="gaussian",
                          output_size=600,
                          output_border=60)

           cv2.imshow("Input", scanner.image)

           try:
               code = scanner.scan()[0]
               assert isinstance(code, QRProcessor)
               cv2.imshow("QR", code.image)
               code.sample(correct_image=True)

               n, m, _ = code.colors.shape

               cv2.imshow("QR trimmed", cv2.resize(code.colors, (5*n, 5*m)))

           except Exception as e:
               print(e)

       if cv2.waitKey(1) & 0xFF == ord('q'):
           break

   cap.release()
   cv2.destroyAllWindows()

Reading the QR codes of a single image
======================================

.. code-block:: python
   
   FILE_PATH = os.path.dirname(os.path.abspath(__file__))
   scanner = Scanner(os.path.join(FILE_PATH, "../", "scripts/images/test_qr.png"),
                     threshold="simple",
                     output_size=1000,
                     output_border=100)

   for code in scanner.scan():
       assert isinstance(code, QRProcessor)
       code.sample(correct_image=True)
       data = code.decode()

And that is how you read all the QR codes in a single image.


