LEADTOOLS Support
General
LEADTOOLS SDK Examples
HOW TO: Use Leadtools.Camera.Xamarin's AutoCapture Class
Groups: Registered, Tech Support
Posts: 6
Using the CameraView offered by Leadtools.Camera.Xamarin, developers can easily integrate camera support into their apps on both Android and iOS. Packaged alongside CameraView is the AutoCapture class, which offers a suite of helper functions that can automatically detect when it is suitable to take an image from the device’s camera. Developers may find this functionality useful to supplement manual controls: for instance, a user may choose to take a picture by pressing a button, but AutoCapture can also perform this functionality automatically. Alternatively, AutoCapture can be used entirely on its own to easily capture images without requiring explicit user direction.
The snippets that follow outline one way AutoCapture’s functionality can be incorporated into a cross-platform Xamarin app. The snippets work by delineating between three “phases” of capture. In the first phase, no document of interest is detected in the frame. Once a document with text is detected, we move to a second phase where the user is prompted to hold their device still as the camera focuses. If the user fails to maintain a stable image during this phase, we return to the first phase to ensure a high-quality final capture. If the image remains stable for long enough and the camera focuses successfully, we move to the final phase: image capture. An image is captured from the device’s camera; the developer can then proceed to use the image however they please. After capturing, we return to the first phase to capture more images.
MainPage.xaml.cs:
Code:using Leadtools;
using Leadtools.Camera.Xamarin;
using System.Diagnostics;
using System.Timers;
using Xamarin.Forms;
namespace AutoCaptureExample
{
public partial class MainPage : ContentPage
{
private AutoCapture autoCapture;
private bool holding = false;
private bool shouldCheckStability = false;
private bool isCapturing = false;
private bool cameraFocused = false;
private Timer checkHoldStabilityTimer;
private Stopwatch captureStopwatch;
public MainPage()
{
string licString = "";
string key = "";
byte[] licBytes = System.Text.Encoding.UTF8.GetBytes(licString);
RasterSupport.SetLicense(licBytes, key);
InitializeComponent();
autoCapture = new AutoCapture();
autoCapture.CaptureMethod = AutoCapture.AutoCaptureMethod.TextDetection;
checkHoldStabilityTimer = new System.Timers.Timer();
checkHoldStabilityTimer.Interval = 300;
checkHoldStabilityTimer.Elapsed += (s, _e) => { shouldCheckStability = true; };
checkHoldStabilityTimer.AutoReset = true;
checkHoldStabilityTimer.Enabled = true;
captureStopwatch = new Stopwatch();
leadCamera.FrameReceived += FrameReceivedHandler;
leadCamera.FocusCompleted += FocusCompletedHandler;
leadCamera.PictureReceived += PictureReceivedHandler;
}
protected override void OnAppearing()
{
base.OnAppearing();
leadCamera.Camera.FocusMode = FocusMode.Continuous;
}
private void PictureReceivedHandler(FrameHandlerEventArgs e)
{
leadCamera.Camera.FocusMode = FocusMode.Continuous;
cameraFocused = false;
autoCapture.Reset();
holding = false;
isCapturing = false;
shouldCheckStability = false;
Device.BeginInvokeOnMainThread(() =>
{
stabilityLabel.Text = "";
DisplayAlert("Picture taken", "Took a picture", "Ok");
});
e.Image.Dispose();
}
private void FocusCompletedHandler(Leadtools.Camera.Xamarin.FocusEventArgs e)
{
cameraFocused = true;
}
private void FrameReceivedHandler(FrameHandlerEventArgs e)
{
if (e.Image == null) return;
if (!holding)
{
autoCapture.CheckStability(e.Image);
}
if (holding && shouldCheckStability)
{
shouldCheckStability = false;
bool stable = autoCapture.CheckHoldStability(e.Image);
if (!stable)
{
holding = false;
autoCapture.Reset();
captureStopwatch.Reset();
Device.BeginInvokeOnMainThread(() =>
{
stabilityLabel.Text = "";
});
e.Image.Dispose();
return;
}
else if (cameraFocused && captureStopwatch.ElapsedMilliseconds > 1500 && !isCapturing)
{
leadCamera.Camera.TakePicture();
isCapturing = true;
Device.BeginInvokeOnMainThread(() =>
{
stabilityLabel.Text = "Taking picture...";
stabilityLabel.TextColor = Color.Green;
});
}
}
if (autoCapture.IsStable && !holding)
{
holding = true;
captureStopwatch.Reset();
captureStopwatch.Start();
cameraFocused = false;
leadCamera.Camera.FocusMode = FocusMode.Auto;
leadCamera.Camera.Focus();
Device.BeginInvokeOnMainThread(() =>
{
stabilityLabel.Text = "Hold still...";
stabilityLabel.TextColor = Color.DarkOrange;
});
}
e.Image.Dispose();
}
}
}
MainPage.xaml:
Code:<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:AutoCaptureExample"
xmlns:leadtools="clr-namespace:Leadtools.Camera.Xamarin;assembly=Leadtools.Camera.Xamarin"
x:Class="AutoCaptureExample.MainPage">
<StackLayout>
<leadtools:CameraView x:Name="leadCamera" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand"/>
<Label x:Name="stabilityLabel" FontSize="Large" HorizontalTextAlignment="Center"/>
</StackLayout>
</ContentPage>
Attached is a sample project containing the page described in these snippets. When an image is captured, an alert dialog is displayed, but no further action is taken with the image.
Edited by moderator 5 years ago
| Reason: syntax highlighting
Joe Kerrigan
Intern
LEAD Technologies, Inc.

LEADTOOLS Support
General
LEADTOOLS SDK Examples
HOW TO: Use Leadtools.Camera.Xamarin's AutoCapture Class
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.