‚Äč

Cloud OCR SDK Documentation

How to create barcode reader in PHP with ABBYY Cloud OCR SDK

You may be developing a dynamic web page in PHP, which is intended for barcode reading. A visitor of this page should be able to read barcodes in different types of documents, or extract values of separate barcode fields. To develop PHP code for such page, you can use Web API provided by ABBYY Cloud OCR SDK.

ABBYY Cloud OCR SDK API is available for registered users only. If you want to register, fill in the registration form. Each registered user can create an Application ID and Application Password for accessing OCR server. The Application ID and Application Password are passed to the server with each request. See details in Authentication.

The procedure of barcode reading

  1. Cloud OCR SDK API provides several methods, which allow you to read barcodes. Select a suitable method depending on your task:
    • With the processImage or processDocument method you can recognize several barcodes of different types on one page. You even do not need to know where exactly on the page barcodes should be found. Everything you should do is to specify the barcodeRecognition profile as a parameter and select a suitable output format.
    • If you use the processBarcodeField method, you can recognize barcode in the specified location of the specified type.

    The How to Recognize Barcodes section can help you to decide on the parameters to be passed to the methods.

  2. When you select the processing method, just perform common processing steps. You can find more information on these steps in How to Work with Cloud OCR SDK.
    Call the selected method with necessary parameters. Do not forget to pass necessary authentication information with the request. The image file is transmited in the request body.
  3. The response of the method is in XML format. It contains the ID of the successfully created task.
  4. Control the status of the task using the getTaskStatus method. The result of recognition is available when the task has the Completed status. The reference to the output file is provided in the XML response.

Sample PHP code

Below you can find implementation of the procedure in PHP. The sample code extracts barcodes from an image and saves results in an XML file. The part of the code, which extracts barcode value from a single field, is a comment. Uncomment it, if necessary.

<?php
  // Enter your data here.
  // You need an Application ID and Application Password,
  // which can be created during registration.
  // If you are not registered yet, register
  // at http://cloud.ocrsdk.com/Account/Register 
  // Application ID and Application Password are passed
  // to Cloud OCR server with each request.
  $applicationId = 'my_application_id';
  $password = 'my_application_password';
  $fileName = 'myfile.jpg';

  ////////////////////////////////////////////////////////////////
  // 1.a Send an image with barcodes to Cloud OCR server 
  //     using processImage call 
  //     with barcodeRecognition profile as a parameter,
  //     or
  // 1.b Send an image of a barcode to Cloud OCR server 
  //     using processBarcodeField call.
  // 2.  Get response as XML.
  // 3.  Read taskId from XML.
  ////////////////////////////////////////////////////////////////

  // Get path to the file that you are going to process.
  $local_directory=dirname(__FILE__).'/images/';

  // Using the processImage method.
  // Use barcodeRecognition profile to extract barcode values.
  // Save results in XML (you can use any other available output format).
  // See details in API Reference.
  $url = 'http://cloud.ocrsdk.com/processImage?profile=barcodeRecognition&exportFormat=xml';

  // Using the processBarcodeField method.
  // Specify the region of a barcode (by default, the whole image is recognized), 
  // barcode type, and other parameters.
  // See details in API Reference.
  // $url = 'http://cloud.ocrsdk.com/processBarcodeField?region=0,0,100,100&barcodeType=pdf417';

  // Send HTTP POST request and get XML response.
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_USERPWD, "$applicationId:$password");
  curl_setopt($ch, CURLOPT_POST, 1);
  $post_array = array(
    "my_file"=>"@".$local_directory.'/'.$fileName,
  );
  curl_setopt($ch, CURLOPT_POSTFIELDS, $post_array); 
  $response = curl_exec($ch);
  curl_close($ch);

  // Parse XML response.
  $xml = simplexml_load_string($response);
  $arr = $xml->task[0]->attributes();

  // Task id.
  $taskid = $arr["id"]; 

  /////////////////////////////////////////////////////////////////
  // 4. Get task information in a loop until task processing finishes.
  // 5. If response contains "Completed" status, extract URL with result.
  // 6. Download recognition result.
  /////////////////////////////////////////////////////////////////

  $url = 'http://cloud.ocrsdk.com/getTaskStatus';
  $qry_str = "?taskid=$taskid";

  // Check task status in a loop until it is "Completed".
  do
  {
    sleep(5);
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url.$qry_str);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_USERPWD, "$applicationId:$password");
    $response = curl_exec($ch);
    curl_close($ch);

    // Parse XML.
    $xml = simplexml_load_string($response);
    $arr = $xml->task[0]->attributes();
  }
  while($arr["status"] != "Completed");

  // Result is ready. Download it.

  $url = $arr["resultUrl"]; 
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  $response = curl_exec($ch);
  curl_close($ch);

  // Parse output XML to extract barcode values.
  // Note that output XML files have different structure
  // depending on the method you used for processing.
?>

This PHP barcode reader sample is also available in the code samples provided with ABBYY Cloud OCR SDK.