PHP Classes

PHP Barcode Reader from Image: Read Barcode with ABBYY Cloud OCR service

Recommend this page to a friend!
  Info   View files Example   Demos   View files View files (4)   DownloadInstall with Composer Download .zip   Reputation   Support forum (4)   Blog    
Ratings Unique User Downloads Download Rankings
StarStarStarStar 70%Total: 746 All time: 4,488 This week: 117Up
Version License PHP version Categories
barcode-reader-php 1.0.1GNU General Publi...5.0PHP 5, Graphics, Web services
Description 

Author

This class can read barcode with ABBYY Cloud OCR service.

It can take a given barcode image and sends a HTTP request to the cloud OCR API Web server.

The class keeps polling the Web service every 5 seconds until the image is scanned and the barcode is decoded.

The class sets a variable with the processing results in XML.

Innovation Award
PHP Programming Innovation award nominee
February 2016
Number 3


Prize: One downloadable copy of PhpED Professional
Barcodes are used to identify products of many types. However, if you need that a Web application identifies a product from a scanned barcode image, you need to be able to process the image to recognize the barcode that it contains.

This class can decode barcode images by sending them to a Web service that can understand and decode those images.

Manuel Lemos
Picture of Tony L. Requena
  Performance   Level  
Name: Tony L. Requena <contact>
Classes: 3 packages by
Country: Spain Spain
Age: 42
All time rank: 182745 in Spain Spain
Week rank: 368 Up15 in Spain Spain Up
Innovation award
Innovation award
Nominee: 2x

Recommendations

Price checking availability using barcode scanner by mobile apps
Example source code in barcode scanner

Barcode reader
Barcode reader to use in my Web site

Example

<?php

/*------------------------------------------------------------------------------
** File: class.barcodereader.php
** Class: Barcode Reader PHP
** Description: Read Barcode with ABBYY Cloud OCR Service
** Version: 1.0.0
** Created: 11-Jan-2016
** Author: Tony L. Requena
** Homepage: www.phpmyipcam.com
**------------------------------------------------------------------------------
** COPYRIGHT (c) 2016 Tony L. Requena
**
** The source code included in this package is free software; you can
** redistribute it and/or modify it under the terms of the GNU General Public
** License as published by the Free Software Foundation. This license can be
** read at:
**
** http://www.opensource.org/licenses/gpl-license.php
**
** This program is distributed in the hope that it will be useful, but WITHOUT
** ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
** FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
**------------------------------------------------------------------------------
**
** Usage:
**
*/

class BarcodeReader
{
     private
$_appID;
     private
$_passApp;
     private
$_fileName;
     private
$_localImageDir = "images/";
     private
$_ResultXML;

     public function
setFileName($filename)
     {
          if (!
$filename) {
               die(
'ERROR: Please, set filename');
          }

          if (!
file_exists($this->_localImageDir.$filename)) {
               die(
'ERROR: Can\'t found the file on server.('.$this->_localImageDir.$filename.')');
          }
         
$this->_fileName = $filename;
     }
     public function
setPassword($password){
       
$this->_passApp = $password;
     }
     public function
setAppID($appID){
       
$this->_appID = $appID;
     }
     public function
getFileName()
     {
          return
$this->_fileName;
     }

     public function
Result(){
       
        return
$this->_ResultXML;
     }
     public function
Read()
     {
         
// 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 = $this->_appID;
         
$password = $this->_passApp;
         
$fileName = $this->_fileName;

         
////////////////////////////////////////////////////////////////
          // 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';


         
// 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);
              
$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);
         
$this->_ResultXML = $response;
         
curl_close($ch);
     }
}

/* USE */

$Barcode = new BarcodeReader();
$Barcode->setAppID("Barcode Reader PHP"); //use your appID
$Barcode->setPassword("xxxxxxxxxxxxxxxxxxxxxxxxx"); //use your password
$Barcode->setFileName('barcode1.jpg');
$Barcode->Read();
$Result = $Barcode->Result();

echo
"<img src='images/barcode1.jpg' /><br />";
echo
"Result: ".
var_dump($Result);


  DEMO_01External page  
  Files folder image Files  
File Role Description
Files folder imageimages (2 files)
Plain text file class.barcodereader.php Class Read Barcode with ABBYY Cloud OCR Service
Accessible without login Plain text file example.php Example Example of use

  Files folder image Files  /  images  
File Role Description
  Accessible without login Image file barcode1.jpg Data Image 1 test
  Accessible without login Image file barcode2.jpg Data Image 2 test

 Version Control Unique User Downloads Download Rankings  
 0%
Total:746
This week:0
All time:4,488
This week:117Up
User Ratings User Comments (1)
 All time
Utility:100%StarStarStarStarStarStar
Consistency:100%StarStarStarStarStarStar
Documentation:-
Examples:100%StarStarStarStarStarStar
Tests:-
Videos:-
Overall:70%StarStarStarStar
Rank:310