Set Recognition Quality and Speed

When working with barcodes in applications, optimizing recognition quality and speed is essential. The RecognitionMode setting in the Aspose.BarCode.Cloud SDK for PHP offers a way to adjust these parameters, making it possible to prioritize either quality or speed during barcode recognition.

Recognition Mode Options

The RecognitionMode parameter can be set to the following values to control the recognition process:

  • Fast: Optimizes for speed but may lower the accuracy slightly. Minimal recognition timeout.
  • Normal: Balances speed and quality, suitable for most cases. Medium recognition timeout.
  • Excellent: Prioritizes accuracy over speed, ideal for challenging barcode images. Maximum recognition timeout.

Usage in API Requests

The RecognizeApi interface provides methods to recognize barcodes from files using different HTTP request types. Below are the available methods:

  • recognize: Recognizes barcode from a file on the server using a GET request.
  • recognizeBase64: Recognizes barcode from a file provided in the request body using a POST request with JSON or XML format.
  • recognizeMultipart: Recognizes barcode from a file provided in the request body using a POST request with multipart/form-data.

Example Implementations

Example 1: Using recognize

<?php

use Aspose\BarCode\Configuration;
use Aspose\BarCode\RecognizeApi;
use Aspose\BarCode\Model\{DecodeBarcodeType, RecognitionMode, RecognitionImageKind};
use Aspose\BarCode\Requests\RecognizeRequestWrapper;

require_once 'vendor/autoload.php';

function makeConfiguration()
{
    $config = new Configuration();

    $envToken = getenv('TEST_CONFIGURATION_ACCESS_TOKEN');
    if (empty($envToken)) {
        $config->setClientId("Client Id from https://dashboard.aspose.cloud/applications");
        $config->setClientSecret("Client Secret from https://dashboard.aspose.cloud/applications");
    } else {
        $config->setAccessToken($envToken);
    }

    return $config;
}

function main()
{
    $recognizeApi = new RecognizeApi(null, makeConfiguration());

    $request = new RecognizeRequestWrapper(DecodeBarcodeType::QR, "https://products.aspose.app/barcode/scan/img/how-to/scan/step2.png");
    $request->recognition_mode = RecognitionMode::Fast;
    $request->image_kind = RecognitionImageKind::Photo;

    $result = $recognizeApi->recognize($request);

    echo sprintf(
        "File '%s' recognized, result: '%s'\n",
        "https://products.aspose.app/barcode/scan/img/how-to/scan/step2.png",
        $result->getBarcodes()[0]->getBarcodeValue()
    );
}

main();

Example 2: Using recognizeBase64

<?php

use Aspose\BarCode\Configuration;
use Aspose\BarCode\RecognizeApi;
use Aspose\BarCode\Model\{DecodeBarcodeType, RecognizeBase64Request, RecognitionMode};
use Aspose\BarCode\Requests\RecognizeBase64RequestWrapper;

require_once 'vendor/autoload.php';

function makeConfiguration()
{
    $config = new Configuration();

    $envToken = getenv('TEST_CONFIGURATION_ACCESS_TOKEN');
    if (empty($envToken)) {
        $config->setClientId("Client Id from https://dashboard.aspose.cloud/applications");
        $config->setClientSecret("Client Secret from https://dashboard.aspose.cloud/applications");
    } else {
        $config->setAccessToken($envToken);
    }

    return $config;
}

function main()
{
    $recognizeApi = new RecognizeApi(null, makeConfiguration());

    $fileName = __DIR__ . '/../testdata/QR_and_Code128.png';
    $imageBytes = file_get_contents($fileName);
    $imageBase64 = base64_encode($imageBytes);

    $base64Request = new RecognizeBase64Request([
        'file_base64' => $imageBase64,
        'recognition_mode' => RecognitionMode::Excellent,
        'barcode_types' => [DecodeBarcodeType::Code128]
    ]);

    $request = new RecognizeBase64RequestWrapper($base64Request);

    $result = $recognizeApi->recognizeBase64($request);

    echo sprintf(
        "File '%s' recognized, result: '%s'\n",
        $fileName,
        $result->getBarcodes()[0]->getBarcodeValue()
    );
}

main();

Example 3: Using recognizeMultipart

<?php

use Aspose\BarCode\Configuration;
use Aspose\BarCode\RecognizeApi;
use Aspose\BarCode\Model\{DecodeBarcodeType, BarcodeResponseList, RecognitionMode, RecognitionImageKind};
use Aspose\BarCode\Requests\RecognizeMultipartRequestWrapper;

require_once 'vendor/autoload.php';

function makeConfiguration()
{
    $config = new Configuration();

    $envToken = getenv('TEST_CONFIGURATION_ACCESS_TOKEN');
    if (empty($envToken)) {
        $config->setClientId("Client Id from https://dashboard.aspose.cloud/applications");
        $config->setClientSecret("Client Secret from https://dashboard.aspose.cloud/applications");
    } else {
        $config->setAccessToken($envToken);
    }

    return $config;
}

function main()
{
    $recognizeApi = new RecognizeApi(null, makeConfiguration());

    $fileName = __DIR__ . '/../testdata/Aztec.png';
   $file = new SplFileObject($fileName, 'rb');
    
    $request = new RecognizeMultipartRequestWrapper(DecodeBarcodeType::Aztec, $file);
    $request->recognition_mode = RecognitionMode::Normal;
    $request->image_kind = RecognitionImageKind::ScannedDocument;

    $result = $recognizeApi->recognizeMultipart($request);
    
    echo sprintf(
        "File '%s' recognized, result: '%s'\n",
        $fileName,
        $result->getBarcodes()[0]->getBarcodeValue()
    );
}

main();

Conclusion

Choosing the appropriate RecognitionMode is essential in tailoring barcode recognition to the specific needs of your application. Whether prioritizing speed or accuracy, the examples provided demonstrate how to use the RecognizeApi interface to adjust settings for optimal performance. By fine-tuning RecognitionMode and RecognitionImageKind, you can enhance both efficiency and accuracy, making your barcode processing solution robust across various use cases.