diff --git a/README.md b/README.md index 85ae5c821..f8a2030cb 100644 --- a/README.md +++ b/README.md @@ -100,48 +100,45 @@ Make sure it is enabled in your manifest file: ``` -## 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 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 @@ -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 @@ -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. @@ -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 diff --git a/build.gradle b/build.gradle index 20a4ac7cb..b33070f45 100644 --- a/build.gradle +++ b/build.gradle @@ -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' } diff --git a/sample/build.gradle b/sample/build.gradle index 0dcb9eb38..a7bcfbdd8 100644 --- a/sample/build.gradle +++ b/sample/build.gradle @@ -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' } diff --git a/sample/src/main/java/example/zxing/MainActivity.java b/sample/src/main/java/example/zxing/MainActivity.java index 99a3b6204..1b3c791eb 100644 --- a/sample/src/main/java/example/zxing/MainActivity.java +++ b/sample/src/main/java/example/zxing/MainActivity.java @@ -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; @@ -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 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) { @@ -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) { @@ -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) { @@ -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 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() { } @@ -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() { @@ -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(); - } - } } } diff --git a/sample/src/main/res/layout/activity_main.xml b/sample/src/main/res/layout/activity_main.xml index 3210cfd61..26749d4d3 100644 --- a/sample/src/main/res/layout/activity_main.xml +++ b/sample/src/main/res/layout/activity_main.xml @@ -25,12 +25,6 @@ android:text="Scan PDF417" android:onClick="scanPDF417"/> -