Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
Expand Down Expand Up @@ -100,6 +101,17 @@ enum USE_FLASH {

private int flashStatus = USE_FLASH.OFF.ordinal();

private BroadcastReceiver finishBroadcast;

@Override
public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter) {
if (Build.VERSION.SDK_INT >= 34 && getApplicationInfo().targetSdkVersion >= 34) {
int RECEIVER_EXPORTED = 2;
return super.registerReceiver(receiver, filter, RECEIVER_EXPORTED);
}
return super.registerReceiver(receiver, filter);
}

/**
* Initializes the UI and creates the detector pipeline.
*/
Expand Down Expand Up @@ -253,6 +265,17 @@ private void createCameraSource(boolean autoFocus, boolean useFlash, int cameraF
protected void onResume() {
super.onResume();
startCameraSource();

finishBroadcast = new BroadcastReceiver() {
@Override
public void onReceive(Context arg0, Intent intent) {
String action = intent.getAction();
if (action.equals("finishCapture")) {
finish();
}
}
};
registerReceiver(finishBroadcast, new IntentFilter("finishCapture"));
}

/**
Expand All @@ -264,6 +287,8 @@ protected void onPause() {
if (mPreview != null) {
mPreview.stop();
}

unregisterReceiver(finishBroadcast);
}

/**
Expand Down Expand Up @@ -533,4 +558,4 @@ public void onBarcodeDetected(Barcode barcode) {
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,11 @@ public static void registerWith(final PluginRegistry.Registrar registrar) {
public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
try {
pendingResult = result;

if (call.method.equals("dismiss")) {
activity.sendBroadcast(new Intent("finishCapture"));
return;
}

if (call.method.equals("scanBarcode")) {
if (!(call.arguments instanceof Map)) {
Expand Down Expand Up @@ -198,7 +203,9 @@ public static void onBarcodeScanReceiver(final Barcode barcode) {
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
barcodeStream.success(barcode.rawValue);
if (barcodeStream != null) {
barcodeStream.success(barcode.rawValue);
}
}
});
}
Expand Down
13 changes: 12 additions & 1 deletion ios/Classes/SwiftFlutterBarcodeScannerPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public class SwiftFlutterBarcodeScannerPlugin: NSObject, FlutterPlugin, ScanBarc
public static var isContinuousScan:Bool=false
static var barcodeStream:FlutterEventSink?=nil
public static var scanMode = ScanMode.QR.index
static var _controller: BarcodeScannerViewController?;

public static func register(with registrar: FlutterPluginRegistrar) {
viewController = (UIApplication.shared.delegate?.window??.rootViewController)!
Expand Down Expand Up @@ -52,11 +53,19 @@ public class SwiftFlutterBarcodeScannerPlugin: NSObject, FlutterPlugin, ScanBarc
}

public static func onBarcodeScanReceiver( barcode:String){
barcodeStream!(barcode)
if let stream = barcodeStream {
stream(barcode)
}
}

public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
let args:Dictionary<String, AnyObject> = call.arguments as! Dictionary<String, AnyObject>;

if let dismissScanner = args["dismissScanner"] as? String{
SwiftFlutterBarcodeScannerPlugin._controller?.dismiss(animated: true);
return;
}

if let colorCode = args["lineColor"] as? String{
SwiftFlutterBarcodeScannerPlugin.lineColor = colorCode
}else {
Expand Down Expand Up @@ -90,6 +99,7 @@ public class SwiftFlutterBarcodeScannerPlugin: NSObject, FlutterPlugin, ScanBarc

pendingResult=result
let controller = BarcodeScannerViewController()
SwiftFlutterBarcodeScannerPlugin._controller = controller;
controller.delegate = self

if #available(iOS 13.0, *) {
Expand Down Expand Up @@ -236,6 +246,7 @@ class BarcodeScannerViewController: UIViewController {
override public func viewDidDisappear(_ animated: Bool){
// Stop video capture
captureSession.stopRunning()
SwiftFlutterBarcodeScannerPlugin._controller = nil;
}

// Init UI components needed
Expand Down
4 changes: 4 additions & 0 deletions lib/flutter_barcode_scanner.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ class FlutterBarcodeScanner {

static Stream? _onBarcodeReceiver;

static dismiss() {
_channel.invokeMethod('dismiss', {'dismissScanner': '1'});
}

/// Scan with the camera until a barcode is identified, then return.
///
/// Shows a scan line with [lineColor] over a scan window. A flash icon is
Expand Down