Click here to Skip to main content
14,768,146 members
Articles » Third Party Products » Product Showcase » General
Article
Posted 26 Jan 2016

Tagged as

Stats

17.8K views
1 bookmarked

How to Make PHP Barcode Reader with Windows COM Object?

26 Jan 2016CPOL
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:

Image 1

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:

Image 2

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:

Image 3

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

License

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

Share

About the Author

Dynamsoft
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/
Group type: Organisation

22 members


Comments and Discussions

 
-- There are no messages in this forum --