Click here to Skip to main content
15,867,308 members
Articles / Programming Languages / PHP
Article

Building Online Barcode Reader on Linux in PHP

14 Jun 2016CPOL2 min read 28.1K   1   1
Dynamsoft has released the Linux version of its barcode reader SDK, which supports programming in PHP. It is convenient to integrate Dynamsoft Barcode Reader to any server-side web applications.

This article is in the Product Showcase section for our sponsors at CodeProject. These articles are intended to provide you with information on products and services that we consider useful and of value to developers.

Introduction

Image 1

More and more people tend to embrace web and cloud services such as Google Cloud, Azure, and AWS, because no application download and installation is needed. A variety of software and functions can be accessed via web browsers. Assuming you want to build a web barcode reader as a service from scratch, Dynamsoft Barcode Reader SDK will expedite your development.

Linux Edition of Dynamsoft Barcode Reader

Supported Linux OS (64-bit):

  • Ubuntu 14.04.4+ LTS, CentOS 7+, Debian 8+

Supported Web Server

  • Nginx, Apache, etc.

Supported PHP version

  • 64-bit PHP 5.3, 5.4, 5.5, 5.6, both thread safe and non-thread safe are supported.

Supported 1D/2D Barcode Types

  • 1D: Code 39, Code 93, Code 128, Codabar, EAN-8, EAN-13, UPC-A, UPC-E, Interleaved 2 of 5 (ITF), Industrial 2 of 5 (Code 2 of 5 Industry, Standard 2 of 5, Code 2 of 5), ITF-14
  • 2D: QRCode, DataMatrix, and PDF417

Supported Image Types

  • BMP, JPEG, PNG, GIF, TIFF, and PDF

Detected Barcode Information

  • Barcode type
  • Barcode count
  • Barcode value as string
  • Barcode raw data as bytes
  • Barcode bounding rectangle
  • Coordinate of four corners
  • Page number

Features

  • Effective: reads multiple barcodes in one image.
  • Powerful: reads poor quality and damaged barcodes.
  • Flexible: detects barcode at any orientation and rotation angle.

Server-side PHP Barcode Reader

Dynamsoft Barcode Reader supports 64-bit PHP from version 5.3 to 5.6. Therefore, you need to first check the version of PHP installed on your system:

php -v

In addition to PHP version, you need to know whether the PHP is thread-safe or not:

php -i | grep Thread

Copy shared libraries of Dynamsoft Barcode Reader to /usr/lib:

sudo cp <Dynamsoft Barcode Reader>/lib/* /usr/lib

Edit php.ini to add the following extension path:

extension=<Dynamsoft Barcode
Reader>/php/extension/<version>/<ts|nts>/php_DynamsoftBarcodeReader.so

Upload barcode images to a web server with Form:

<form id="uploadForm" method="post" action="readbarcode.php" enctype="multipart/form-data">
    <input type="file" id="upLoadFile" name="upLoadFile" class="ImgLocalPath" />
    <input type="text" readonly="readonly" id="txtUploadFileName" class="radius3" />
    <input type="button" id="btnUploadFile" value="Browse..." class="radius3 ml20"/>
    <input type="submit" id="btnReadBarcode" class="radius3 left ml20"  value="Read Barcode" />

</form>

Detect barcode data on server-side:

PHP
<?php
include 'DynamsoftBarcodeReader.php';
ini_set('display_errors',1);
error_reporting(E_ALL);
$post_max_size = ini_get("post_max_size");
$maxsize = return_bytes($post_max_size);

if($_SERVER['CONTENT_LENGTH'] > $maxsize) {
         echo "Post data size is bigger than " . $post_max_size;
         exit;
}

$file = $_FILES["upLoadFile"]["tmp_name"];
if(!empty($file)){
         readBarcode($file);                                         
}
else {
         echo "Fail to upload file.";
}

function readBarcode($path) {
    try {       
        $br = new BarcodeReader();
    } catch (exception $exp) {       
        echo 'Your barcode reader component is not registered correctly. Please refer to ReadMe.txt for details.<br/>';
        exit;
    }

    $br->initLicense('693C401F1CC972A5018B729568B0CDD8');

    try {       
        $br->decodeFile($path);
    } catch(Exception $exp) {
        echo $br->getErrorString() . '<br/>';
        exit;
    }

    $cnt = $br->getBarcodesCount();
    if($cnt > 0) {
        echo 'Total barcode(s) found:' . $cnt . '.<br/>';
        for ($i = 0; $i < $cnt; $i++) {
            $result = $br->getBarcodeResult($i);
             echo ($i+1) . ': ';
             echo "$result->BarcodeFormatString, ";
             echo "$result->BarcodeText<br/>";
        }
    }
    else {
        echo 'No barcodes found.<br/>';
    }   
}
?>

How to Deploy the Sample Code to Apache on Ubuntu

Install php5-curl, apache2 and libapache2-mod-php5:

sudo apt-get install php5-curl apache2
libapache2-mod-php5

Extract the package and copy the project to /var/www/html/:

sudo cp -r DecodeLocalFile /var/www/html

Add extension path to /etc/php5/apache2/php.ini.

Start Apache service:

sudo service apache2 start

Visit http://localhost/DecodeLocalFile/index.php in Firefox or Chrome.

Online Demo

Visit PHP Barcode Reader to experience the online demo.

Get SDK and Samples

Do you want to build a server-side barcode reader in PHP on Linux? Get your hands dirty now:

Dynamsoft Barcode Reader 30-day Free Trial

Meanwhile, you can get lots of code samples from:

Dynamsoft Barcode Reader Demos

Beyond Desktop Barcode SDK

In addition to desktop platform – Windows, Linux and Mac OS X, Dynamsoft Barcode Reader SDK will also support Raspberry Pi, Android, and iOS in the near future. Please stay tuned to Dynamsoft Barcode Reader page and Dynamsoft labs. If you are interested in IoT and mobile SDKs, please contact support@dynamsoft.com to get preview versions.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Canada Canada
Dynamsoft has more than 15 years of experience in TWAIN SDKs, imaging SDKs and version control solutions.

Our products include:

TWAIN SDK
- Dynamic Web TWAIN: a TWAIN scanning SDK optimized for web document management applications.
- Dynamic .NET TWAIN: a .NET TWAIN and Directshow Image Capture SDK for WinForms/WPF applications.

Imaging SDKs
- Barcode Reader for Windows, Linux, macOS, iOS, Android and Raspberry Pi.
- OCR addon for both web and .NET TWAIN SDKs

Version Control
- SourceAnywhere: a SQL server-based source control solution. Both on-premise and hosting options are provided.

http://www.dynamsoft.com/
This is a Organisation

21 members

Comments and Discussions

 
QuestionNice Pin
PierreTessier14-Jun-16 5:19
PierreTessier14-Jun-16 5:19 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.