Learning Objectives

In this tutorial, you’ll learn how to:

  • Generate barcodes using the Aspose.BarCode Cloud API without storing files
  • Acquire barcode images directly as stream objects
  • Save generated barcodes to your local environment
  • Customize basic barcode parameters

Prerequisites

Before starting this tutorial, make sure you have:

  • An Aspose Cloud account (sign up for a free trial if needed)
  • Your Client ID and Client Secret from the Aspose Cloud Dashboard
  • Basic knowledge of REST APIs
  • A development environment for your preferred language (C#, Java, PHP, Python, Node.js, or Go)

Introduction

The Aspose.BarCode Cloud API offers a streamlined way to generate barcodes without the need to use cloud storage operations. This approach is perfect for scenarios where you need to:

  • Quickly generate a barcode on demand
  • Avoid additional storage operations
  • Generate barcodes to be embedded directly in your application
  • Create one-time use barcodes

Practical Scenario

Imagine you’re developing an e-ticket system that needs to generate unique barcodes for each ticket purchase. You need these barcodes generated in real-time and don’t want to store each barcode image. This tutorial addresses exactly this scenario by showing how to:

  1. Make a direct API call to generate a barcode
  2. Receive the barcode as a stream
  3. Either display it immediately or save it locally

Step 1: Understanding the API Endpoint

The API we’ll use for generating barcodes without cloud storage is:

GET /barcode/generate

This endpoint allows you to generate a barcode and receive the image directly, without any intermediate storage on the cloud.

API Reference: GetBarcodeGenerate

Step 2: Authentication

Before making API calls, you need to authenticate and obtain an access token:

Try it yourself: Get an 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"

Replace YOUR_CLIENT_ID and YOUR_CLIENT_SECRET with your actual credentials. The response will contain an access token that you’ll use in subsequent API calls.

Step 3: Generate a Basic Barcode

Let’s start by generating a simple QR code barcode:

cURL Example

curl -v "https://api.aspose.cloud/v3.0/barcode/generate?text=HelloWorld&type=QR&format=png" \
     -X GET \
     -H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
     -H "Accept: multipart/form-data" \
     -o hello-world-qr.png

Try it yourself:

  1. Replace YOUR_ACCESS_TOKEN with the token you received in Step 2
  2. Run the command and check your local directory for the generated PNG file
  3. Try changing the text parameter to encode different information

What’s happening:

  • text=HelloWorld: The data to encode in the barcode
  • type=QR: The type of barcode to generate (QR Code in this case)
  • format=png: The image format for the barcode
  • -o hello-world-qr.png: Saves the response directly to a file

Step 4: Customizing Your Barcode

The API provides several parameters to customize your barcode generation:

cURL Example with Customization

curl -v "https://api.aspose.cloud/v3.0/barcode/generate?text=AsposeBarCode&type=Code128&format=png&enableChecksum=NO&resolution=300&dimensionX=1.4" \
     -X GET \
     -H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
     -H "Accept: multipart/form-data" \
     -o customized-barcode.png

Customization parameters:

  • resolution=300: Sets the resolution to 300 DPI for higher quality
  • dimensionX=1.4: Sets the barcode module width (the width of the narrowest bar)
  • enableChecksum=NO: Disables checksum for this barcode type

Learning checkpoint:

What would you change to:

  • Generate a Data Matrix barcode instead of Code128?
  • Save the output as a JPEG instead of PNG?
  • Generate a barcode with a resolution of 600 DPI?

Step 5: Implementing in Various Programming Languages

Let’s see how to implement barcode generation in different programming languages:

C# Example

// Tutorial Code Example: Generate barcode and save to local file in C#
using System;
using System.IO;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;

namespace AsposeBarcodeCloudTutorial
{
    class Program
    {
        // Replace with your actual credentials
        const string ClientId = "YOUR_CLIENT_ID";
        const string ClientSecret = "YOUR_CLIENT_SECRET";
        const string ApiBaseUrl = "https://api.aspose.cloud/v3.0/barcode/";
        const string AuthUrl = "https://api.aspose.cloud/oauth2/token";

        static async Task Main(string[] args)
        {
            // Step 1: Get the access token
            var accessToken = await GetAccessToken();
            
            // Step 2: Generate barcode and save to file
            await GenerateAndSaveBarcode(accessToken, "HelloFromCSharp", "QR", "qr-code.png");
            
            Console.WriteLine("Barcode generated and saved successfully!");
        }

        static async Task<string> GetAccessToken()
        {
            using (var client = new HttpClient())
            {
                // Prepare the form data for token request
                var formContent = new FormUrlEncodedContent(new[]
                {
                    new KeyValuePair<string, string>("grant_type", "client_credentials"),
                    new KeyValuePair<string, string>("client_id", ClientId),
                    new KeyValuePair<string, string>("client_secret", ClientSecret)
                });

                // Make the token request
                var response = await client.PostAsync(AuthUrl, formContent);
                response.EnsureSuccessStatusCode();
                
                // Parse the JSON response
                var jsonResponse = await response.Content.ReadAsStringAsync();
                // Simple parsing - in production, use proper JSON parsing
                var accessToken = jsonResponse.Split('"')[3];
                
                return accessToken;
            }
        }

        static async Task GenerateAndSaveBarcode(string accessToken, string text, string type, string outputFile)
        {
            using (var client = new HttpClient())
            {
                // Set authorization header
                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
                
                // Build the request URL with parameters
                string requestUrl = $"{ApiBaseUrl}generate?text={text}&type={type}&format=png";
                
                // Make the request
                var response = await client.GetAsync(requestUrl);
                response.EnsureSuccessStatusCode();
                
                // Save the barcode image to file
                using (var fileStream = File.Create(outputFile))
                {
                    await response.Content.CopyToAsync(fileStream);
                }
            }
        }
    }
}

Python Example

# Tutorial Code Example: Generate barcode and save to local file in Python
import requests
import os

# Replace with your actual credentials
client_id = "YOUR_CLIENT_ID"
client_secret = "YOUR_CLIENT_SECRET"
auth_url = "https://api.aspose.cloud/oauth2/token"
api_base_url = "https://api.aspose.cloud/v3.0/barcode/"

def get_access_token():
    """Get OAuth2 access token"""
    payload = {
        'grant_type': 'client_credentials',
        'client_id': client_id,
        'client_secret': client_secret
    }
    
    headers = {
        'Content-Type': 'application/x-www-form-urlencoded',
        'Accept': 'application/json'
    }
    
    response = requests.post(auth_url, data=payload, headers=headers)
    response.raise_for_status()  # Raise exception for HTTP errors
    
    return response.json()['access_token']

def generate_and_save_barcode(access_token, text, barcode_type, output_file):
    """Generate barcode and save to file"""
    # Build request URL with parameters
    request_url = f"{api_base_url}generate"
    
    params = {
        'text': text,
        'type': barcode_type,
        'format': 'png',
        'resolution': 300  # Optional: Set higher resolution
    }
    
    headers = {
        'Authorization': f'Bearer {access_token}',
        'Accept': 'multipart/form-data'
    }
    
    # Make request and save response to file
    response = requests.get(request_url, params=params, headers=headers)
    response.raise_for_status()  # Raise exception for HTTP errors
    
    # Save the image to file
    with open(output_file, 'wb') as f:
        f.write(response.content)

if __name__ == "__main__":
    # Get access token
    token = get_access_token()
    
    # Generate and save barcode
    generate_and_save_barcode(token, "HelloFromPython", "QR", "python-qr-code.png")
    
    print(f"Barcode generated and saved successfully!")

Step 6: Troubleshooting Common Issues

Issue: Authentication errors

  • Symptom: 401 Unauthorized response
  • Solution: Verify your Client ID and Client Secret, and make sure your access token hasn’t expired. Tokens typically expire after 1 hour.

Issue: Invalid barcode parameters

  • Symptom: 400 Bad Request response
  • Solution: Check that you’re using valid barcode type names and parameters. Reference the API documentation for supported values.

Issue: Image not displaying correctly

  • Symptom: Generated barcode image is corrupted or won’t scan
  • Solution: Try increasing the resolution parameter (e.g., resolution=300) or adjusting the dimensionX value for better quality.

What You’ve Learned

Congratulations! In this tutorial, you’ve learned how to:

  • Generate barcodes directly without using cloud storage
  • Customize basic barcode parameters like type, format, and resolution
  • Save the generated barcode to your local system
  • Implement the solution in different programming languages

Further Practice

To reinforce your learning:

  1. Try generating different barcode types (Code39, DataMatrix, PDF417)
  2. Experiment with different output formats (JPG, GIF, TIFF)
  3. Implement error handling in your code to manage API exceptions
  4. Create a simple web form that takes user input and generates a corresponding barcode

Next Steps

Now that you’ve mastered generating barcodes without cloud storage, you might want to learn how to use cloud storage for more complex scenarios:

Tutorial: Generate, Format and Manipulate Barcodes Using Cloud Storage

Helpful Resources

Have questions about this tutorial? Feel free to post them on our support forum.