mirror of
https://github.com/simplezhli/flutter_barcode_reader.git
synced 2024-11-26 06:09:19 +08:00
Merge pull request #4 from BenSower/master
Switched to (imo) better error handling
This commit is contained in:
commit
443c756ab7
@ -41,7 +41,11 @@ class BarcodeScanPlugin(val activity: Activity): MethodCallHandler,
|
|||||||
if (resultCode == Activity.RESULT_OK) {
|
if (resultCode == Activity.RESULT_OK) {
|
||||||
val barcode = data?.getStringExtra("SCAN_RESULT")
|
val barcode = data?.getStringExtra("SCAN_RESULT")
|
||||||
barcode?.let { this.result?.success(barcode) }
|
barcode?.let { this.result?.success(barcode) }
|
||||||
|
} else {
|
||||||
|
val errorCode = data?.getStringExtra("ERROR_CODE")
|
||||||
|
this.result?.error(errorCode, null, null)
|
||||||
}
|
}
|
||||||
|
return true
|
||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
@ -26,7 +26,6 @@ import android.app.Activity
|
|||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
import android.content.pm.PackageManager
|
import android.content.pm.PackageManager
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import android.support.design.widget.Snackbar
|
|
||||||
import android.support.v4.app.ActivityCompat
|
import android.support.v4.app.ActivityCompat
|
||||||
import android.support.v4.content.ContextCompat
|
import android.support.v4.content.ContextCompat
|
||||||
import android.view.Menu
|
import android.view.Menu
|
||||||
@ -38,32 +37,29 @@ import me.dm7.barcodescanner.zxing.ZXingScannerView
|
|||||||
class BarcodeScannerActivity : Activity(), ZXingScannerView.ResultHandler {
|
class BarcodeScannerActivity : Activity(), ZXingScannerView.ResultHandler {
|
||||||
|
|
||||||
lateinit var scannerView: me.dm7.barcodescanner.zxing.ZXingScannerView
|
lateinit var scannerView: me.dm7.barcodescanner.zxing.ZXingScannerView
|
||||||
//lateinit var pressLightSnackBar: Snackbar
|
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
val REQUEST_TAKE_PHOTO_CAMERA_PERMISSION = 100
|
val REQUEST_TAKE_PHOTO_CAMERA_PERMISSION = 100
|
||||||
val TOGGLE_FLASH = 200
|
val TOGGLE_FLASH = 200
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onCreate(savedInstanceState: Bundle?) {
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
super.onCreate(savedInstanceState)
|
super.onCreate(savedInstanceState)
|
||||||
title = ""
|
title = ""
|
||||||
scannerView = ZXingScannerView(this)
|
scannerView = ZXingScannerView(this)
|
||||||
scannerView.setAutoFocus(true)
|
scannerView.setAutoFocus(true)
|
||||||
setContentView(scannerView)
|
setContentView(scannerView)
|
||||||
// pressLightSnackBar = Snackbar.make(scannerView, "Press Screen To Turn Light On",
|
|
||||||
// Int.MAX_VALUE)
|
|
||||||
// pressLightSnackBar.show()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onCreateOptionsMenu(menu: Menu): Boolean {
|
override fun onCreateOptionsMenu(menu: Menu): Boolean {
|
||||||
if (scannerView.flash) {
|
if (scannerView.flash) {
|
||||||
val item = menu.add(0,
|
val item = menu.add(0,
|
||||||
TOGGLE_FLASH, 0, "Flash Off")
|
TOGGLE_FLASH, 0, "Flash Off")
|
||||||
item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS)
|
item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS)
|
||||||
} else {
|
} else {
|
||||||
val item = menu.add(0,
|
val item = menu.add(0,
|
||||||
TOGGLE_FLASH, 0, "Flash On")
|
TOGGLE_FLASH, 0, "Flash On")
|
||||||
item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS)
|
item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS)
|
||||||
}
|
}
|
||||||
return super.onCreateOptionsMenu(menu)
|
return super.onCreateOptionsMenu(menu)
|
||||||
@ -81,6 +77,7 @@ class BarcodeScannerActivity : Activity(), ZXingScannerView.ResultHandler {
|
|||||||
override fun onResume() {
|
override fun onResume() {
|
||||||
super.onResume()
|
super.onResume()
|
||||||
scannerView.setResultHandler(this)
|
scannerView.setResultHandler(this)
|
||||||
|
// start camera immediately if permission is already given
|
||||||
if (!requestCameraAccessIfNecessary()) {
|
if (!requestCameraAccessIfNecessary()) {
|
||||||
scannerView.startCamera()
|
scannerView.startCamera()
|
||||||
}
|
}
|
||||||
@ -98,38 +95,38 @@ class BarcodeScannerActivity : Activity(), ZXingScannerView.ResultHandler {
|
|||||||
finish()
|
finish()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun finishWithError(errorCode: String) {
|
||||||
|
val intent = Intent()
|
||||||
|
intent.putExtra("ERROR_CODE", errorCode)
|
||||||
|
setResult(Activity.RESULT_CANCELED, intent)
|
||||||
|
finish()
|
||||||
|
}
|
||||||
|
|
||||||
private fun requestCameraAccessIfNecessary(): Boolean {
|
private fun requestCameraAccessIfNecessary(): Boolean {
|
||||||
val array = arrayOf(Manifest.permission.CAMERA)
|
val array = arrayOf(Manifest.permission.CAMERA)
|
||||||
if (ContextCompat
|
if (ContextCompat
|
||||||
.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
|
.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
|
||||||
if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CAMERA)) {
|
|
||||||
val snackbar = Snackbar.make(scannerView, "Grant access to the camera to enable barcode scanning.", Snackbar.LENGTH_INDEFINITE)
|
ActivityCompat.requestPermissions(this, array,
|
||||||
snackbar.setAction("OK") {
|
REQUEST_TAKE_PHOTO_CAMERA_PERMISSION)
|
||||||
ActivityCompat.requestPermissions(this,
|
|
||||||
array,
|
|
||||||
REQUEST_TAKE_PHOTO_CAMERA_PERMISSION)
|
|
||||||
}
|
|
||||||
snackbar.show()
|
|
||||||
} else {
|
|
||||||
ActivityCompat.requestPermissions(this,
|
|
||||||
array,
|
|
||||||
REQUEST_TAKE_PHOTO_CAMERA_PERMISSION)
|
|
||||||
}
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>,
|
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>,grantResults: IntArray) {
|
||||||
grantResults: IntArray) {
|
|
||||||
when (requestCode) {
|
when (requestCode) {
|
||||||
REQUEST_TAKE_PHOTO_CAMERA_PERMISSION -> {
|
REQUEST_TAKE_PHOTO_CAMERA_PERMISSION -> {
|
||||||
if (PermissionUtil.verifyPermissions(grantResults)) {
|
if (PermissionUtil.verifyPermissions(grantResults)) {
|
||||||
scannerView.startCamera()
|
scannerView.startCamera()
|
||||||
|
} else {
|
||||||
|
finishWithError("PERMISSION_NOT_GRANTED")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
else -> {
|
||||||
|
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -155,4 +152,4 @@ object PermissionUtil {
|
|||||||
}
|
}
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,6 +2,7 @@ import 'dart:async';
|
|||||||
|
|
||||||
import 'package:barcode_scan/barcode_scan.dart';
|
import 'package:barcode_scan/barcode_scan.dart';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:flutter/services.dart';
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
runApp(new MyApp());
|
runApp(new MyApp());
|
||||||
@ -43,7 +44,21 @@ class _MyAppState extends State<MyApp> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Future scan() async {
|
Future scan() async {
|
||||||
String barcode = await BarcodeScanner.scan();
|
try {
|
||||||
setState(() => this.barcode = barcode);
|
String barcode = await BarcodeScanner.scan();
|
||||||
|
setState(() => this.barcode = barcode);
|
||||||
|
} on PlatformException catch (e) {
|
||||||
|
if (e.code == BarcodeScanner.CameraAccessDenied) {
|
||||||
|
setState(() {
|
||||||
|
this.barcode = 'The user did not grant the camera permission!';
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
setState(() => this.barcode = 'Unknown error: $e');
|
||||||
|
}
|
||||||
|
} on FormatException{
|
||||||
|
setState(() => this.barcode = 'null (User returned using the "back"-button before scanning anything. Result)');
|
||||||
|
} catch (e) {
|
||||||
|
setState(() => this.barcode = 'Unknown error: $e');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -32,4 +32,12 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
- (void)barcodeScannerViewController:(BarcodeScannerViewController *)controller didFailWithErrorCode:(NSString *)errorCode {
|
||||||
|
if (self.result){
|
||||||
|
self.result([FlutterError errorWithCode:errorCode
|
||||||
|
message:nil
|
||||||
|
details:nil]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
@ -38,7 +38,8 @@
|
|||||||
if (success) {
|
if (success) {
|
||||||
[self startScan];
|
[self startScan];
|
||||||
} else {
|
} else {
|
||||||
[self showNoCameraAccessAlert];
|
[self.delegate barcodeScannerViewController:self didFailWithErrorCode:@"PERMISSION_NOT_GRANTED"];
|
||||||
|
[self dismissViewControllerAnimated:NO completion:nil];
|
||||||
}
|
}
|
||||||
}];
|
}];
|
||||||
}
|
}
|
||||||
@ -81,16 +82,6 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)showNoCameraAccessAlert {
|
|
||||||
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Camera access denied"
|
|
||||||
message:@"Camera access has been disabled for this application. To turn camera access back on, please go to the iOS settings application."
|
|
||||||
preferredStyle:UIAlertControllerStyleAlert];
|
|
||||||
[alertController addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
|
|
||||||
[self dismissViewControllerAnimated:YES completion:nil];
|
|
||||||
}]];
|
|
||||||
[self presentViewController:alertController animated:YES completion:nil];
|
|
||||||
}
|
|
||||||
|
|
||||||
- (void)toggle {
|
- (void)toggle {
|
||||||
[self toggleFlash:!self.isFlashOn];
|
[self toggleFlash:!self.isFlashOn];
|
||||||
[self updateFlashButton];
|
[self updateFlashButton];
|
||||||
@ -132,4 +123,4 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
@ -9,5 +9,6 @@
|
|||||||
@protocol BarcodeScannerViewControllerDelegate <NSObject>
|
@protocol BarcodeScannerViewControllerDelegate <NSObject>
|
||||||
|
|
||||||
- (void)barcodeScannerViewController:(BarcodeScannerViewController *)controller didScanBarcodeWithResult:(NSString *)result;
|
- (void)barcodeScannerViewController:(BarcodeScannerViewController *)controller didScanBarcodeWithResult:(NSString *)result;
|
||||||
|
- (void)barcodeScannerViewController:(BarcodeScannerViewController *)controller didFailWithErrorCode:(NSString *)errorCode;
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
@ -3,6 +3,7 @@ import 'dart:async';
|
|||||||
import 'package:flutter/services.dart';
|
import 'package:flutter/services.dart';
|
||||||
|
|
||||||
class BarcodeScanner {
|
class BarcodeScanner {
|
||||||
|
static const CameraAccessDenied = 'PERMISSION_NOT_GRANTED';
|
||||||
static const MethodChannel _channel =
|
static const MethodChannel _channel =
|
||||||
const MethodChannel('com.apptreesoftware.barcode_scan');
|
const MethodChannel('com.apptreesoftware.barcode_scan');
|
||||||
static Future<String> scan() async => await _channel.invokeMethod('scan');
|
static Future<String> scan() async => await _channel.invokeMethod('scan');
|
||||||
|
Loading…
Reference in New Issue
Block a user