Skip to main content

Handling contour detection results | Android Document Scanner

You can handle the contour detection results using ContourDetectorFrameHandler#addResultHandler. It might be useful if you want to guide your user through the snapping process by, for instance, displaying respective icons and status messages.

contourDetectorFrameHandler.addResultHandler(ContourDetectorFrameHandler.ResultHandler { result ->    when (result) {        is FrameHandlerResult.Success -> {            //handle result here result.value.detectionResult        }        is FrameHandlerResult.Failure -> {           //there is a license problem that needs to be handled        }    }    false})
caution

This callback is coming from the worker thread. You need to move execution to the main thread before updating the UI.

On each frame you will get a DetectedFrame object which contains the results of the contour detection. One of the most important fields here is detectionResult which is basically the status of the contour detection. Possible values for this status are:

  • OK - contour detection was successful. Detected contour looks like a valid document. This is a good time to take a picture.
  • OK_BUT_TOO_SMALL - document was detected, but it takes up too small an area in the camera viewport. Quality can be improved by moving the camera closer to the document.
  • OK_BUT_BAD_ANGLES - document was detected, but perspective is wrong (camera is tilted relative to the document). Quality can be improved by holding the camera directly over the document.
  • OK_BUT_BAD_ASPECT_RATIO - document was detected, but it has the wrong rotation relative to the camera sensor. Quality can be improved by rotating the camera by 90 degrees.
  • OK_OFF_CENTER - document was detected, but its center is too far away from the image center.
  • ERROR_TOO_DARK - document was not found, most likely because of bad lighting conditions.
  • ERROR_TOO_NOISY - document was not found, most likely because there is too much background noise (maybe too many other objects on the table, or the background texture is too complex).
  • ERROR_NOTHING_DETECTED - document was not found. The document is probably not in the viewport. Usually it does not makes sense to show any information to the user at this point.