Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into next
Browse files Browse the repository at this point in the history
  • Loading branch information
robingenz committed Oct 9, 2024
2 parents 7ddd405 + 596ce13 commit 26891e1
Show file tree
Hide file tree
Showing 20 changed files with 181 additions and 19 deletions.
5 changes: 0 additions & 5 deletions .changeset/hungry-trees-raise.md

This file was deleted.

2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ on:
workflow_dispatch:

env:
NODE_VERSION: 18
NODE_VERSION: 20
JAVA_VERSION: 17

jobs:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ on:
concurrency: ${{ github.workflow }}-${{ github.ref }}

env:
NODE_VERSION: 18
NODE_VERSION: 20

jobs:
release:
Expand Down
12 changes: 12 additions & 0 deletions packages/barcode-scanning/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
# Changelog

## 6.2.0

### Minor Changes

- [`fa76d3c`](https://github.com/capawesome-team/capacitor-mlkit/commit/fa76d3c0eafa1c60143421caa04f3c553401c87f) ([#195](https://github.com/capawesome-team/capacitor-mlkit/pull/195)): feat: add `barcodesScanned` listener

* [`c3672b4`](https://github.com/capawesome-team/capacitor-mlkit/commit/c3672b47e3e6f9f190bd432e9518aae0f87c302f): feat: vote system

### Patch Changes

- [`a0198b6`](https://github.com/capawesome-team/capacitor-mlkit/commit/a0198b6844a7200ccc6a3303700a30f43b680293) ([#188](https://github.com/capawesome-team/capacitor-mlkit/pull/188)): fix(ios): add delay before starting camera session

## 6.1.0

### Minor Changes
Expand Down
30 changes: 30 additions & 0 deletions packages/barcode-scanning/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,7 @@ If you can't see the camera view, make sure all elements in the DOM are not visi
* [`checkPermissions()`](#checkpermissions)
* [`requestPermissions()`](#requestpermissions)
* [`addListener('barcodeScanned', ...)`](#addlistenerbarcodescanned)
* [`addListener('barcodesScanned', ...)`](#addlistenerbarcodesscanned)
* [`addListener('scanError', ...)`](#addlistenerscanerror)
* [`addListener('googleBarcodeScannerModuleInstallProgress', ...)`](#addlistenergooglebarcodescannermoduleinstallprogress)
* [`removeAllListeners()`](#removealllisteners)
Expand Down Expand Up @@ -666,6 +667,28 @@ Available on Android and iOS.
--------------------


### addListener('barcodesScanned', ...)

```typescript
addListener(eventName: 'barcodesScanned', listenerFunc: (event: BarcodesScannedEvent) => void) => Promise<PluginListenerHandle>
```

Called when barcodes are scanned.

Available on Android and iOS.

| Param | Type |
| ------------------ | ----------------------------------------------------------------------------------------- |
| **`eventName`** | <code>'barcodesScanned'</code> |
| **`listenerFunc`** | <code>(event: <a href="#barcodesscannedevent">BarcodesScannedEvent</a>) =&gt; void</code> |

**Returns:** <code>Promise&lt;<a href="#pluginlistenerhandle">PluginListenerHandle</a>&gt;</code>

**Since:** 6.2.0

--------------------


### addListener('scanError', ...)

```typescript
Expand Down Expand Up @@ -852,6 +875,13 @@ Remove all listeners for this plugin.
| **`barcode`** | <code><a href="#barcode">Barcode</a></code> | A detected barcode. | 0.0.1 |


#### BarcodesScannedEvent

| Prop | Type | Description | Since |
| -------------- | ---------------------- | ---------------------- | ----- |
| **`barcodes`** | <code>Barcode[]</code> | The detected barcodes. | 6.2.0 |


#### ScanErrorEvent

| Prop | Type | Description | Since |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@
import io.capawesome.capacitorjs.plugins.mlkit.barcodescanning.classes.results.GetMaxZoomRatioResult;
import io.capawesome.capacitorjs.plugins.mlkit.barcodescanning.classes.results.GetMinZoomRatioResult;
import io.capawesome.capacitorjs.plugins.mlkit.barcodescanning.classes.results.GetZoomRatioResult;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class BarcodeScanner implements ImageAnalysis.Analyzer {

Expand All @@ -70,6 +73,8 @@ public class BarcodeScanner implements ImageAnalysis.Analyzer {
@Nullable
private ModuleInstallProgressListener moduleInstallProgressListener;

private HashMap<String, Integer> barcodeRawValueVotes = new HashMap<String, Integer>();

private boolean isTorchEnabled = false;

public BarcodeScanner(BarcodeScannerPlugin plugin) {
Expand Down Expand Up @@ -135,6 +140,7 @@ public void stopScan() {
camera = null;
barcodeScannerInstance = null;
scanSettings = null;
barcodeRawValueVotes.clear();
}

public void readBarcodesFromImage(String path, ScanSettings scanSettings, ReadBarcodesFromImageResultCallback callback)
Expand Down Expand Up @@ -346,9 +352,13 @@ public void analyze(@NonNull ImageProxy imageProxy) {
// Scanning stopped while processing the image
return;
}
for (Barcode barcode : barcodes) {
List<Barcode> barcodesWithEnoughVotes = voteForBarcodes(barcodes);
for (Barcode barcode : barcodesWithEnoughVotes) {
handleScannedBarcode(barcode, imageSize);
}
if (barcodesWithEnoughVotes.size() > 0) {
handleScannedBarcodes(barcodesWithEnoughVotes.toArray(new Barcode[0]), imageSize);
}
}
)
.addOnFailureListener(
Expand Down Expand Up @@ -403,6 +413,10 @@ private void handleScannedBarcode(Barcode barcode, Point imageSize) {
plugin.notifyBarcodeScannedListener(barcode, imageSize);
}

private void handleScannedBarcodes(Barcode[] barcodes, Point imageSize) {
plugin.notifyBarcodesScannedListener(barcodes, imageSize);
}

private void handleScanError(Exception exception) {
plugin.notifyScanErrorListener(exception.getMessage());
}
Expand All @@ -418,4 +432,29 @@ private GmsBarcodeScannerOptions buildGmsBarcodeScannerOptions(ScanSettings scan
GmsBarcodeScannerOptions options = new GmsBarcodeScannerOptions.Builder().setBarcodeFormats(formats[0], formats).build();
return options;
}

private Integer voteForBarcode(Barcode barcode) {
String rawValue = barcode.getRawValue();
if (rawValue == null) {
return 1;
} else {
if (barcodeRawValueVotes.containsKey(rawValue)) {
barcodeRawValueVotes.put(rawValue, barcodeRawValueVotes.get(rawValue) + 1);
} else {
barcodeRawValueVotes.put(rawValue, 1);
}
return barcodeRawValueVotes.get(rawValue);
}
}

private List<Barcode> voteForBarcodes(List<Barcode> barcodes) {
List<Barcode> barcodesWithEnoughVotes = new ArrayList<>();
for (Barcode barcode : barcodes) {
Integer votes = voteForBarcode(barcode);
if (votes >= 10) {
barcodesWithEnoughVotes.add(barcode);
}
}
return barcodesWithEnoughVotes;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public class BarcodeScannerPlugin extends Plugin {
public static final String CAMERA = "camera";

public static final String BARCODE_SCANNED_EVENT = "barcodeScanned";
public static final String BARCODES_SCANNED_EVENT = "barcodesScanned";
public static final String SCAN_ERROR_EVENT = "scanError";
public static final String GOOGLE_BARCODE_SCANNER_MODULE_INSTALL_PROGRESS_EVENT = "googleBarcodeScannerModuleInstallProgress";
public static final String ERROR_SCAN_CANCELED = "scan canceled.";
Expand Down Expand Up @@ -477,6 +478,23 @@ public void notifyBarcodeScannedListener(Barcode barcode, Point imageSize) {
}
}

public void notifyBarcodesScannedListener(Barcode[] barcodes, Point imageSize) {
try {
Point screenSize = this.getScreenSize();
JSArray barcodesResult = new JSArray();
for (Barcode barcode : barcodes) {
JSObject barcodeResult = BarcodeScannerHelper.createBarcodeResultForBarcode(barcode, imageSize, screenSize);
barcodesResult.put(barcodeResult);
}

JSObject result = new JSObject();
result.put("barcodes", barcodesResult);
notifyListeners(BARCODES_SCANNED_EVENT, result);
} catch (Exception exception) {
Logger.error(TAG, exception.getMessage(), exception);
}
}

public void notifyScanErrorListener(String message) {
try {
JSObject result = new JSObject();
Expand Down
30 changes: 29 additions & 1 deletion packages/barcode-scanning/ios/Plugin/BarcodeScanner.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ typealias MLKitBarcodeScanner = MLKitBarcodeScanning.BarcodeScanner

private var cameraView: BarcodeScannerView?
private var scanCompletionHandler: (([Barcode]?, AVCaptureVideoOrientation?, String?) -> Void)?
private var barcodeRawValueVotes = [String: Int]()

init(plugin: BarcodeScannerPlugin) {
self.plugin = plugin
Expand Down Expand Up @@ -50,6 +51,7 @@ typealias MLKitBarcodeScanner = MLKitBarcodeScanning.BarcodeScanner
self.cameraView = nil
}
self.scanCompletionHandler = nil
self.barcodeRawValueVotes.removeAll()
}

@objc public func readBarcodesFromImage(imageUrl: URL, settings: ScanSettings, completion: @escaping ([Barcode]?, String?) -> Void) {
Expand Down Expand Up @@ -257,6 +259,28 @@ typealias MLKitBarcodeScanner = MLKitBarcodeScanning.BarcodeScanner
private func handleScannedBarcode(barcode: Barcode, imageSize: CGSize, videoOrientation: AVCaptureVideoOrientation?) {
plugin.notifyBarcodeScannedListener(barcode: barcode, imageSize: imageSize, videoOrientation: videoOrientation)
}

private func handleScannedBarcodes(barcodes: [Barcode], imageSize: CGSize, videoOrientation: AVCaptureVideoOrientation?) {
plugin.notifyBarcodesScannedListener(barcodes: barcodes, imageSize: imageSize, videoOrientation: videoOrientation)
}

private func voteForBarcode(barcode: Barcode) -> Int {
guard let rawValue = barcode.rawValue else {
return 1
}
if let votes = self.barcodeRawValueVotes[rawValue] {
self.barcodeRawValueVotes[rawValue] = votes + 1
} else {
self.barcodeRawValueVotes[rawValue] = 1
}
return self.barcodeRawValueVotes[rawValue] ?? 1
}

private func voteForBarcodes(barcodes: [Barcode]) -> [Barcode] {
return barcodes.filter { barcode in
return self.voteForBarcode(barcode: barcode) >= 10
}
}
}

extension BarcodeScanner: BarcodeScannerViewDelegate {
Expand All @@ -265,9 +289,13 @@ extension BarcodeScanner: BarcodeScannerViewDelegate {
scanCompletionHandler(barcodes, videoOrientation, nil)
self.stopScan()
} else {
for barcode in barcodes {
let barcodesWithEnoughVotes = self.voteForBarcodes(barcodes: barcodes)
for barcode in barcodesWithEnoughVotes {
self.handleScannedBarcode(barcode: barcode, imageSize: imageSize, videoOrientation: videoOrientation)
}
if barcodesWithEnoughVotes.count > 0 {
self.handleScannedBarcodes(barcodes: barcodesWithEnoughVotes, imageSize: imageSize, videoOrientation: videoOrientation)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,12 @@ public class BarcodeScannerHelper {
case .portrait, .portraitUpsideDown:
x = ((imageHeight - cornerPoint.cgPointValue.y) * scale) - (invisibleWidth / 2)
y = (cornerPoint.cgPointValue.x * scale) - (invisibleHeight / 2)
break
case .landscapeLeft:
case .landscapeLeft:
x = ((imageHeight - cornerPoint.cgPointValue.x) * scale) - (invisibleWidth / 2)
y = ((imageWidth - cornerPoint.cgPointValue.y) * scale) - (invisibleHeight / 2)
break
default:
default:
x = (cornerPoint.cgPointValue.x * scale) - (invisibleWidth / 2)
y = (cornerPoint.cgPointValue.y * scale) - (invisibleHeight / 2)
break
}
let point = CGPoint(x: Int(x), y: Int(y))
let value = NSValue(cgPoint: point)
Expand Down
11 changes: 11 additions & 0 deletions packages/barcode-scanning/ios/Plugin/BarcodeScannerPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public class BarcodeScannerPlugin: CAPPlugin {
public let errorPermissionDenied = "User denied access to camera."
public let errorOpenSettingsFailed = "Cannot open settings."
public let barcodeScannedEvent = "barcodeScanned"
public let barcodesScannedEvent = "barcodesScanned"

private var implementation: BarcodeScanner?

Expand Down Expand Up @@ -239,6 +240,16 @@ public class BarcodeScannerPlugin: CAPPlugin {
result["barcode"] = BarcodeScannerHelper.createBarcodeResultForBarcode(barcode, imageSize: imageSize, videoOrientation: videoOrientation)
notifyListeners(barcodeScannedEvent, data: result)
}

func notifyBarcodesScannedListener(barcodes: [Barcode], imageSize: CGSize, videoOrientation: AVCaptureVideoOrientation?) {
var barcodesResult = JSArray()
for barcode in barcodes {
barcodesResult.append(BarcodeScannerHelper.createBarcodeResultForBarcode(barcode, imageSize: imageSize, videoOrientation: videoOrientation))
}
var result = JSObject()
result["barcodes"] = barcodesResult
notifyListeners(barcodesScannedEvent, data: result)
}
}

extension AVAuthorizationStatus {
Expand Down
2 changes: 1 addition & 1 deletion packages/barcode-scanning/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@capacitor-mlkit/barcode-scanning",
"version": "6.1.0",
"version": "6.2.0",
"description": "Capacitor plugin for ML Kit Barcode Scanning.",
"main": "dist/plugin.cjs.js",
"module": "dist/esm/index.js",
Expand Down
24 changes: 24 additions & 0 deletions packages/barcode-scanning/src/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,11 +179,23 @@ export interface BarcodeScannerPlugin {
* Available on Android and iOS.
*
* @since 0.0.1
* @deprecated Use the `barcodesScanned` event listener instead.
*/
addListener(
eventName: 'barcodeScanned',
listenerFunc: (event: BarcodeScannedEvent) => void,
): Promise<PluginListenerHandle>;
/**
* Called when barcodes are scanned.
*
* Available on Android and iOS.
*
* @since 6.2.0
*/
addListener(
eventName: 'barcodesScanned',
listenerFunc: (event: BarcodesScannedEvent) => void,
): Promise<PluginListenerHandle>;
/**
* Called when an error occurs during the scan.
*
Expand Down Expand Up @@ -417,6 +429,18 @@ export interface BarcodeScannedEvent {
barcode: Barcode;
}

/**
* @since 6.2.0
*/
export interface BarcodesScannedEvent {
/**
* The detected barcodes.
*
* @since 6.2.0
*/
barcodes: Barcode[];
}

/**
* @since 0.0.1
*/
Expand Down
2 changes: 2 additions & 0 deletions packages/face-detection/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Changelog

## 6.2.0

## 6.1.0

### Minor Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/face-detection/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@capacitor-mlkit/face-detection",
"version": "6.1.0",
"version": "6.2.0",
"description": "Capacitor plugin for ML Kit Face Detection.",
"main": "dist/plugin.cjs.js",
"module": "dist/esm/index.js",
Expand Down
2 changes: 2 additions & 0 deletions packages/face-mesh-detection/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Changelog

## 6.2.0

## 6.1.0

## 6.0.0
Expand Down
2 changes: 1 addition & 1 deletion packages/face-mesh-detection/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@capacitor-mlkit/face-mesh-detection",
"version": "6.1.0",
"version": "6.2.0",
"description": "Capacitor plugin for ML Kit Face Mesh Detection.",
"main": "dist/plugin.cjs.js",
"module": "dist/esm/index.js",
Expand Down
Loading

0 comments on commit 26891e1

Please sign in to comment.