Endrit Gojani
Endrit Gojani

Reputation: 1

Firebase ML Kit TextRecognizer change the size scan area

I want the camera to detect text only within this rectangle (as shown in the picture)

text detector app exemple

Upvotes: 0

Views: 676

Answers (2)

private fun filterBarcodes(barcodes: List<Barcode>) {
        val filteredBarcodes = mutableListOf<Barcode>()
        barcodes.forEach { barcode ->
            if (barcode.boundingBox.left > 100 && barcode.boundingBox.right < 400 &&
                    barcode.boundingBox.top > 200 && barcode.boundingBox.bottom < 400) {
                filteredBarcodes.add(barcode)
            }
        }
        if (filteredBarcodes.isNotEmpty()) {
            readBarcodeData(filteredBarcodes.first())
        }
    }

Upvotes: 0

Chrisito
Chrisito

Reputation: 524

Please take a look at the ML Kit Material Design Showcase app which gives an example on how to do this. The approach we take here is that we detect all barcodes in view, but only act on barcodes that is in the center of the overlay.

The following snippet from BarcodeProcessor.java:

 for (FirebaseVisionBarcode barcode : results) {
  RectF box = graphicOverlay.translateRect(barcode.getBoundingBox());
  if (box.contains(graphicOverlay.getWidth() / 2f, graphicOverlay.getHeight() / 2f)) {
    barcodeInCenter = barcode;
    break;
  }
}

Alternatively you could do a crop before sending the image through the barcode detector. However, the benefit of sending the whole image is that typical barcodes, they can be detected and decoded long before a user has aligned the barcode with the overlay.

Upvotes: 2

Related Questions