在Linux上搭建PHP条形码阅读器

来源:互联网 发布:个人可以注册cn域名吗 编辑:程序博客网 时间:2024/06/05 17:25

无论你是一个开发者还是一个用户,你都或许听说过SaaS、Paas、laaS这些词。这是三个不同的云端模型。为什么越来越多的人都来拥抱类似于Google Cloud,Azure和AWS这样的云服务端?因为使用这些云服务器不需要下载应用,也不需要安装。大量的软件和应用已经可以通过B\S端实现。假设你现在想要搭建一个基于WEB的条形码阅读器,使用Dynamsoft Barcode Reader SDK可以加快你的开发进程。



下载SDK

Dynamsoft Barcode Reader 30-day Free Trial

条形码阅读器的PHP服务端

Dynamsoft Barcode Reader 支持 64-bit位的 PHP  5.3 到 5.6版本。 因此,首先你需要检查安装在你系统中的PHP版本:
1
php -v

在确认了PHP版本之后,你还需要知道你的PHP是否是线性安全的:
1
 php –i | grep Thread

复制Dynamsoft Barcode Reader库到/usr/lib:
1
sudocp /lib/*/usr/lib

编辑php.ini:
1
extension=/php/extension///php_DynamsoftBarcodeReader.so

通过表单上传条形码图片到服务器:
1<form id="uploadForm" method="post" action="readbarcode.php" enctype="multipart/form-data">
2    <input type="file" id="upLoadFile" name="upLoadFile" class="ImgLocalPath">
3    <input type="text" readonly="readonly" id="txtUploadFileName" class="radius3">
4    <input type="button" id="btnUploadFile" value="Browse..." class="radius3 ml20">
5    <input type="submit" id="btnReadBarcode" class="radius3 left ml20" value="Read Barcode">
6  
7</form>

在服务端检测条形码数据:
01include 'DynamsoftBarcodeReader.php';
02ini_set('display_errors',1);
03error_reporting(E_ALL);
04$post_max_size ini_get("post_max_size");
05$maxsize = return_bytes($post_max_size);
06  
07if($_SERVER['CONTENT_LENGTH'] > $maxsize) {
08         echo "Post data size is bigger than " $post_max_size;
09         exit;
10}
11  
12$file $_FILES["upLoadFile"]["tmp_name"];
13if(!empty($file)){
14         readBarcode($file);                                        
15}
16else {
17         echo "Fail to upload file.";
18}
19  
20function readBarcode($path) {
21    try {      
22        $br new BarcodeReader();
23    } catch (exception $exp) {      
24        echo 'Your barcode reader component is not registered correctly. Please refer to ReadMe.txt for details.<br>';
25        exit;
26    }
27  
28    $br->initLicense('693C401F1CC972A5018B729568B0CDD8');
29  
30    try {      
31        $br->decodeFile($path);
32    } catch(Exception $exp) {
33        echo $br->getErrorString() . '<br>';
34        exit;
35    }
36  
37    $cnt $br->getBarcodesCount();
38    if($cnt > 0) {
39        echo 'Total barcode(s) found:' $cnt '.<br>';
40        for ($i = 0; $i $cnt$i++) {
41            $result $br->getBarcodeResult($i);
42             echo ($i+1) . ': ';
43             echo "$result->BarcodeFormatString, ";
44             echo "$result->BarcodeText<br>";
45        }
46    }
47    else {
48        echo 'No barcodes found.<br>';
49    }  
50}

如何在Ubuntu的Apache上部署示例代码

安装php5-curl, apache2  libapache2-mod-php5:
1
sudoapt-get installphp5-curl apache2 libapache2-mod-php5

提取代码文件复制到 /var/www/html/:
1
sudocp -r DecodeLocalFile /var/www/html

增加扩展路径到 /etc/php5/apache2/php.ini.

开启Apache服务:
1sudo service apache2 start
在浏览器访问http://localhost/DecodeLocalFile/index.php

Demo和源码

点击PHP Barcode Reader 查看demo.
点击这里下载源码。

来自:PHPChina
0 0
原创粉丝点击