Skip to content

Commit

Permalink
feat: initial functionality with click to scan barcode iOS/Android
Browse files Browse the repository at this point in the history
  • Loading branch information
KholofeloMoyaba committed Nov 24, 2019
1 parent 2a7347b commit 2224537
Show file tree
Hide file tree
Showing 91 changed files with 2,930 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## 0.0.1

* scanBarcode method that opens a camera views and exits once an item has been scanned or the user disimisses the scanner.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2019 Amol Gangadhare

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.
6 changes: 6 additions & 0 deletions android/.classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8/"/>
<classpathentry kind="con" path="org.eclipse.buildship.core.gradleclasspathcontainer"/>
<classpathentry kind="output" path="bin/default"/>
</classpath>
8 changes: 8 additions & 0 deletions android/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
*.iml
.gradle
/local.properties
/.idea/workspace.xml
/.idea/libraries
.DS_Store
/build
/captures
23 changes: 23 additions & 0 deletions android/.project
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>flutter_scandit</name>
<comment>Project flutter_scandit created by Buildship.</comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.buildship.core.gradleprojectbuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.buildship.core.gradleprojectnature</nature>
</natures>
</projectDescription>
13 changes: 13 additions & 0 deletions android/.settings/org.eclipse.buildship.core.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
arguments=
auto.sync=false
build.scans.enabled=false
connection.gradle.distribution=GRADLE_DISTRIBUTION(VERSION(6.0-20191016123526+0000))
connection.project.dir=../example/android
eclipse.preferences.version=1
gradle.user.home=
java.home=
jvm.arguments=
offline.mode=false
override.workspace.settings=true
show.console.view=true
show.executions.view=true
44 changes: 44 additions & 0 deletions android/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
group 'za.co.palota.flutter_scandit'
version '1.0'

buildscript {
repositories {
google()
jcenter()
maven { url 'https://scandit.bintray.com/DataCapture' }
}

dependencies {
classpath 'com.android.tools.build:gradle:3.2.1'
}
}

rootProject.allprojects {
repositories {
google()
jcenter()
}
}

apply plugin: 'com.android.library'

android {
compileSdkVersion 28

defaultConfig {
minSdkVersion 19
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
lintOptions {
disable 'InvalidPackage'
}
dependencies {
implementation "com.scandit.datacapture:core:6.1.0"
implementation "com.scandit.datacapture:barcode:6.1.0"

implementation "androidx.appcompat:appcompat:1.0.2"
implementation "com.google.android.material:material:1.0.0"

implementation "androidx.constraintlayout:constraintlayout:1.1.3"
}
}
2 changes: 2 additions & 0 deletions android/gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
org.gradle.jvmargs=-Xmx1536M

5 changes: 5 additions & 0 deletions android/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.2-all.zip
1 change: 1 addition & 0 deletions android/settings.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
rootProject.name = 'flutter_scandit'
19 changes: 19 additions & 0 deletions android/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="za.co.palota.flutter_scandit">
<uses-permission android:name="android.permission.INTERNET"/>

<application
android:supportsRtl="true"
android:theme="@style/Theme.AppCompat.NoActionBar">

<activity
android:name=".BarcodeScanActivity"
android:screenOrientation="sensor">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>

<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,216 @@
package za.co.palota.flutter_scandit;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import com.scandit.datacapture.barcode.capture.*;
import com.scandit.datacapture.barcode.data.Barcode;
import com.scandit.datacapture.barcode.data.Symbology;
import com.scandit.datacapture.barcode.ui.overlay.BarcodeCaptureOverlay;
import com.scandit.datacapture.core.capture.DataCaptureContext;
import com.scandit.datacapture.core.data.FrameData;
import com.scandit.datacapture.core.source.Camera;
import com.scandit.datacapture.core.source.FrameSourceState;
import com.scandit.datacapture.core.ui.DataCaptureView;
import com.scandit.datacapture.core.ui.viewfinder.RectangularViewfinder;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;

public class BarcodeScanActivity
extends CameraPermissionActivity implements BarcodeCaptureListener {
public static final String BARCODE_ERROR = "error";
public static final String BARCODE_DATA = "data";
public static final String BARCODE_SYMBOLOGY = "symbology";

private DataCaptureContext dataCaptureContext;
private BarcodeCapture barcodeCapture;
private Camera camera;
private DataCaptureView dataCaptureView;

private AlertDialog dialog;

private String licenseKey;
private HashSet<Symbology> symbologies;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

Intent intent = getIntent();
this.licenseKey = intent.getStringExtra(FlutterScanditPlugin.LICENSE_KEY);
ArrayList<String> passedSymbologies = intent.getStringArrayListExtra(FlutterScanditPlugin.SYMBOLOGIES_KEY);
symbologies = new HashSet<>();

for(String symbologyName:passedSymbologies){
Symbology symbology = FlutterScanditPlugin.convertToSymbology(symbologyName);
if(symbology != null){
symbologies.add(symbology);
}
}
if(symbologies.isEmpty()){
symbologies.add(Symbology.EAN13_UPCA); // default
}

// Initialize and start the barcode recognition.
initializeAndStartBarcodeScanning();
}

private void finishWithError(String errorReason) {
Intent data = new Intent();
data.putExtra(BARCODE_ERROR, errorReason);
setResult(Activity.RESULT_CANCELED, data);
finish();
}

private void initializeAndStartBarcodeScanning() {
// Create data capture context using your license key.
dataCaptureContext = DataCaptureContext.forLicenseKey(this.licenseKey);

// Use the default camera and set it as the frame source of the context.
// The camera is off by default and must be turned on to start streaming frames to the data
// capture context for recognition.
// See resumeFrameSource and pauseFrameSource below.
camera = Camera.getDefaultCamera();
if (camera != null) {
// Use the recommended camera settings for the BarcodeCapture mode.
camera.applySettings(BarcodeCapture.createRecommendedCameraSettings());
dataCaptureContext.setFrameSource(camera);
} else {
finishWithError("NO_CAMERA");
}

// The barcode capturing process is configured through barcode capture settings
// which are then applied to the barcode capture instance that manages barcode recognition.
BarcodeCaptureSettings barcodeCaptureSettings = new BarcodeCaptureSettings();

// The settings instance initially has all types of barcodes (symbologies) disabled.
// For the purpose of this sample we enable a very generous set of symbologies.
// In your own app ensure that you only enable the symbologies that your app requires as
// every additional enabled symbology has an impact on processing times.

barcodeCaptureSettings.enableSymbologies(this.symbologies);

// Some linear/1d barcode symbologies allow you to encode variable-length data.
// By default, the Scandit Data Capture SDK only scans barcodes in a certain length range.
// If your application requires scanning of one of these symbologies, and the length is
// falling outside the default range, you may need to adjust the "active symbol counts"
// for this symbology. This is shown in the following few lines of code for one of the
// variable-length symbologies.
SymbologySettings symbologySettings =
barcodeCaptureSettings.getSymbologySettings(Symbology.CODE39);

HashSet<Short> activeSymbolCounts = new HashSet<>(
Arrays.asList(new Short[] { 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 }));

symbologySettings.setActiveSymbolCounts(activeSymbolCounts);

// Create new barcode capture mode with the settings from above.
barcodeCapture = BarcodeCapture.forDataCaptureContext(dataCaptureContext, barcodeCaptureSettings);

// Register self as a listener to get informed whenever a new barcode got recognized.
barcodeCapture.addListener(this);

// To visualize the on-going barcode capturing process on screen, setup a data capture view
// that renders the camera preview. The view must be connected to the data capture context.
dataCaptureView = DataCaptureView.newInstance(this, dataCaptureContext);

// Add a barcode capture overlay to the data capture view to render the location of captured
// barcodes on top of the video preview.
// This is optional, but recommended for better visual feedback.
BarcodeCaptureOverlay overlay = BarcodeCaptureOverlay.newInstance(barcodeCapture, dataCaptureView);
overlay.setViewfinder(new RectangularViewfinder());

setContentView(dataCaptureView);
}

@Override
protected void onPause() {
pauseFrameSource();
super.onPause();
}

@Override
protected void onDestroy() {
barcodeCapture.removeListener(this);
dataCaptureContext.removeMode(barcodeCapture);
super.onDestroy();
}

private void pauseFrameSource() {
// Switch camera off to stop streaming frames.
// The camera is stopped asynchronously and will take some time to completely turn off.
// Until it is completely stopped, it is still possible to receive further results, hence
// it's a good idea to first disable barcode capture as well.
barcodeCapture.setEnabled(false);
camera.switchToDesiredState(FrameSourceState.OFF, null);
}

@Override
protected void onResume() {
super.onResume();

// Check for camera permission and request it, if it hasn't yet been granted.
// Once we have the permission the onCameraPermissionGranted() method will be called.
requestCameraPermission();
}

@Override
public void onCameraPermissionGranted() {
resumeFrameSource();
}

@Override
public void onCameraPermissionDenied() {
finishWithError("CAMERA_PERMISSION_DENIED");
}

private void resumeFrameSource() {
dismissScannedCodesDialog();

// Switch camera on to start streaming frames.
// The camera is started asynchronously and will take some time to completely turn on.
barcodeCapture.setEnabled(true);
camera.switchToDesiredState(FrameSourceState.ON, null);
}

private void dismissScannedCodesDialog() {
if (dialog != null) {
dialog.dismiss();
dialog = null;
}
}


@Override
public void onBarcodeScanned(
@NonNull BarcodeCapture barcodeCapture,
@NonNull BarcodeCaptureSession session,
@NonNull FrameData frameData
) {
if (session.getNewlyRecognizedBarcodes().isEmpty()) return;

Barcode barcode = session.getNewlyRecognizedBarcodes().get(0);


Intent data = new Intent();
data.putExtra(BARCODE_DATA, barcode.getData());
data.putExtra(BARCODE_SYMBOLOGY, barcode.getSymbology().name());
setResult(Activity.RESULT_OK, data);
finish();
}


@Override
public void onSessionUpdated(@NonNull BarcodeCapture barcodeCapture,
@NonNull BarcodeCaptureSession session, @NonNull FrameData data) {}

@Override
public void onObservationStarted(@NonNull BarcodeCapture barcodeCapture) {}

@Override
public void onObservationStopped(@NonNull BarcodeCapture barcodeCapture) {}
}
Loading

0 comments on commit 2224537

Please sign in to comment.