Cordova(PhoneGap)基于android平台的二维码处理

来源:互联网 发布:像素设计软件 编辑:程序博客网 时间:2024/05/29 08:28

最近公司要求用cordova插件处理二维码(基于android平台),查阅了各种资料,简单实现了效果,现将其记录下来,方便以后有个引导。

至于cordova的安装需要借助于npm包管理工具,可自行Google


第一步:cmd进入命令行


第二步:进入项目目录,依次执行下面语句(前提在cordova环境配置好之后)

  •       cordova create MyBarcode com.example.cordova  MyCode (其中MyBarCode是你新建项目的名称,MyCode是你App名字) 
  •       cd MyBarcode
  •       cordova platform add android
  •       cordova plugin add cordova-plugin-barcodescanner(有些博客写的是cordova plugin add https://github.com/wildabeast/BarcodeScanner.git)


第三步:导入工程,其中有很多坑,直接导入并不能运行,正确的导入方法是打开D:\MyBarcode\platforms\android的build.gradle文件(本人用的是Android Studio)。


第四步:进入assets/www/index.html文件,注释掉原有代码并添加以下代码


<html><head>    <script type="text/javascript" src="cordova.js"></script>    <script type="text/javascript">        function scannerCode(){         //扫码的js接口代码            cordova.plugins.barcodeScanner.scan(                function (result) {                   document.getElementById("test").innerHTML = "结果: " + result.text;                },                function (error) {                   alert("扫描失败: " + error);                });         }         //生成二维码的js接口代码         function encodeCode(){            cordova.plugins.barcodeScanner.encode(               "TEXT_TYPE",   //编码的类型 文本型、email、sms、手机号               "http://www.baidu.com",   //url               function(success) {                   //实现功能逻辑                   alert("encode success: " + success);  //生成之后会默认作为图片显示出来               },               function(fail) {                   alert("encoding failed: " + fail);                });         }    </script></head><body><center>    <button style="margin-top:80px; width=100px; height:40px" onclick="scannerCode()">扫描二维码</button>    <button style=" width=100px; height:40px" onclick="encodeCode()">生成二维码</button>    <div style="margin-top:50px" id="test">扫描结果</div></center></body></html>



好了,大功告成,貌似可以扫描了,但你会发现他是横屏的,好像达不到预期效果,不用急,慢慢看下面操作。 

下载 zxing_4.1.8.jar (http://download.csdn.net/detail/cmh1748218486/9792791)这个jar包,替换掉之前D:\MyBarcode\platforms\android\libs目录下的com.google.zxing.client.android.captureactivity.jar文件,刷新即可,

在运行怎么感觉是骗人的,图像都扭曲了,因为还差一步,我们需要在AndroidMainifest.xml文件中把android:screenOrientation="landscape"改为android:screenOrientation="portrait"。在运行,嗯非常完美。。。


特别声明:zxing_4.1.8.jar借助于别人的改版,并非原创。



1 0
原创粉丝点击