Tutorial: How to Recognize Barcodes Using Cloud Storage

Learning Objectives

In this tutorial, you’ll learn:

  • How to upload barcode images to Aspose Cloud Storage
  • Methods to recognize barcodes from images stored in cloud storage
  • How to read barcodes from specific regions of an image
  • Techniques to handle and process recognition results

Prerequisites

Before starting this tutorial, make sure you have:

  • Completed the Barcode Recognition Basics tutorial
  • An active Aspose Cloud account with Client ID and Client Secret
  • A development environment with one of the supported languages (C#, Java, PHP, Python, Node.js, or Go)
  • Sample barcode images for testing

Understanding Barcode Recognition with Cloud Storage

Aspose.BarCode Cloud provides an efficient way to recognize barcodes from images stored in your Aspose Cloud Storage. This approach is particularly useful when:

  • You need to process numerous barcode images
  • You want to maintain a central repository of your barcode images
  • You’re building applications that require cloud-based processing

Tutorial Scenario

In this tutorial, we’ll build a barcode recognition system that can:

  1. Upload barcode images to Aspose Cloud Storage
  2. Read barcodes from these stored images
  3. Process barcodes from specific regions within images

Step 1: Upload Images to Cloud Storage

Before we can recognize barcodes, we need to upload our images to Aspose Cloud Storage.

Using cURL

# First get Access Token
curl -v "https://api.aspose.cloud/oauth2/token" \
     -X POST \
     -d "grant_type=client_credentials&client_id=YOUR_CLIENT_ID&client_secret=YOUR_CLIENT_SECRET" \
     -H "Content-Type: application/x-www-form-urlencoded" \
     -H "Accept: application/json"

# Upload an image to cloud storage
curl -v "https://api.aspose.cloud/v3.0/barcode/storage/file/sample-barcode.png" \
     -X PUT \
     -H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
     -H "Content-Type: application/octet-stream" \
     --data-binary @/path/to/your/sample-barcode.png

Using SDK (Python Example)

# Tutorial Code Example: Uploading barcode images to cloud storage
import aspose_barcode_cloud
from aspose_barcode_cloud.apis.barcode_api import BarcodeApi
from aspose_barcode_cloud.apis.storage_api import StorageApi
from aspose_barcode_cloud.api_client import ApiClient
from aspose_barcode_cloud.configuration import Configuration

# Configure API credentials
configuration = Configuration(
    client_id="YOUR_CLIENT_ID",
    client_secret="YOUR_CLIENT_SECRET"
)

# Create API client
api_client = ApiClient(configuration)
storage_api = StorageApi(api_client)

# Specify the file to upload
local_file_path = "/path/to/your/sample-barcode.png"
remote_file_name = "sample-barcode.png"
remote_folder = None  # Use root folder

# Upload file to cloud storage
with open(local_file_path, 'rb') as file:
    result = storage_api.upload_file(
        path=remote_file_name,
        file=file,
        storage_name=None  # Use default storage
    )
    
    if result.uploaded:
        print(f"File {remote_file_name} uploaded successfully!")
    else:
        print("File upload failed")

Step 2: Recognize Barcodes from Cloud Storage

Now that our images are in cloud storage, let’s recognize barcodes from them.

Using cURL

# Recognize barcode from an image in cloud storage
curl -v "https://api.aspose.cloud/v3.0/barcode/sample-barcode.png/recognize?type=Code128&format=png" \
     -X GET \
     -H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
     -H "Content-Type: application/json" \
     -H "Accept: application/json"

Using SDK (C# Example)

// Tutorial Code Example: Reading barcodes from cloud storage images
using Aspose.BarCode.Cloud.Sdk.Api;
using Aspose.BarCode.Cloud.Sdk.Client;
using Aspose.BarCode.Cloud.Sdk.Model;
using System;

// Configure API credentials
var config = new Configuration
{
    ClientId = "YOUR_CLIENT_ID",
    ClientSecret = "YOUR_CLIENT_SECRET"
};

// Create API client
var apiClient = new ApiClient(config);
var barcodeApi = new BarcodeApi(apiClient);

// Specify parameters
string imageName = "sample-barcode.png";  // Image name in cloud storage
string barcodeType = "Code128";  // Type of barcode to recognize
string format = "png";           // Image format
string folder = null;            // Root folder

try
{
    // Perform recognition
    var response = barcodeApi.GetBarcodeRecognize(
        name: imageName,
        type: barcodeType,
        format: format,
        folder: folder
    );

    // Process results
    Console.WriteLine($"Recognition Status: {response.Status}");
    Console.WriteLine($"Number of barcodes found: {response.Barcodes.Count}");

    foreach (var barcode in response.Barcodes)
    {
        Console.WriteLine($"\nBarcode Value: {barcode.BarcodeValue}");
        Console.WriteLine($"Barcode Type: {barcode.BarcodeType}");
        if (barcode.Region != null)
        {
            Console.WriteLine($"Region: {string.Join(", ", barcode.Region)}");
        }
        if (!string.IsNullOrEmpty(barcode.Checksum))
        {
            Console.WriteLine($"Checksum: {barcode.Checksum}");
        }
    }
}
catch (Exception ex)
{
    Console.WriteLine($"Error: {ex.Message}");
}

Try It Yourself!

Upload one of your barcode images to Aspose Cloud Storage and try to recognize it using the code examples above. Modify the parameters to match your image name and expected barcode type.

Step 3: Reading Barcodes from Specific Regions

If you know the approximate location of barcodes in your images, you can optimize recognition by specifying target regions.

Using SDK (Java Example)

// Tutorial Code Example: Reading barcodes from specific regions
import com.aspose.barcode.cloud.api.BarcodeApi;
import com.aspose.barcode.cloud.client.ApiClient;
import com.aspose.barcode.cloud.model.BarcodeResponseList;

// Configure API credentials
ApiClient apiClient = new ApiClient("YOUR_CLIENT_ID", "YOUR_CLIENT_SECRET");
BarcodeApi barcodeApi = new BarcodeApi(apiClient);

try {
    // Specify parameters
    String imageName = "sample-multiple-barcodes.png";
    String barcodeType = "Code128,QR,DataMatrix"; // Multiple barcode types to check
    String format = "png";
    String folder = null; // Root folder
    
    // Define the region coordinates (x, y, width, height)
    Integer rectX = 50;
    Integer rectY = 100;
    Integer rectWidth = 300;
    Integer rectHeight = 200;

    // Perform recognition with target region
    BarcodeResponseList response = barcodeApi.getBarcodeRecognize(
        imageName,
        barcodeType,
        null, // checksum validation
        null, // preset
        rectX,
        rectY,
        rectWidth,
        rectHeight,
        null, // strip FNC
        null, // timeout
        null, // medianSmoothingWindowSize
        null, // allow detect color inverted
        null, // allow data matrix industrial barcodes
        null, // allow QR micro QR
        null, // allow one d wiped bars
        null, // allow one d fast barcodes
        null, // australian post encoding table
        null, // ignore ending filling patterns for C table
        format,
        folder
    );

    // Process results
    System.out.println("Recognition Status: " + response.getStatus());
    System.out.println("Number of barcodes found: " + response.getBarcodes().size());
    
    response.getBarcodes().forEach(barcode -> {
        System.out.println("\nBarcode Value: " + barcode.getBarcodeValue());
        System.out.println("Barcode Type: " + barcode.getBarcodeType());
        if (barcode.getRegion() != null) {
            System.out.println("Region: " + String.join(", ", barcode.getRegion()));
        }
    });
} catch (Exception e) {
    System.err.println("Error: " + e.getMessage());
}

Step 4: Handling Recognition Results

The barcode recognition results include valuable information such as barcode value, type, region coordinates, and in some cases, checksum information. Let’s explore how to work with these results.

Using SDK (PHP Example)

<?php
// Tutorial Code Example: Processing barcode recognition results

require_once(__DIR__ . '/vendor/autoload.php');

use Aspose\BarCode\Cloud\Api\BarcodeApi;
use Aspose\BarCode\Cloud\Api\Configuration;
use GuzzleHttp\Client;

// Configure API credentials
$config = new Configuration();
$config->setClientId('YOUR_CLIENT_ID');
$config->setClientSecret('YOUR_CLIENT_SECRET');

// Create API client
$apiClient = new Client();
$barcodeApi = new BarcodeApi($apiClient, $config);

try {
    // Specify parameters
    $imageName = 'sample-barcode.png';
    $barcodeType = 'Code128';
    $format = 'png';
    $folder = null; // Root folder
    
    // Perform recognition
    $response = $barcodeApi->getBarcodeRecognize(
        $imageName,
        $barcodeType,
        null, // checksumValidation
        null, // preset
        null, // rectX
        null, // rectY
        null, // rectWidth
        null, // rectHeight
        null, // stripFnc
        null, // timeout
        null, // medianSmoothingWindowSize
        null, // allowDetectColorInverted
        null, // allowDataMatrixIndustrialBarcodes
        null, // allowQRMicroQR
        null, // allowOneDWipedBars
        null, // allowOneDFastBarcodes
        null, // australianPostEncodingTable
        null, // ignoreEndingFillingPatternsForCTable
        $format,
        $folder
    );
    
    // Process results
    echo "Recognition Status: " . $response->getStatus() . "\n";
    echo "Number of barcodes found: " . count($response->getBarcodes()) . "\n";
    
    foreach ($response->getBarcodes() as $barcode) {
        echo "\nBarcode Value: " . $barcode->getBarcodeValue() . "\n";
        echo "Barcode Type: " . $barcode->getBarcodeType() . "\n";
        
        if ($barcode->getRegion() !== null) {
            echo "Region: " . implode(", ", $barcode->getRegion()) . "\n";
        }
        
        if ($barcode->getChecksum() !== null && $barcode->getChecksum() !== '') {
            echo "Checksum: " . $barcode->getChecksum() . "\n";
        }
    }
} catch (Exception $e) {
    echo "Error: " . $e->getMessage() . "\n";
}

Try It Yourself!

Upload some barcode images to your Aspose Cloud Storage and try to process the recognition results using the code example above. Pay attention to the different properties available in the results and how they can be used in your application.

Step 5: Handling Multiple Barcodes in an Image

When working with images that contain multiple barcodes, you can leverage the same API to detect and recognize all of them at once.

Using SDK (Node.js Example)

// Tutorial Code Example: Handling multiple barcodes in one image
const { Configuration, BarcodeApi } = require('aspose-barcode-cloud');

// Configure API credentials
const config = new Configuration({
    clientId: "YOUR_CLIENT_ID",
    clientSecret: "YOUR_CLIENT_SECRET"
});

// Create API client
const barcodeApi = new BarcodeApi(config);

// Specify parameters
const imageName = "multiple_barcodes.png";
const barcodeType = "ALL_SUPPORTED_TYPES"; // Scan for all barcode types
const format = "png";
const folder = null; // Root folder

// Perform recognition
barcodeApi.getBarcodeRecognize(
    imageName,
    barcodeType,
    null, // checksum validation
    null, // preset
    null, // rectX
    null, // rectY
    null, // rectWidth
    null, // rectHeight
    null, // strip FNC
    null, // timeout
    null, // medianSmoothingWindowSize
    null, // allow detect color inverted
    null, // allow data matrix industrial barcodes
    null, // allow QR micro QR
    null, // allow one d wiped bars
    null, // allow one d fast barcodes
    null, // australian post encoding table
    null, // ignore ending filling patterns for C table
    format,
    folder
).then((response) => {
    console.log(`Recognition Status: ${response.status}`);
    console.log(`Total barcodes found: ${response.barcodes.length}`);
    
    // Process each barcode
    response.barcodes.forEach((barcode, index) => {
        console.log(`\nBarcode ${index + 1}:`);
        console.log(`  Value: ${barcode.barcodeValue}`);
        console.log(`  Type: ${barcode.barcodeType}`);
        
        if (barcode.region) {
            console.log(`  Region: ${barcode.region.join(', ')}`);
        }
        
        if (barcode.checksum) {
            console.log(`  Checksum: ${barcode.checksum}`);
        }
    });
}).catch(error => {
    console.error("Error:", error);
});

Troubleshooting Common Issues

Issue: File Not Found in Cloud Storage

If you get a “File not found” error:

  • Verify that you’ve correctly uploaded the image to your cloud storage
  • Check the path and filename (they are case-sensitive)
  • Ensure you’re using the correct storage name if you have multiple storages

Issue: Barcode Not Recognized

If barcodes aren’t recognized correctly:

  • Try specifying the exact barcode type if you know it
  • Ensure the image has sufficient quality and resolution
  • Check if the barcode is in a valid format
  • Try using a different region if you’re using the region parameter

Issue: Authentication Errors

If you encounter authentication errors:

  • Double-check your Client ID and Client Secret
  • Ensure your access token hasn’t expired
  • Verify that your Aspose Cloud subscription is active

What You’ve Learned

In this tutorial, you’ve learned:

  • How to upload barcode images to Aspose Cloud Storage
  • Methods to recognize barcodes from images stored in cloud storage
  • Techniques to read barcodes from specific regions within images
  • Ways to process recognition results and extract valuable information
  • Approaches for handling multiple barcodes in a single image

Next Steps

Ready to explore more ways to recognize barcodes? Check out the next tutorial in this series:

Tutorial: Recognize Barcodes Without Cloud Storage

Further Practice

To reinforce what you’ve learned, try these exercises:

  1. Create a program that reads barcodes from multiple images stored in cloud storage
  2. Build a solution that recognizes specific barcode types from defined regions of images
  3. Develop a utility that analyzes and reports on the quality of recognized barcodes

Helpful Resources

Feedback

Did you find this tutorial helpful? Do you have questions about recognizing barcodes from cloud storage? We’d love to hear from you! Please leave your questions and feedback in our support forum.