mirror of
https://github.com/simplezhli/flutter_barcode_reader.git
synced 2024-11-26 06:49: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) {
|
||||
val barcode = data?.getStringExtra("SCAN_RESULT")
|
||||
barcode?.let { this.result?.success(barcode) }
|
||||
} else {
|
||||
val errorCode = data?.getStringExtra("ERROR_CODE")
|
||||
this.result?.error(errorCode, null, null)
|
||||
}
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
@ -26,7 +26,6 @@ import android.app.Activity
|
||||
import android.content.Intent
|
||||
import android.content.pm.PackageManager
|
||||
import android.os.Bundle
|
||||
import android.support.design.widget.Snackbar
|
||||
import android.support.v4.app.ActivityCompat
|
||||
import android.support.v4.content.ContextCompat
|
||||
import android.view.Menu
|
||||
@ -38,32 +37,29 @@ import me.dm7.barcodescanner.zxing.ZXingScannerView
|
||||
class BarcodeScannerActivity : Activity(), ZXingScannerView.ResultHandler {
|
||||
|
||||
lateinit var scannerView: me.dm7.barcodescanner.zxing.ZXingScannerView
|
||||
//lateinit var pressLightSnackBar: Snackbar
|
||||
|
||||
companion object {
|
||||
val REQUEST_TAKE_PHOTO_CAMERA_PERMISSION = 100
|
||||
val TOGGLE_FLASH = 200
|
||||
|
||||
}
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
title = ""
|
||||
scannerView = ZXingScannerView(this)
|
||||
scannerView.setAutoFocus(true)
|
||||
setContentView(scannerView)
|
||||
// pressLightSnackBar = Snackbar.make(scannerView, "Press Screen To Turn Light On",
|
||||
// Int.MAX_VALUE)
|
||||
// pressLightSnackBar.show()
|
||||
}
|
||||
|
||||
override fun onCreateOptionsMenu(menu: Menu): Boolean {
|
||||
if (scannerView.flash) {
|
||||
val item = menu.add(0,
|
||||
TOGGLE_FLASH, 0, "Flash Off")
|
||||
TOGGLE_FLASH, 0, "Flash Off")
|
||||
item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS)
|
||||
} else {
|
||||
val item = menu.add(0,
|
||||
TOGGLE_FLASH, 0, "Flash On")
|
||||
TOGGLE_FLASH, 0, "Flash On")
|
||||
item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS)
|
||||
}
|
||||
return super.onCreateOptionsMenu(menu)
|
||||
@ -81,6 +77,7 @@ class BarcodeScannerActivity : Activity(), ZXingScannerView.ResultHandler {
|
||||
override fun onResume() {
|
||||
super.onResume()
|
||||
scannerView.setResultHandler(this)
|
||||
// start camera immediately if permission is already given
|
||||
if (!requestCameraAccessIfNecessary()) {
|
||||
scannerView.startCamera()
|
||||
}
|
||||
@ -98,38 +95,38 @@ class BarcodeScannerActivity : Activity(), ZXingScannerView.ResultHandler {
|
||||
finish()
|
||||
}
|
||||
|
||||
fun finishWithError(errorCode: String) {
|
||||
val intent = Intent()
|
||||
intent.putExtra("ERROR_CODE", errorCode)
|
||||
setResult(Activity.RESULT_CANCELED, intent)
|
||||
finish()
|
||||
}
|
||||
|
||||
private fun requestCameraAccessIfNecessary(): Boolean {
|
||||
val array = arrayOf(Manifest.permission.CAMERA)
|
||||
if (ContextCompat
|
||||
.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)
|
||||
snackbar.setAction("OK") {
|
||||
ActivityCompat.requestPermissions(this,
|
||||
array,
|
||||
REQUEST_TAKE_PHOTO_CAMERA_PERMISSION)
|
||||
}
|
||||
snackbar.show()
|
||||
} else {
|
||||
ActivityCompat.requestPermissions(this,
|
||||
array,
|
||||
REQUEST_TAKE_PHOTO_CAMERA_PERMISSION)
|
||||
}
|
||||
.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
|
||||
|
||||
ActivityCompat.requestPermissions(this, array,
|
||||
REQUEST_TAKE_PHOTO_CAMERA_PERMISSION)
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>,
|
||||
grantResults: IntArray) {
|
||||
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>,grantResults: IntArray) {
|
||||
when (requestCode) {
|
||||
REQUEST_TAKE_PHOTO_CAMERA_PERMISSION -> {
|
||||
if (PermissionUtil.verifyPermissions(grantResults)) {
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -2,6 +2,7 @@ import 'dart:async';
|
||||
|
||||
import 'package:barcode_scan/barcode_scan.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
|
||||
void main() {
|
||||
runApp(new MyApp());
|
||||
@ -43,7 +44,21 @@ class _MyAppState extends State<MyApp> {
|
||||
}
|
||||
|
||||
Future scan() async {
|
||||
String barcode = await BarcodeScanner.scan();
|
||||
setState(() => this.barcode = barcode);
|
||||
try {
|
||||
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
|
||||
|
@ -38,7 +38,8 @@
|
||||
if (success) {
|
||||
[self startScan];
|
||||
} 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 {
|
||||
[self toggleFlash:!self.isFlashOn];
|
||||
[self updateFlashButton];
|
||||
@ -132,4 +123,4 @@
|
||||
}
|
||||
|
||||
|
||||
@end
|
||||
@end
|
||||
|
@ -9,5 +9,6 @@
|
||||
@protocol BarcodeScannerViewControllerDelegate <NSObject>
|
||||
|
||||
- (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';
|
||||
|
||||
class BarcodeScanner {
|
||||
static const CameraAccessDenied = 'PERMISSION_NOT_GRANTED';
|
||||
static const MethodChannel _channel =
|
||||
const MethodChannel('com.apptreesoftware.barcode_scan');
|
||||
static Future<String> scan() async => await _channel.invokeMethod('scan');
|
||||
|
Loading…
Reference in New Issue
Block a user