Capture and Crop a Document in Xamarin

LEADTOOLS Camera Control for Xamarin makes app development easier by managing device-specific nuances for you and giving you access to a buffer for frame-by-frame control. Using this powerful tool allows you to capture, edit, and save video, audio, and image data. Better yet, pass each frame to LEADTOOLS Barcode Recognition or OCR to discern the data the camera is recording.

In this post, we’ll show you how easy it is to scan a document using LEADTOOLS. Typically, a user has to take a very precise picture of the document, checking to make sure it’s not blurry and that all 4 corners are present in the image. With LEADTOOLS, users can take advantage of AutoCapture as well as various other image processing functions to automatically capture the best possible image of the document and crop to just the needed document area.

Below are C# and XML code samples that use the FrameReceived event, AutoCapture class, and PerspectiveDeskewCommand class together to capture the frame with the document in it and crop to the document.

C# Code


AutoCapture autoCapture = new AutoCapture();

public LiveCapturePage()
{
    InitializeComponent();
    leadCamera.CameraOptions.AutoRotateImage = true;
    autoCapture.CaptureMethod = AutoCapture.AutoCaptureMethod.DocumentDetection;
    leadCamera.FrameReceived += LeadCamera_FrameReceived;
}

private void LeadCamera_FrameReceived(Leadtools.Camera.Xamarin.FrameHandlerEventArgs e)
{
    using (RasterImage img = e.Image.Clone())
    {
        if (autoCapture.CheckHoldStability(img))
        {
            leadCamera.FrameReceived -= LeadCamera_FrameReceived;

            RasterImage image = e.Image.Clone();

            PerspectiveDeskewCommand cmd = new PerspectiveDeskewCommand();
            cmd.Run(image);

            // Code here to pass to a DocumentViewer content page or to export to the mobile device.
        }
    }
}

XML


<ContentPage.Content>
    <StackLayout>
        <!--main grid container for everything-->
        <Grid x:Name="mainGrid" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">
            <namespace:CameraView x:Name="leadCamera" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand"/>
        </Grid>
    </StackLayout>
</ContentPage.Content>

For more information on the Xamarin Camera Control, check out our full tutorial!

See for yourself – Get a Free evaluation

Download the LEADTOOLS SDK for free. It’s fully-functional for 60 days and comes with free chat and email support.

Need Assistance? LEAD Is Here For You

Need help while you wait for more blogs and tutorials? Contact our support team for free technical support! For pricing or licensing questions, you can contact our sales team via email or call us at 704-332-5532.

About 

Developer Support Manager

This entry was posted in Document Imaging and tagged , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *