Candybar Barcode API

GRATIS
Da wildidea | Aggiornamento 2ใƒถๆœˆๅ‰ | Tools
Popolaritร 

7.9 / 10

Latenza

126ms

Livello di servizio

100%

Health Check

N/A

Torna a tutti i tutorial (1)

Decode barcodes in an image

Candybar can decode a collection of barcodes in a single image file. Code39, Code128, and QR Code decoding is supported. In this example we use the Python requests library to call Candybar and retrieve the translation of barcodes in an image.

import requests

def decode_barcode(api_key, image_file):
    url = "https://"+api_host+"/v1/decode"  

    headers = {
        "X-RapidAPI-Key": api_key,
        "X-RapidAPI-Host": api_host,
    }

    files = {
        "file": open(image_file, "rb")
    }

    response = requests.post(url, headers=headers, files=files)
    data = response.json()

    if response.status_code == 200:
        # Successful response
        for decode in data:
            decoded_data = decode["contents"]
            barcode_type = decode["code_type"]
            print(f"Decoded Barcode Type: {barcode_type}")
            print(f"Decoded Data: {decoded_data}")
    else:
        # Error handling
        print("Failed to decode the barcode. Error message:")
        print(data)

if __name__ == "__main__":
    # Replace 'YOUR_RAPID_API_KEY' with your actual API key and CANDYBAR_RAPID_HOST with the current candybar host value
    api_key = "YOUR_RAPID_API_KEY"
    api_host = "CANDYBAR_RAPID_HOST"

    image_file = "barcode_image.jpg"  # Replace with your barcode image file

    decode_barcode(api_key, image_file)