कोड में QR Code पढ़ना और डिकोड करना

Embed This Widget

Theme


      
    

Widget powered by . Free, no account required.

Programmatic decoding covers the cases a phone camera cannot: batch processing, automated workflows and custom scanning applications. In Python, pyzbar wraps the ZBar library and returns the payload, the symbol type and a bounding rectangle from a still image; combining it with OpenCV video capture turns the same call into a live camera loop. In the browser, jsQR decodes directly from Canvas image data, taking the raw pixel buffer together with its width and height. Batch work applies the same call across a directory, and that is where the difference from interactive scanning shows: throughput rather than latency governs the design.

QR Code Reading and Decoding in Code

Programmatic QR code reading enables batch processing, automated workflows, and custom scanning applications.

Python: pyzbar

from pyzbar.pyzbar import decode
from PIL import Image

image = Image.open("qrcode.png")
results = decode(image)
for result in results:
    print(result.data.decode("utf-8"))
    print(f"Type: {result.type}, Rect: {result.rect}")

pyzbar wraps the ZBar library — fast and reliable for static image decoding.

Python: OpenCV + pyzbar

For camera-based scanning:

import cv2
from pyzbar.pyzbar import decode

cap = cv2.VideoCapture(0)
while True:
    ret, frame = cap.read()
    codes = decode(frame)
    for code in codes:
        data = code.data.decode("utf-8")
        print(f"Decoded: {data}")

JavaScript: jsQR

Browser-based decoding from Canvas image data:

const imageData = context.getImageData(0, 0, width, height);
const code = jsQR(imageData.data, width, height);
if (code) {
    console.log("Decoded:", code.data);
}

Batch Processing

For processing thousands of QR code images:

import os
from pyzbar.pyzbar import decode
from PIL import Image

results = {}
for filename in os.listdir("qr_images/"):
    if filename.endswith(".png"):
        image = Image.open(f"qr_images/{filename}")
        codes = decode(image)
        results[filename] = [c.data.decode("utf-8") for c in codes]

Error Handling

QR code reading can fail for multiple reasons:

  • Image quality too low — increase resolution or preprocessing
  • QR code too small in the image — crop or zoom
  • Multiple QR codes in one image — most libraries return all found codes
  • Damaged or partially obscured — error correction limits apply

Preprocessing for Better Results

  • Convert to greyscale before decoding
  • Apply adaptive thresholding for uneven lighting
  • Increase contrast for faded or low-contrast codes
  • Sharpen blurry images before attempting decode

Key Takeaways

  • pyzbar (Python) wraps ZBar for fast, reliable static image decoding
  • OpenCV + pyzbar enables real-time camera-based scanning in Python
  • jsQR provides browser-based decoding from Canvas image data
  • Batch processing scripts can decode thousands of QR images
  • Image preprocessing (greyscale, contrast, sharpening) improves decode success

अक्सर पूछे जाने वाले प्रश्न

Which library decodes QR codes in Python?

pyzbar, which wraps the ZBar library and returns the payload, the symbol type and a bounding rectangle for every code found in an image. Paired with OpenCV video capture, the same call becomes a live camera loop rather than a still-image operation.

How is a QR code decoded inside a browser?

From Canvas pixel data. jsQR takes the raw buffer returned by getImageData together with its width and height, and returns the decoded string when a symbol is present, which keeps the entire operation client-side.

What changes when decoding thousands of images at once?

The design priority. Interactive scanning optimises for latency, whereas batch decoding optimises for throughput: the same per-image call runs across a directory, and the useful measures become images per second and the proportion that fail to decode at all.