How to Detect Filled Circles (Radio Buttons) from a Scanned Document in Android using Java?

I am working on an Android app that scans a paper document using GmsDocumentScanner. My goal is to detect which circles are filled, similar to radio buttons on a form.

The circles are pre-printed on paper, and the user fills one of them to mark their choice. After scanning the document, I need to detect the marked circle and also the text or icon associate with this. I am sharing a snippet here for better understanding.

This snippet provides clarity on the scanned paper, which should be further processed to detect color markings in the circles.

I tried these steps

  1. Scanned the document using GmsDocumentScanning
  2. Attempted to detect filled circles using pixel intensity analysis
private void recognizeText(Bitmap bitmap) {
    InputImage image = InputImage.fromBitmap(bitmap, 0);
    TextRecognizer recognizer = TextRecognition.getClient(TextRecognizerOptions.DEFAULT_OPTIONS);

    recognizer.process(image)
            .addOnSuccessListener(result -> {
                for (Text.TextBlock block : result.getTextBlocks()) {
                    detectRadioButtons(block, bitmap);
                }
            })
            .addOnFailureListener(e -> Log.e("Error", "Text recognition failed", e));
}

private void detectRadioButtons(Text.TextBlock textBlock, Bitmap bitmap) {
    for (Text.Line line : textBlock.getLines()) {
        Rect boundingBox = line.getBoundingBox();
        if (isRadioButtonChecked(boundingBox, bitmap)) {
            Log.i("Detection", "Checked Radio Button: " + line.getText());
        }
    }
}

private boolean isRadioButtonChecked(Rect rect, Bitmap bitmap) {
    int checkedPixelThreshold = 100;
    int checkedPixels = 0;
    int totalPixels = rect.width() * rect.height();

    for (int x = rect.left; x < rect.right; x++) {
        for (int y = rect.top; y < rect.bottom; y++) {
            int pixel = bitmap.getPixel(x, y);
            if (Color.red(pixel) < checkedPixelThreshold &&
                Color.green(pixel) < checkedPixelThreshold &&
                Color.blue(pixel) < checkedPixelThreshold) {
                checkedPixels++;
            }
        }
    }
    return checkedPixels > (totalPixels * 0.5); // More than 50% of pixels are filled
}

The expected outcome is

Detect the circles on the scanned paper Identify which circle is filled Associate the filled circle with the correct text label or icons

Upvotes: 0

Views: 31

Answers (0)

Related Questions