Skip to content
This repository was archived by the owner on Apr 4, 2023. It is now read-only.

Commit fe411b8

Browse files
MLKitBarcodeScanner - iOS crash based on processEveryNthFrame rate #1190
1 parent b5c0d14 commit fe411b8

File tree

2 files changed

+40
-28
lines changed

2 files changed

+40
-28
lines changed

src/mlkit/custommodel/index.ios.ts

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,7 @@ export class MLKitCustomModel extends MLKitCustomModelBase {
1818
return this.modelInterpreter;
1919
}
2020

21-
runDetector(image: UIImage): void {
22-
if (this.detectorBusy) {
23-
return;
24-
}
25-
26-
this.detectorBusy = true;
27-
21+
runDetector(image: UIImage, onComplete: () => void): void {
2822
const modelExpectsWidth = this.modelInputShape[1];
2923
const modelExpectsHeight = this.modelInputShape[2];
3024
const isQuantized = this.modelInputType !== "FLOAT32";
@@ -63,20 +57,20 @@ export class MLKitCustomModel extends MLKitCustomModelBase {
6357

6458
if (this.labels.length !== probabilities.count) {
6559
console.log(`The number of labels (${this.labels.length}) is not equal to the interpretation result (${probabilities.count})!`);
66-
return;
67-
}
68-
69-
const result = <MLKitCustomModelResult>{
70-
result: getSortedResult(this.labels, probabilities, this.maxResults)
71-
};
60+
onComplete();
61+
} else {
62+
const result = <MLKitCustomModelResult>{
63+
result: getSortedResult(this.labels, probabilities, this.maxResults)
64+
};
7265

73-
this.notify({
74-
eventName: MLKitCustomModel.scanResultEvent,
75-
object: this,
76-
value: result
77-
});
66+
this.notify({
67+
eventName: MLKitCustomModel.scanResultEvent,
68+
object: this,
69+
value: result
70+
});
71+
}
7872
}
79-
this.detectorBusy = false;
73+
onComplete();
8074
})
8175
}
8276

src/mlkit/mlkit-cameraview.ios.ts

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ export abstract class MLKitCameraView extends MLKitCameraViewBase {
176176

177177
abstract createSuccessListener(): any;
178178

179-
runDetector(image: UIImage) {
179+
runDetector(image: UIImage, onComplete: () => void) {
180180
throw new Error("No custom detector implemented, so 'runDetector' can't do its thing");
181181
}
182182
}
@@ -190,6 +190,8 @@ class TNSMLKitCameraViewDelegateImpl extends NSObject implements TNSMLKitCameraV
190190
private detector: any;
191191
private onSuccessListener: any;
192192

193+
private detectorBusy = false;
194+
193195
public static createWithOwnerResultCallbackAndOptions(owner: WeakRef<MLKitCameraView>, callback: (message: any) => void, options?: any): TNSMLKitCameraViewDelegateImpl {
194196
// defer initialisation because the framework may not be available / used
195197
if (TNSMLKitCameraViewDelegateImpl.ObjCProtocols.length === 0 && typeof (TNSMLKitCameraViewDelegate) !== "undefined") {
@@ -205,14 +207,30 @@ class TNSMLKitCameraViewDelegateImpl extends NSObject implements TNSMLKitCameraV
205207
}
206208

207209
cameraDidOutputImage(image: UIImage): void {
208-
if (image) {
209-
if (this.detector.detectInImageCompletion) {
210-
this.detector.detectInImageCompletion(this.uiImageToFIRVisionImage(image), this.onSuccessListener);
211-
} else if (this.detector.processImageCompletion) {
212-
this.detector.processImageCompletion(this.uiImageToFIRVisionImage(image), this.onSuccessListener);
213-
} else {
214-
this.owner.get().runDetector(image);
215-
}
210+
if (!image || this.detectorBusy) {
211+
return;
212+
}
213+
214+
this.detectorBusy = true;
215+
216+
const onComplete = () => {
217+
this.detectorBusy = false;
218+
};
219+
220+
if (this.detector.detectInImageCompletion) {
221+
this.detector.detectInImageCompletion(this.uiImageToFIRVisionImage(image), (result, error) => {
222+
this.onSuccessListener(result, error);
223+
onComplete();
224+
});
225+
226+
} else if (this.detector.processImageCompletion) {
227+
this.detector.processImageCompletion(this.uiImageToFIRVisionImage(image), (result, error) => {
228+
this.onSuccessListener(result, error);
229+
onComplete();
230+
});
231+
232+
} else {
233+
this.owner.get().runDetector(image, onComplete);
216234
}
217235
}
218236

0 commit comments

Comments
 (0)