Skip to content

Commit

Permalink
ScanOptions and ScanContract.
Browse files Browse the repository at this point in the history
  • Loading branch information
rkistner committed Oct 25, 2021
1 parent 9beeac7 commit 0c97cf7
Show file tree
Hide file tree
Showing 9 changed files with 549 additions and 140 deletions.
75 changes: 35 additions & 40 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,48 +100,45 @@ Make sure it is enabled in your manifest file:
<application android:hardwareAccelerated="true" ... >
```

## Usage with IntentIntegrator
## Usage with ScanContract

Launch the intent with the default options:
```java
new IntentIntegrator(this).initiateScan(); // `this` is the current Activity


// Get the results:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
if(result != null) {
if(result.getContents() == null) {
Toast.makeText(this, "Cancelled", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(this, "Scanned: " + result.getContents(), Toast.LENGTH_LONG).show();
}
} else {
super.onActivityResult(requestCode, resultCode, data);
}
}
```
Note: `startActivityForResult` is deprecated, so this example uses `registerForActivityResult` instead.
See for details: https://developer.android.com/training/basics/intents/result

Use from a Fragment:
```java
IntentIntegrator.forFragment(this).initiateScan(); // `this` is the current Fragment
`startActivityForResult` can still be used via `IntentIntegrator`, but that is not recommended anymore.

// If you're using the support library, use IntentIntegrator.forSupportFragment(this) instead.
```java
// Note: startActivityForResult is deprecated, so this example uses registerForActivityResult instead.
// See for details: https://developer.android.com/training/basics/intents/result

// Register the launcher and result handler
private final ActivityResultLauncher<ScanOptions> barcodeLauncher = registerForActivityResult(new ScanContract(),
result -> {
if(result.getContents() == null) {
Toast.makeText(MyActivity.this, "Cancelled", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(MyActivity.this, "Scanned: " + result.getContents(), Toast.LENGTH_LONG).show();
}
});

// Launch
public void onButtonClick(View view) {
barcodeLauncher.launch(new ScanOptions());
}
```

Customize options:
```java
IntentIntegrator integrator = new IntentIntegrator(this);
integrator.setDesiredBarcodeFormats(IntentIntegrator.ONE_D_CODE_TYPES);
integrator.setPrompt("Scan a barcode");
integrator.setCameraId(0); // Use a specific camera of the device
integrator.setBeepEnabled(false);
integrator.setBarcodeImageEnabled(true);
integrator.initiateScan();
ScanOptions options = new ScanOptions();
options.setDesiredBarcodeFormats(ScanOptions.ONE_D_CODE_TYPES);
options.setPrompt("Scan a barcode");
options.setCameraId(0); // Use a specific camera of the device
options.setBeepEnabled(false);
options.setBarcodeImageEnabled(true);
barcodeLauncher.launch(options);
```

See [IntentIntegrator][5] for more options.
See [BarcodeOptions][5] for more options.

### Generate Barcode example

Expand Down Expand Up @@ -177,9 +174,9 @@ Sample:
```

```java
IntentIntegrator integrator = new IntentIntegrator(this);
integrator.setOrientationLocked(false);
integrator.initiateScan();
ScanOptions options = new ScanOptions();
options.setOrientationLocked(false);
barcodeLauncher.launch(options);
```

### Customization and advanced options
Expand Down Expand Up @@ -223,7 +220,7 @@ You can then use your local version by specifying in your `build.gradle` file:

Licensed under the [Apache License 2.0][7]

Copyright (C) 2012-2018 ZXing authors, Journey Mobile
Copyright (C) 2012-201 ZXing authors, Journey Mobile

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand All @@ -241,7 +238,5 @@ Licensed under the [Apache License 2.0][7]

[1]: http://journeyapps.com
[2]: https://github.com/zxing/zxing/
[3]: https://github.com/zxing/zxing/wiki/Scanning-Via-Intent
[4]: https://github.com/journeyapps/zxing-android-embedded/blob/2.x/README.md
[5]: zxing-android-embedded/src/com/google/zxing/integration/android/IntentIntegrator.java
[5]: zxing-android-embedded/src/com/journeyapps/barcodescanner/ScanOptions.java
[7]: http://www.apache.org/licenses/LICENSE-2.0
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ subprojects {
version = '4.2.0'
group = 'com.journeyapps'

ext.androidTargetSdk = 28
ext.androidTargetSdk = 30
ext.zxingCore = 'com.google.zxing:core:3.4.1'
}
7 changes: 4 additions & 3 deletions sample/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -65,16 +65,17 @@ dependencies {
implementation project(':zxing-android-embedded')

implementation 'androidx.appcompat:appcompat:1.3.1'
implementation "androidx.legacy:legacy-support-v13:1.0.0"
implementation 'androidx.legacy:legacy-support-v13:1.0.0'
implementation "androidx.activity:activity:1.3.1"

// Desugaring and multidex is required for API < 21.
coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:1.1.5'
implementation "androidx.multidex:multidex:2.0.1"
implementation 'androidx.multidex:multidex:2.0.1'

// leakcanary is for development purposes only
// https://github.com/square/leakcanary
debugImplementation 'com.squareup.leakcanary:leakcanary-android:2.7'

// AboutLibraries
implementation "com.mikepenz:aboutlibraries:6.2.3"
implementation 'com.mikepenz:aboutlibraries:6.2.3'
}
159 changes: 69 additions & 90 deletions sample/src/main/java/example/zxing/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
import android.content.Intent;
import android.hardware.Camera;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import androidx.appcompat.app.AppCompatActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
Expand All @@ -13,14 +11,33 @@
import android.widget.Toast;

import com.google.zxing.client.android.Intents;
import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;
import com.journeyapps.barcodescanner.ScanContract;
import com.journeyapps.barcodescanner.ScanOptions;
import com.mikepenz.aboutlibraries.LibsBuilder;

import androidx.activity.result.ActivityResultLauncher;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;


public class MainActivity extends AppCompatActivity {
private final ActivityResultLauncher<ScanOptions> barcodeLauncher = registerForActivityResult(new ScanContract(),
result -> {
if(result.getContents() == null) {
Intent originalIntent = result.getOriginalIntent();
if (originalIntent == null) {
Log.d("MainActivity", "Cancelled scan");
Toast.makeText(MainActivity.this, "Cancelled", Toast.LENGTH_LONG).show();
} else if(originalIntent.hasExtra(Intents.Scan.MISSING_CAMERA_PERMISSION)) {
Log.d("MainActivity", "Cancelled scan due to missing camera permission");
Toast.makeText(MainActivity.this, "Cancelled due to missing camera permission", Toast.LENGTH_LONG).show();
}
} else {
Log.d("MainActivity", "Scanned");
Toast.makeText(MainActivity.this, "Scanned: " + result.getContents(), Toast.LENGTH_LONG).show();
}
});

public final int CUSTOMIZED_REQUEST_CODE = 0x0000ffff;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -29,49 +46,45 @@ protected void onCreate(Bundle savedInstanceState) {
}

public void scanBarcode(View view) {
new IntentIntegrator(this).initiateScan();
}

public void scanBarcodeWithCustomizedRequestCode(View view) {
new IntentIntegrator(this).setRequestCode(CUSTOMIZED_REQUEST_CODE).initiateScan();
barcodeLauncher.launch(new ScanOptions());
}

public void scanBarcodeInverted(View view){
IntentIntegrator integrator = new IntentIntegrator(this);
integrator.addExtra(Intents.Scan.SCAN_TYPE, Intents.Scan.INVERTED_SCAN);
integrator.initiateScan();
ScanOptions options = new ScanOptions();
options.addExtra(Intents.Scan.SCAN_TYPE, Intents.Scan.INVERTED_SCAN);
barcodeLauncher.launch(options);
}

public void scanMixedBarcodes(View view){
IntentIntegrator integrator = new IntentIntegrator(this);
integrator.addExtra(Intents.Scan.SCAN_TYPE, Intents.Scan.MIXED_SCAN);
integrator.initiateScan();
ScanOptions options = new ScanOptions();
options.addExtra(Intents.Scan.SCAN_TYPE, Intents.Scan.MIXED_SCAN);
barcodeLauncher.launch(options);
}

public void scanBarcodeCustomLayout(View view) {
IntentIntegrator integrator = new IntentIntegrator(this);
integrator.setCaptureActivity(AnyOrientationCaptureActivity.class);
integrator.setDesiredBarcodeFormats(IntentIntegrator.ONE_D_CODE_TYPES);
integrator.setPrompt("Scan something");
integrator.setOrientationLocked(false);
integrator.setBeepEnabled(false);
integrator.initiateScan();
ScanOptions options = new ScanOptions();
options.setCaptureActivity(AnyOrientationCaptureActivity.class);
options.setDesiredBarcodeFormats(ScanOptions.ONE_D_CODE_TYPES);
options.setPrompt("Scan something");
options.setOrientationLocked(false);
options.setBeepEnabled(false);
barcodeLauncher.launch(options);
}

public void scanPDF417(View view) {
IntentIntegrator integrator = new IntentIntegrator(this);
integrator.setDesiredBarcodeFormats(IntentIntegrator.PDF_417);
integrator.setPrompt("Scan something");
integrator.setOrientationLocked(false);
integrator.setBeepEnabled(false);
integrator.initiateScan();
ScanOptions options = new ScanOptions();
options.setDesiredBarcodeFormats(ScanOptions.PDF_417);
options.setPrompt("Scan something");
options.setOrientationLocked(false);
options.setBeepEnabled(false);
barcodeLauncher.launch(options);
}


public void scanBarcodeFrontCamera(View view) {
IntentIntegrator integrator = new IntentIntegrator(this);
integrator.setCameraId(Camera.CameraInfo.CAMERA_FACING_FRONT);
integrator.initiateScan();
ScanOptions options = new ScanOptions();
options.setCameraId(Camera.CameraInfo.CAMERA_FACING_FRONT);
barcodeLauncher.launch(options);
}

public void scanContinuous(View view) {
Expand All @@ -80,24 +93,26 @@ public void scanContinuous(View view) {
}

public void scanToolbar(View view) {
new IntentIntegrator(this).setCaptureActivity(ToolbarCaptureActivity.class).initiateScan();
ScanOptions options = new ScanOptions().setCaptureActivity(ToolbarCaptureActivity.class);
barcodeLauncher.launch(options);
}

public void scanCustomScanner(View view) {
new IntentIntegrator(this).setOrientationLocked(false).setCaptureActivity(CustomScannerActivity.class).initiateScan();
ScanOptions options = new ScanOptions().setOrientationLocked(false).setCaptureActivity(CustomScannerActivity.class);
barcodeLauncher.launch(options);
}

public void scanMarginScanner(View view) {
IntentIntegrator integrator = new IntentIntegrator(this);
integrator.setOrientationLocked(false);
integrator.setCaptureActivity(SmallCaptureActivity.class);
integrator.initiateScan();
ScanOptions options = new ScanOptions();
options.setOrientationLocked(false);
options.setCaptureActivity(SmallCaptureActivity.class);
barcodeLauncher.launch(options);
}

public void scanWithTimeout(View view) {
IntentIntegrator integrator = new IntentIntegrator(this);
integrator.setTimeout(8000);
integrator.initiateScan();
ScanOptions options = new ScanOptions();
options.setTimeout(8000);
barcodeLauncher.launch(options);
}

public void tabs(View view) {
Expand All @@ -109,45 +124,24 @@ public void about(View view) {
new LibsBuilder().start(this);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode != CUSTOMIZED_REQUEST_CODE && requestCode != IntentIntegrator.REQUEST_CODE) {
// This is important, otherwise the result will not be passed to the fragment
super.onActivityResult(requestCode, resultCode, data);
return;
}
switch (requestCode) {
case CUSTOMIZED_REQUEST_CODE: {
Toast.makeText(this, "REQUEST_CODE = " + requestCode, Toast.LENGTH_LONG).show();
break;
}
default:
break;
}

IntentResult result = IntentIntegrator.parseActivityResult(resultCode, data);

if(result.getContents() == null) {
Intent originalIntent = result.getOriginalIntent();
if (originalIntent == null) {
Log.d("MainActivity", "Cancelled scan");
Toast.makeText(this, "Cancelled", Toast.LENGTH_LONG).show();
} else if(originalIntent.hasExtra(Intents.Scan.MISSING_CAMERA_PERMISSION)) {
Log.d("MainActivity", "Cancelled scan due to missing camera permission");
Toast.makeText(this, "Cancelled due to missing camera permission", Toast.LENGTH_LONG).show();
}
} else {
Log.d("MainActivity", "Scanned");
Toast.makeText(this, "Scanned: " + result.getContents(), Toast.LENGTH_LONG).show();
}
}

/**
* Sample of scanning from a Fragment
*/
public static class ScanFragment extends Fragment {
private String toast;

private final ActivityResultLauncher<ScanOptions> fragmentLauncher = registerForActivityResult(new ScanContract(),
result -> {
if(result.getContents() == null) {
toast = "Cancelled from fragment";
} else {
toast = "Scanned from fragment: " + result.getContents();
}

// At this point we may or may not have a reference to the activity
displayToast();
});

public ScanFragment() {
}

Expand All @@ -168,7 +162,7 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
}

public void scanFromFragment() {
IntentIntegrator.forSupportFragment(this).initiateScan();
fragmentLauncher.launch(new ScanOptions());
}

private void displayToast() {
Expand All @@ -177,20 +171,5 @@ private void displayToast() {
toast = null;
}
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
if(result != null) {
if(result.getContents() == null) {
toast = "Cancelled from fragment";
} else {
toast = "Scanned from fragment: " + result.getContents();
}

// At this point we may or may not have a reference to the activity
displayToast();
}
}
}
}
6 changes: 0 additions & 6 deletions sample/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,6 @@
android:text="Scan PDF417"
android:onClick="scanPDF417"/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/scan_barcode_with_request_code"
android:onClick="scanBarcodeWithCustomizedRequestCode"/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
Expand Down
Loading

0 comments on commit 0c97cf7

Please sign in to comment.