Skip to content

Commit

Permalink
Fix crash when repeating detection (flutter#872)
Browse files Browse the repository at this point in the history
* Fix crash when using multiple queries
  • Loading branch information
bparrishMines authored and kroikie committed Nov 13, 2018
1 parent fe088c3 commit 37c02d9
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,41 @@
import com.google.firebase.ml.vision.barcode.FirebaseVisionBarcodeDetectorOptions;
import com.google.firebase.ml.vision.common.FirebaseVisionImage;
import io.flutter.plugin.common.MethodChannel;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

class BarcodeDetector implements Detector {
public static final BarcodeDetector instance = new BarcodeDetector();
static final BarcodeDetector instance = new BarcodeDetector();

private BarcodeDetector() {}

private FirebaseVisionBarcodeDetector detector;
private Map<String, Object> lastOptions;

@Override
public void handleDetection(
FirebaseVisionImage image, Map<String, Object> options, final MethodChannel.Result result) {

FirebaseVisionBarcodeDetector detector =
FirebaseVision.getInstance().getVisionBarcodeDetector(parseOptions(options));
// Use instantiated detector if the options are the same. Otherwise, close and instantiate new
// options.

if (detector == null) {
lastOptions = options;
detector = FirebaseVision.getInstance().getVisionBarcodeDetector(parseOptions(lastOptions));
} else if (!options.equals(lastOptions)) {
try {
detector.close();
} catch (IOException e) {
result.error("barcodeDetectorIOError", e.getLocalizedMessage(), null);
return;
}

lastOptions = options;
detector = FirebaseVision.getInstance().getVisionBarcodeDetector(parseOptions(lastOptions));
}

detector
.detectInImage(image)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,44 @@
import com.google.firebase.ml.vision.cloud.label.FirebaseVisionCloudLabelDetector;
import com.google.firebase.ml.vision.common.FirebaseVisionImage;
import io.flutter.plugin.common.MethodChannel;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

class CloudLabelDetector implements Detector {
public static final CloudLabelDetector instance = new CloudLabelDetector();
static final CloudLabelDetector instance = new CloudLabelDetector();

private CloudLabelDetector() {}

private FirebaseVisionCloudLabelDetector detector;
private Map<String, Object> lastOptions;

@Override
public void handleDetection(
FirebaseVisionImage image, Map<String, Object> options, final MethodChannel.Result result) {
FirebaseVisionCloudLabelDetector detector =
FirebaseVision.getInstance().getVisionCloudLabelDetector(parseOptions(options));

// Use instantiated detector if the options are the same. Otherwise, close and instantiate new
// options.

if (detector == null) {
lastOptions = options;
detector =
FirebaseVision.getInstance().getVisionCloudLabelDetector(parseOptions(lastOptions));
} else if (!options.equals(lastOptions)) {
try {
detector.close();
} catch (IOException e) {
result.error("cloudLabelDetectorIOError", e.getLocalizedMessage(), null);
return;
}

lastOptions = options;
detector =
FirebaseVision.getInstance().getVisionCloudLabelDetector(parseOptions(lastOptions));
}

detector
.detectInImage(image)
.addOnSuccessListener(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,40 @@
import com.google.firebase.ml.vision.face.FirebaseVisionFaceDetectorOptions;
import com.google.firebase.ml.vision.face.FirebaseVisionFaceLandmark;
import io.flutter.plugin.common.MethodChannel;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

class FaceDetector implements Detector {
public static final FaceDetector instance = new FaceDetector();
static final FaceDetector instance = new FaceDetector();

private FaceDetector() {}

private FirebaseVisionFaceDetector detector;
private Map<String, Object> lastOptions;

@Override
public void handleDetection(
FirebaseVisionImage image, Map<String, Object> options, final MethodChannel.Result result) {

FirebaseVisionFaceDetector detector;
if (options == null) {
detector = FirebaseVision.getInstance().getVisionFaceDetector();
} else {
detector = FirebaseVision.getInstance().getVisionFaceDetector(parseOptions(options));
// Use instantiated detector if the options are the same. Otherwise, close and instantiate new
// options.

if (detector == null) {
lastOptions = options;
detector = FirebaseVision.getInstance().getVisionFaceDetector(parseOptions(lastOptions));
} else if (!options.equals(lastOptions)) {
try {
detector.close();
} catch (IOException e) {
result.error("faceDetectorIOError", e.getLocalizedMessage(), null);
return;
}

lastOptions = options;
detector = FirebaseVision.getInstance().getVisionFaceDetector(parseOptions(lastOptions));
}

detector
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,42 @@
import com.google.firebase.ml.vision.label.FirebaseVisionLabelDetector;
import com.google.firebase.ml.vision.label.FirebaseVisionLabelDetectorOptions;
import io.flutter.plugin.common.MethodChannel;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

class LabelDetector implements Detector {
public static final LabelDetector instance = new LabelDetector();
static final LabelDetector instance = new LabelDetector();

private LabelDetector() {}

private FirebaseVisionLabelDetector detector;
private Map<String, Object> lastOptions;

@Override
public void handleDetection(
FirebaseVisionImage image, Map<String, Object> options, final MethodChannel.Result result) {
FirebaseVisionLabelDetector detector =
FirebaseVision.getInstance().getVisionLabelDetector(parseOptions(options));

// Use instantiated detector if the options are the same. Otherwise, close and instantiate new
// options.

if (detector == null) {
lastOptions = options;
detector = FirebaseVision.getInstance().getVisionLabelDetector(parseOptions(lastOptions));
} else if (!options.equals(lastOptions)) {
try {
detector.close();
} catch (IOException e) {
result.error("labelDetectorIOError", e.getLocalizedMessage(), null);
return;
}

lastOptions = options;
detector = FirebaseVision.getInstance().getVisionLabelDetector(parseOptions(lastOptions));
}

detector
.detectInImage(image)
.addOnSuccessListener(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,42 @@
import com.google.firebase.ml.vision.text.FirebaseVisionTextRecognizer;
import com.google.firebase.ml.vision.text.RecognizedLanguage;
import io.flutter.plugin.common.MethodChannel;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class TextRecognizer implements Detector {
public static final TextRecognizer instance = new TextRecognizer();
static final TextRecognizer instance = new TextRecognizer();

private TextRecognizer() {}

private FirebaseVisionTextRecognizer textRecognizer;
private Map<String, Object> lastOptions;

@Override
public void handleDetection(
FirebaseVisionImage image, Map<String, Object> options, final MethodChannel.Result result) {
FirebaseVisionTextRecognizer textRecognizer =
FirebaseVision.getInstance().getOnDeviceTextRecognizer();

// Use instantiated detector if the options are the same. Otherwise, close and instantiate new
// options.

if (textRecognizer == null) {
lastOptions = options;
textRecognizer = FirebaseVision.getInstance().getOnDeviceTextRecognizer();
} else if (!options.equals(lastOptions)) {
try {
textRecognizer.close();
} catch (IOException e) {
result.error("textRecognizerIOError", e.getLocalizedMessage(), null);
return;
}

lastOptions = options;
textRecognizer = FirebaseVision.getInstance().getOnDeviceTextRecognizer();
}

textRecognizer
.processImage(image)
.addOnSuccessListener(
Expand Down

0 comments on commit 37c02d9

Please sign in to comment.