Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

How to Make PHP Barcode Reader with Windows COM Object?

26 Jan 2016 1  
This article illustrates how to implement an online barcode reader using PHP and Windows Component Object Model (COM).

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

Dynamsoft Barcode Reader for Windows includes dynamic-link libraries for C/C++, DotNET and COM. If you want to use high-level programming languages such as Python, PHP, Java, Ruby and so on to create barcode reader apps with Dynamsoft Barcode Reader, you could either make a C/C++ wrapper as an extension or use COM objects. To create a PHP extension on Windows, you can refer to the sample code on Dynamsoft GitHub repository. Nevertheless, when making PHP extension, many developers suffered from the issue of PHP version number mismatch. Therefore, using COM in PHP seems to be more convenient.

Supported 1D/2D Barcode Types

  • Code 39, Code 93, Code 128, Codabar, Interleaved 2 of 5, EAN-8, EAN-13, UPC-A, UPC-E,Industrial 2 of 5
  • QRCode
  • DataMatrix
  • PDF417

Supported Image Types

  • BMP
  • JPEG
  • PNG
  • GIF
  • TIFF
  • PDF

Environment

  • Windows
  • PHP 5.5 and above

COM Registration

The installer of Dynamsoft Barcode Reader will automatically register COM DLLs. If you want to distribute the DLLs to other Windows PCs directly, do not forget to manually register them:

regsvr32 DynamsoftBarcodeReaderCtrlx86.dll
regsvr32 DynamsoftBarcodeReaderCtrlx64.dll

To unregister COMs:

regsvr32 /u DynamsoftBarcodeReaderCtrlx86.dll
regsvr32 /u DynamsoftBarcodeReaderCtrlx64.dll

File Uploading with JavaScript & PHP

Use the FormData interface to wrap selected file and send it using XMLHttpRequest.

var fd = new FormData();
var file = document.getElementById('fileToUpload').files[0];
fd.append('fileToUpload', file);
fd.append('uploadFlag', uploadFlag);
fd.append('barcodetype', getSelectedBarcodeTypes());
var xhr = new XMLHttpRequest();
xhr.addEventListener("load", uploadComplete, false);
xhr.upload.addEventListener("progress", uploadProgress, false);
xhr.addEventListener("error", uploadFailed, false);
xhr.addEventListener("abort", uploadCanceled, false);
xhr.open("POST", "readbarcode.php");
xhr.send(fd);

Save the temporary uploaded file to a folder:

// get current working directory
$root = getcwd();
// tmp dir for receiving uploaded barcode images
$tmpDir = $root . "/uploads/";
if (!file_exists($tmpDir)) {
	mkdir($tmpDir);
}
$target_file = $tmpDir . $tmpname;
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file))
{
	readBarcode($target_file, $btype);
	unlink($target_file);
}
else {
	echo "Fail to upload file.";
}

PHP Barcode Reader with COM Objects

Create COM objects in PHP:

$this->m_reader = new COM("DBRCtrl.BarcodeReader");
$this->m_option = new COM("DBRCtrl.ReaderOptions");

Initialize license:

$this->m_reader->InitLicense($key);

Set barcode formats and reader options:

$this->m_option->BarcodeFormats = $format;
$this->m_reader->ReaderOptions =$this->m_option;

Decode barcode image:

$this->m_reader->DecodeFile($filename);

Get results:

$this->m_resultArray = $this->m_reader->Barcodes;

Running PHP Barcode Reader on Nginx

First, you have to open php.ini to confirm whether the PHP extension%php%\ext\php_com_dotnet.dll is enabled:

extension=php_com_dotnet.dll

Open %nginx%\confg\ nginx.conf and uncomment the PHP configurations:

location ~ \.php$ {
            root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
            include        fastcgi_params;
        }

Specify the SCRIPT_FILENAME. For example:

fastcgi_param  SCRIPT_FILENAME %nginx%/html/$fastcgi_script_name;

Copy all PHP scripts to %nginx%/html/.

Run Nginx and PHP CGI:

%nginx%\nginx
%php%\php-cgi.exe -b 127.0.0.1:9000 -c %php%\php.ini

Visit localhost:8080/index.php and try to read a barcode image:

There is an error: 413 Request Entity Too Large! The size of the uploaded image is about 20M. You may also encounter the issue if uploading big files. To avoid the problem, you can change the default entity size in nginx.conf:

client_max_body_size 30M;

Try to read the barcode image again:

Still failed! According to the warning, you should also change the PHP limitation. Open php.ini and edit the following line:

post_max_size 30M;

Try one more time:

Now you can get your hands dirty. For more information about Dynamsoft Barcode Reader, please contact support@dynamsoft.com.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here