Skip to content

QR Code and Bar code scanning support for Codename One

Notifications You must be signed in to change notification settings

codenameone/cn1-codescan

 
 

Repository files navigation

Codename One CodeScanner Library

IMPORTANT: this library is deprecated in favor of QRScanner which is our official fork of the great work done by Little Monkey. This library works by lanuching an external QR/barcode scanner application, this approach broke with Android 13. The QRScanner library uses an embedded native code library and is much bigger as a result.

This library provides code scanning (QR code and Bar code) support for Codename One. This comprises a refactoring of the com.codename1.codescan package into a separate cn1lib so that only applications that require this feature need include it. The codescan package in the core Codename One library will be deprecated in the future, and this library will be the officially supported method of code scanning in Codename One.

License

Apache 2.0

Features

  • Scan QR Codes with device camera
  • Scan Bar Codes with device camera

Supported Platforms

Use CodeScanner.isSupported() to find out if it is supported on the current platform.

  • iOS - Supported and tested
  • Android - Supported and tested
  • Simulator - Not supported yet
  • Javascript - Not supported yet
  • RIM - Implemented but not tested
  • WinPhone - Not supported yet
  • J2ME - Implemented but not tested

Via Codename One Preferences

  1. Open Codename One Preferences (i.e. Control Center)
  2. Go to "Advanced Settings" > "Extensions" in the menu.
  3. Find the "Codenameone CodeScanning Library" option.
  4. Press "Download"

Maven Dependency

If you are using Maven as your build tool, then you can simply paste the following snippet into your common/pom.xml file:

<dependency>
  <groupId>com.codenameone</groupId>
  <artifactId>cn1-codescan-lib</artifactId>
  <version>1.0</version>
  <type>pom</type>
</dependency>

NOTE: You should replace the version with the latest on Maven central.

iOS Permissions

On iOS, apps require the NSCameraUsageDescription permission which can be set using the ios.NSCameraUsageDescription build hint. The value should describe the reason that you require the camera. e.g.

ios.NSCameraUsageDescription=The camera is required to scan bar codes

This build hint will be automatically added to your project if your app in the simulator and call the CodeScanner.isSupported() method.

Usage Example

Scanning QR Code:

...
import com.codename1.ext.codescan.CodeScanner;
import com.codename1.ext.codescan.ScanResult;
...

    CodeScanner.getInstance().scanQRCode(new ScanResult() {

        public void scanCompleted(String contents, String formatName, byte[] rawBytes) {
            //barCode.setText("Bar: " + contents);
            cnt.addComponent(new Label(contents));
            cnt.revalidate();
        }

        public void scanCanceled() {
            System.out.println("cancelled");
        }

        public void scanError(int errorCode, String message) {
            System.out.println("err " + message);
        }
    });

Scanning Bar Code:

...
import com.codename1.ext.codescan.CodeScanner;
import com.codename1.ext.codescan.ScanResult;
...

    CodeScanner.getInstance().scanBarCode(new ScanResult() {

        public void scanCompleted(String contents, String formatName, byte[] rawBytes) {
            //barCode.setText("Bar: " + contents);
            cnt.addComponent(new Label(contents));
            cnt.revalidate();
        }

        public void scanCanceled() {
            System.out.println("cancelled");
        }

        public void scanError(int errorCode, String message) {
            System.out.println("err " + message);
        }
    });   

Documentation

Building From Source

$ git clone https://github.com/codenameone/cn1-codescan.git
$ cd cn1-codescan
$ mvn package

You'll find cn1-codescan-1.0.cn1lib inside the common/target directory.

Creating new Maven Releases

  1. Update the version the next appropriate version number. E.g. to update to 1.0.1 run bash update-version.sh 1.0.1
  2. mvn deploy -Psign-artifacts
  3. Log into https://oss.sonatype.org/#stagingRepositories and close, then release repo.
  4. Update version to next snapshot version. E.g. bash update-version.sh 1.0.2-SNAPSHOT
  5. Push to git

About

QR Code and Bar code scanning support for Codename One

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C++ 63.2%
  • Objective-C 21.9%
  • Java 10.4%
  • HTML 2.7%
  • Shell 1.3%
  • JavaScript 0.3%
  • C# 0.2%