# Test QR Code API from CircuitDigest Cloud
#More info: https://circuitdigest.com/article/qr-code-scanner-api-for-low-power-embedded-soc-boards
!pip install requests
import requests
from google.colab import files
import json
# Function to read QR code from an image using the API
def read_qr_code(api_key, image_path):
# API details
url = "https://www.circuitdigest.cloud/readqrcode" # API endpoint
# Read the image file in binary mode
with open(image_path, "rb") as image_file:
# Prepare the payload
files = {
"imageFile": (f"{api_key}.jpg", image_file, "image/jpg")
}
headers = {
"Authorization": api_key # Add the API key for authorization
}
# Send the request
try:
response = requests.post(url, headers=headers, files=files)
# Check if request was successful
if response.status_code == 200:
print("Request successful!")
# Parse JSON response
response_data = response.json()
print(response_data)
# Extract QR code data and image link
qr_code_data = response_data.get("data", {}).get("QR_code", [])
image_link = response_data.get("data", {}).get("view_image", "")
print("QR Code Data:", qr_code_data)
print("Image Link:", image_link)
else:
print(f"Request failed with status code {response.status_code}")
print("Response:", response.text)
except requests.exceptions.RequestException as e:
print("Error connecting to the API:", e)
# User input for API key
api_key = input("Enter your API key from circuitdigest.cloud: ")
# Upload image file
uploaded = files.upload()
# Get the filename
for filename in uploaded.keys():
print(f"Uploaded image file (.jpg): {filename}")
image_path = filename
# Call the function to read QR code
read_qr_code(api_key, image_path)