5r1m-phonegap-plugin-barcodescanner 中文文档教程

发布于 4年前 浏览 62 项目主页 更新于 3年前

# PhoneGap Plugin BarcodeScanner

Build Status

用于 Cordova / PhoneGap 的跨平台 BarcodeScanner。

遵循 Cordova 插件规范,以便它与 Plugman

Installation

这需要 phonegap 7.1.0+(当前稳定 v8.0.0)

phonegap plugin add phonegap-plugin-barcodescanner

也可以直接通过 repo url 安装(不稳定)

phonegap plugin add https://github.com/phonegap/phonegap-plugin-barcodescanner.git

可选变量: 此插件需要 Android 支持库 v4。 最低版本为 24.1.0。 默认值为 27.+。 在此处查看最新版本。

phonegap plugin add phonegap-plugin-barcodescanner --variable ANDROID_SUPPORT_V4_VERSION="27.1.1"

Supported Platforms

  • Android
  • iOS
  • Windows (Windows/Windows Phone 8.1 and Windows 10)
  • Browser

注意:该项目的 Android 源代码包括一个 Android 库项目。 plugman 目前不支持 Library Project refs,所以它一直 预构建为 jar 库。 图书馆计划的任何更新都应该 使用更新的 jar 提交。

注意:无法为 AnyCPU 架构构建 Windows 10 应用程序,这是 Windows 平台的默认架构。 如果您想构建/运行 Windows 10 应用程序,您应该明确指定目标体系结构,例如(Cordova CLI):

cordova run windows -- --archs=x86

PhoneGap Build Usage

将以下内容添加到您的 config.xml:

<!-- add a version here, otherwise PGB will use whatever the latest version of the package on npm is -->
<plugin name="phonegap-plugin-barcodescanner" />

如果您使用的 cordova-android 版本为 4,则在 PhoneGap Build 上或更少,确保您使用 gradle 构建:

<preference name="android-build-tool" value="gradle" />

Using the plugin

插件使用方法 scan(success, fail) 创建对象 cordova.plugins.barcodeScanner

目前支持以下条码类型:

Barcode TypeAndroidiOSWindows
QR_CODE
DATA_MATRIX
UPC_A
UPC_E
EAN_8
EAN_13
CODE_39
CODE_93
CODE_128
CODABAR
ITF
RSS14
PDF_417
RSS_EXPANDED
MSI
AZTEC

successfail是回调函数。 成功传递了一个具有数据、类型和取消属性的​​对象。 数据是条码数据的文本表示,类型是检测到的条码类型,取消是用户是否取消扫描。

一个完整的示例可能是:

   cordova.plugins.barcodeScanner.scan(
      function (result) {
          alert("We got a barcode\n" +
                "Result: " + result.text + "\n" +
                "Format: " + result.format + "\n" +
                "Cancelled: " + result.cancelled);
      },
      function (error) {
          alert("Scanning failed: " + error);
      },
      {
          preferFrontCamera : true, // iOS and Android
          showFlipCameraButton : true, // iOS and Android
          showTorchButton : true, // iOS and Android
          torchOn: true, // Android, launch with the torch switched on (if available)
          saveHistory: true, // Android, save scan history (default false)
          prompt : "Place a barcode inside the scan area", // Android
          resultDisplayDuration: 500, // Android, display scanned text for X ms. 0 suppresses it entirely, default 1500
          formats : "QR_CODE,PDF_417", // default: all but PDF_417 and RSS_EXPANDED
          orientation : "landscape", // Android only (portrait|landscape), default unset so it rotates with the device
          disableAnimations : true, // iOS
          disableSuccessBeep: false // iOS and Android
      }
   );

Encoding a Barcode

插件使用方法 encode(type, data, success, fail) 创建对象 cordova.plugins.barcodeScanner

支持的编码类型:

  • TEXT_TYPE
  • EMAIL_TYPE
  • PHONE_TYPE
  • SMS_TYPE
A full example could be:

   cordova.plugins.barcodeScanner.encode(cordova.plugins.barcodeScanner.Encode.TEXT_TYPE, "http://www.nytimes.com", function(success) {
            alert("encode success: " + success);
          }, function(fail) {
            alert("encoding failed: " + fail);
          }
        );

iOS quirks

自 iOS 10 起,必须在 Info.plist 中添加 NSCameraUsageDescription

NSCameraUsageDescription 描述了应用访问用户相机的原因。 当系统提示用户允许访问时,该字符串显示为对话框的一部分。 如果您没有提供使用说明,应用程序将在显示对话框之前崩溃。 此外,Apple 将拒绝访问私人数据但不提供使用说明的应用程序。

要添加此条目,您可以在 config.xml 中使用 edit-config 标记,如下所示:

<edit-config target="NSCameraUsageDescription" file="*-Info.plist" mode="merge">
    <string>To scan barcodes</string>
</edit-config>

Windows quirks

  • Windows 实现当前不支持编码功能。

  • 在 Windows 10 桌面上,确保安装了 Windows Media Player 和媒体功能包。

Thanks on Github

这么多 - 查看原始 iOSAndroidBlackBerry 10 回购。

Licence

MIT 许可证

版权所有 (c) 2010 Matt Kane

特此免费向任何获得副本的人授予许可 本软件和相关文档文件(“软件”),处理 在软件中不受限制,包括但不限于权利 使用、复制、修改、合并、发布、分发、再许可和/或出售 该软件的副本,并允许该软件是 提供这样做,但须满足以下条件:

上述版权声明和本许可声明应包含在 本软件的所有副本或重要部分。

本软件“按原样”提供,不提供任何形式的明示或保证 暗示的,包括但不限于适销性保证, 适用于特定目的和非侵权。 在任何情况下都不得 作者或版权持有人对任何索赔、损害或其他 责任,无论是在合同、侵权或其他方面的行为中,由以下原因引起, 出于或与软件或使用或其他交易有关 软件。

# PhoneGap Plugin BarcodeScanner

Build Status

Cross-platform BarcodeScanner for Cordova / PhoneGap.

Follows the Cordova Plugin spec, so that it works with Plugman.

Installation

This requires phonegap 7.1.0+ ( current stable v8.0.0 )

phonegap plugin add phonegap-plugin-barcodescanner

It is also possible to install via repo url directly ( unstable )

phonegap plugin add https://github.com/phonegap/phonegap-plugin-barcodescanner.git

Optional variables: This plugin requires the Android support library v4. The minimum version is 24.1.0. Default value is 27.+. Check out the latest version here.

phonegap plugin add phonegap-plugin-barcodescanner --variable ANDROID_SUPPORT_V4_VERSION="27.1.1"

Supported Platforms

  • Android
  • iOS
  • Windows (Windows/Windows Phone 8.1 and Windows 10)
  • Browser

Note: the Android source for this project includes an Android Library Project. plugman currently doesn't support Library Project refs, so its been prebuilt as a jar library. Any updates to the Library Project should be committed with an updated jar.

Note: Windows 10 applications can not be build for AnyCPU architecture, which is default for Windows platform. If you want to build/run Windows 10 app, you should specify target architecture explicitly, for example (Cordova CLI):

cordova run windows -- --archs=x86

PhoneGap Build Usage

Add the following to your config.xml:

<!-- add a version here, otherwise PGB will use whatever the latest version of the package on npm is -->
<plugin name="phonegap-plugin-barcodescanner" />

On PhoneGap Build if you're using a version of cordova-android of 4 or less, ensure you're building with gradle:

<preference name="android-build-tool" value="gradle" />

Using the plugin

The plugin creates the object cordova.plugins.barcodeScanner with the method scan(success, fail).

The following barcode types are currently supported:

Barcode TypeAndroidiOSWindows
QR_CODE
DATA_MATRIX
UPC_A
UPC_E
EAN_8
EAN_13
CODE_39
CODE_93
CODE_128
CODABAR
ITF
RSS14
PDF_417
RSS_EXPANDED
MSI
AZTEC

success and fail are callback functions. Success is passed an object with data, type and cancelled properties. Data is the text representation of the barcode data, type is the type of barcode detected and cancelled is whether or not the user cancelled the scan.

A full example could be:

   cordova.plugins.barcodeScanner.scan(
      function (result) {
          alert("We got a barcode\n" +
                "Result: " + result.text + "\n" +
                "Format: " + result.format + "\n" +
                "Cancelled: " + result.cancelled);
      },
      function (error) {
          alert("Scanning failed: " + error);
      },
      {
          preferFrontCamera : true, // iOS and Android
          showFlipCameraButton : true, // iOS and Android
          showTorchButton : true, // iOS and Android
          torchOn: true, // Android, launch with the torch switched on (if available)
          saveHistory: true, // Android, save scan history (default false)
          prompt : "Place a barcode inside the scan area", // Android
          resultDisplayDuration: 500, // Android, display scanned text for X ms. 0 suppresses it entirely, default 1500
          formats : "QR_CODE,PDF_417", // default: all but PDF_417 and RSS_EXPANDED
          orientation : "landscape", // Android only (portrait|landscape), default unset so it rotates with the device
          disableAnimations : true, // iOS
          disableSuccessBeep: false // iOS and Android
      }
   );

Encoding a Barcode

The plugin creates the object cordova.plugins.barcodeScanner with the method encode(type, data, success, fail).

Supported encoding types:

  • TEXT_TYPE
  • EMAIL_TYPE
  • PHONE_TYPE
  • SMS_TYPE
A full example could be:

   cordova.plugins.barcodeScanner.encode(cordova.plugins.barcodeScanner.Encode.TEXT_TYPE, "http://www.nytimes.com", function(success) {
            alert("encode success: " + success);
          }, function(fail) {
            alert("encoding failed: " + fail);
          }
        );

iOS quirks

Since iOS 10 it's mandatory to add a NSCameraUsageDescription in the Info.plist.

NSCameraUsageDescription describes the reason that the app accesses the user's camera. When the system prompts the user to allow access, this string is displayed as part of the dialog box. If you didn't provide the usage description, the app will crash before showing the dialog. Also, Apple will reject apps that access private data but don't provide an usage description.

To add this entry you can use the edit-config tag in the config.xml like this:

<edit-config target="NSCameraUsageDescription" file="*-Info.plist" mode="merge">
    <string>To scan barcodes</string>
</edit-config>

Windows quirks

  • Windows implementation currently doesn't support encode functionality.

  • On Windows 10 desktop ensure that you have Windows Media Player and Media Feature pack installed.

Thanks on Github

So many -- check out the original iOS, Android and BlackBerry 10 repos.

Licence

The MIT License

Copyright (c) 2010 Matt Kane

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文