Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scanbot Xamarin SDK update to v3.10.0 #106

Merged
merged 10 commits into from
Nov 6, 2023
591 changes: 554 additions & 37 deletions Libraries.txt

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,10 @@
<PackageReference Include="Xamarin.Forms" Version="5.0.0.2545" />
<PackageReference Include="Xamarin.Essentials" Version="1.7.4" />
<PackageReference Include="ScanbotSDK.Xamarin.Forms">
<Version>3.9.0</Version>
<Version>3.10.0</Version>
</PackageReference>
<PackageReference Include="ScanbotSDK.Xamarin">
<Version>3.9.0</Version>
<Version>3.10.0</Version>
</PackageReference>
<PackageReference Include="Xamarin.AndroidX.Lifecycle.LiveData">
<Version>2.5.1.1</Version>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ namespace Native.Renderers.Example.Forms.Droid.Renderers
By extending 'ViewRenderer' we specify that we want our custom renderer to target 'BarcodeCameraView' and
override it with our native view, which is a 'FrameLayout' in this case (see layout/barcode_camera_view.xml)
*/
class AndroidBarcodeCameraRenderer : ViewRenderer<BarcodeCameraView, FrameLayout>
class AndroidBarcodeCameraRenderer : ViewRenderer<BarcodeCameraView, FrameLayout>, BarcodePolygonsView.IBarcodeHighlightDelegate, BarcodePolygonsView.IBarcodeAppearanceDelegate
{
bool flashEnabled;
protected FrameLayout cameraLayout;
Expand Down Expand Up @@ -123,12 +123,7 @@ protected override void OnElementChanged(ElementChangedEventArgs<BarcodeCameraVi
}
}

private void OnSelectionOverlayBarcodeClicked(object sender, AndroidBarcode e)
{
var items = new List<AndroidBarcode> { e };
var args = new BarcodeEventArgs(new IO.Scanbot.Sdk.Barcode.Entity.BarcodeScanningResult(items, new Java.Util.Date().Time), null);
OnBarcodeResult(this, args);
}
#region Registered Handlers

public void OnCameraOpened()
{
Expand All @@ -139,40 +134,11 @@ public void OnCameraOpened()
}, 300);
}


private void SetSelectionOverlayConfiguration()
private void OnSelectionOverlayBarcodeClicked(object sender, AndroidBarcode e)
{
if (Element?.OverlayConfiguration?.Enabled == true)
{
// Important to set to 0 when the AR Overlay is used
cameraView.ViewController.BarcodeDetectionInterval = 0;
cameraView.SelectionOverlayController.SetEnabled(Element.OverlayConfiguration.Enabled);
cameraView.SelectionOverlayController.SetTextFormat(Element.OverlayConfiguration.OverlayTextFormat.ToAndroid());
cameraView.SelectionOverlayController.SetPolygonColor(Element.OverlayConfiguration.PolygonColor.ToAndroid());
cameraView.SelectionOverlayController.SetTextColor(Element.OverlayConfiguration.TextColor.ToAndroid());
cameraView.SelectionOverlayController.SetTextContainerColor(Element.OverlayConfiguration.TextContainerColor.ToAndroid());

if (Element.OverlayConfiguration.HighlightedPolygonColor != null)
{
cameraView.SelectionOverlayController.SetPolygonHighlightedColor(Element.OverlayConfiguration.HighlightedPolygonColor.Value.ToAndroid());
}

if (Element.OverlayConfiguration.HighlightedTextColor != null)
{
cameraView.SelectionOverlayController.SetTextHighlightedColor(Element.OverlayConfiguration.HighlightedTextColor.Value.ToAndroid());
}

if (Element.OverlayConfiguration.HighlightedTextContainerColor != null)
{
cameraView.SelectionOverlayController.SetTextContainerHighlightedColor(Element.OverlayConfiguration.HighlightedTextContainerColor.Value.ToAndroid());
}

// if selection overlay is set than we get barcode results from it's own OnSelectionOverlayBarcodeClicked handler. So we don't need below result handler.
if (resultHandler != null)
{
resultHandler.Success -= OnBarcodeResult;
}
}
var items = new List<AndroidBarcode> { e };
var args = new BarcodeEventArgs(new IO.Scanbot.Sdk.Barcode.Entity.BarcodeScanningResult(items, new Java.Util.Date().Time), null);
OnBarcodeResult(this, args);
}

private void OnBarcodeResult(object sender, BarcodeEventArgs e)
Expand All @@ -194,6 +160,8 @@ private void OnBarcodeResult(object sender, BarcodeEventArgs e)
}
}

#endregion

private void CheckPermissions()
{
if (Context == null || Context.GetActivity() == null)
Expand All @@ -208,6 +176,98 @@ private void CheckPermissions()
ActivityCompat.RequestPermissions(activity, new string[] { Manifest.Permission.Camera }, REQUEST_PERMISSION_CODE);
}
}

#region Overlay Configuration

private void SetSelectionOverlayConfiguration()
{
if (Element?.OverlayConfiguration?.Enabled == true)
{
cameraView.SelectionOverlayController.SetEnabled(Element.OverlayConfiguration.Enabled);
cameraView.SelectionOverlayController.SetBarcodeHighlightedDelegate(this);
cameraView.SelectionOverlayController.SetBarcodeAppearanceDelegate(this);

if (resultHandler != null)
{
resultHandler.Success -= OnBarcodeResult;
}
}
}

public bool ShouldHighlight(AndroidBarcode barcodeItem)
{
return Element?.OverlayConfiguration?.AutomaticSelectionEnabled ?? false;
}

public BarcodePolygonsView.BarcodePolygonStyle GetPolygonStyle(BarcodePolygonsView.BarcodePolygonStyle defaultStyle, AndroidBarcode barcodeItem)
{
return GetOverlayPolygonStyle(defaultStyle);
}

public BarcodePolygonsView.BarcodeTextViewStyle GetTextViewStyle(BarcodePolygonsView.BarcodeTextViewStyle defaultStyle, AndroidBarcode barcodeItem)
{
return GetOverlayTextStyle(defaultStyle);
}

private BarcodePolygonsView.BarcodePolygonStyle GetOverlayPolygonStyle(BarcodePolygonsView.BarcodePolygonStyle defaultStyle)
{
if (Element.OverlayConfiguration != null)
{
var polygonColor = Element.OverlayConfiguration.PolygonColor.ToAndroid();
var polygonHighlightedColor = defaultStyle.StrokeHighlightedColor;
if (Element.OverlayConfiguration.HighlightedPolygonColor != null)
{
polygonHighlightedColor = Element.OverlayConfiguration.HighlightedPolygonColor.Value.ToAndroid();
}

var polygonStyle = new BarcodePolygonsView.BarcodePolygonStyle(drawPolygon: defaultStyle.DrawPolygon,
useFill: false, // default fill is true. Please set true if you want to fill color into the barcode polygon.
useFillHighlighted:defaultStyle.UseFillHighlighted,
cornerRadius: defaultStyle.CornerRadius,
strokeWidth: defaultStyle.StrokeWidth,
strokeColor: polygonColor,
strokeHighlightedColor: polygonHighlightedColor,
fillColor: defaultStyle.FillColor,
fillHighlightedColor: defaultStyle.FillHighlightedColor,
shouldDrawShadows: defaultStyle.ShouldDrawShadows);

return polygonStyle;
}
return defaultStyle;
}

private BarcodePolygonsView.BarcodeTextViewStyle GetOverlayTextStyle(BarcodePolygonsView.BarcodeTextViewStyle defaultStyle)
{
if (Element.OverlayConfiguration != null)
{
var textColor = Element.OverlayConfiguration.TextColor.ToAndroid();
var textContainerColor = Element.OverlayConfiguration.TextContainerColor.ToAndroid();
var textFormat = Element.OverlayConfiguration.OverlayTextFormat.ToAndroid();

var textHighlightedColor = defaultStyle.TextHighlightedColor;
if (Element.OverlayConfiguration.HighlightedTextColor != null)
{
textHighlightedColor = Element.OverlayConfiguration.HighlightedTextColor.Value.ToAndroid();
}

var textContainerHighlightedColor = defaultStyle.TextContainerHighlightedColor;
if (Element.OverlayConfiguration.HighlightedTextContainerColor != null)
{
textContainerHighlightedColor = Element.OverlayConfiguration.HighlightedTextContainerColor.Value.ToAndroid();
}

var textStyle = new BarcodePolygonsView.BarcodeTextViewStyle(
textColor: textColor,
textHighlightedColor: textHighlightedColor,
textContainerColor: textContainerColor,
textContainerHighlightedColor: textContainerHighlightedColor,
textFormat: textFormat);
return textStyle;
}
return defaultStyle;
}

#endregion
}

public static class Extension
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<string>UIInterfaceOrientationLandscapeRight</string>
</array>
<key>MinimumOSVersion</key>
<string>9.0</string>
<string>13.0</string>
<key>CFBundleDisplayName</key>
<string>Native.Renderers.Example.Forms</string>
<key>CFBundleIdentifier</key>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<MtouchArch>x86_64</MtouchArch>
<MtouchLink>SdkOnly</MtouchLink>
<MtouchLink>None</MtouchLink>
<MtouchDebug>true</MtouchDebug>
<CodesignProvision>Automatic</CodesignProvision>
<CodesignKey>iPhone Developer</CodesignKey>
Expand Down Expand Up @@ -136,10 +136,10 @@
<PackageReference Include="Xamarin.Forms" Version="5.0.0.2545" />
<PackageReference Include="Xamarin.Essentials" Version="1.7.4" />
<PackageReference Include="ScanbotSDK.Xamarin">
<Version>3.9.0</Version>
<Version>3.10.0</Version>
</PackageReference>
<PackageReference Include="ScanbotSDK.Xamarin.Forms">
<Version>3.9.0</Version>
<Version>3.10.0</Version>
</PackageReference>
</ItemGroup>
<Import Project="$(MSBuildExtensionsPath)\Xamarin\iOS\Xamarin.iOS.CSharp.targets" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,21 +103,34 @@ public void Initialize(UIViewController parentViewController)
Controller.AcceptedBarcodeTypes = SBSDKBarcodeType.AllTypes;
}

internal void SetSelectionOverlayConfiguration(SelectionOverlayConfiguration config)
internal void SetSelectionOverlayConfiguration(SelectionOverlayConfiguration configuration)
{
if (config != null && config.Enabled)
if (configuration != null && configuration.Enabled)
{
Controller.SelectionOverlayEnabled = config.Enabled;
Controller.AutomaticSelectionEnabled = config.AutomaticSelectionEnabled;
Controller.SelectionOverlayTextFormat = config.OverlayTextFormat.ToNative();

Controller.SelectionPolygonColor = config.PolygonColor.ToUIColor();
Controller.SelectionTextColor = config.TextColor.ToUIColor();
Controller.SelectionTextContainerColor = config.TextContainerColor.ToUIColor();

Controller.SelectionHighlightedPolygonColor = config.HighlightedPolygonColor?.ToUIColor();
Controller.SelectionHighlightedTextColor = config.HighlightedTextColor?.ToUIColor();
Controller.SelectionHighlightedTextColor = config.HighlightedTextContainerColor?.ToUIColor();
var overlayConfiguration = new SBSDKBarcodeTrackingOverlayConfiguration();

var polygonStyle = new SBSDKBarcodeTrackedViewPolygonStyle();
polygonStyle.PolygonColor = configuration.PolygonColor.ToUIColor();
polygonStyle.PolygonSelectedColor = configuration.HighlightedPolygonColor?.ToUIColor();

// use below properties if you want to set background color to the polygon. As of now they are set to clear
// eg: to show translucent color over barcode.
polygonStyle.PolygonBackgroundColor = UIColor.Clear;
polygonStyle.PolygonBackgroundSelectedColor = UIColor.Clear;

var textStyle = new SBSDKBarcodeTrackedViewTextStyle();
textStyle.TrackingOverlayTextFormat = configuration.OverlayTextFormat.ToNative();
textStyle.TextColor = configuration.TextColor.ToUIColor();
textStyle.SelectedTextColor = configuration.HighlightedTextColor?.ToUIColor();
textStyle.TextBackgroundColor = configuration.TextContainerColor.ToUIColor();
textStyle.TextBackgroundSelectedColor= configuration.HighlightedTextContainerColor?.ToUIColor();

overlayConfiguration.IsAutomaticSelectionEnabled = configuration.AutomaticSelectionEnabled;
overlayConfiguration.TextStyle = textStyle;
overlayConfiguration.PolygonStyle = polygonStyle;

Controller.IsTrackingOverlayEnabled = configuration.Enabled;
Controller.TrackingOverlayController.Configuration = overlayConfiguration;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,11 @@ private void SetupViews()
resultsLabel.Text = text;
});
};
cameraView.OverlayConfiguration = new SelectionOverlayConfiguration(true, OverlayFormat.CodeAndType, Color.Yellow, Color.Yellow, Color.Black, Color.Red, Color.Red, Color.Black);

cameraView.OverlayConfiguration = new SelectionOverlayConfiguration(
automaticSelectionEnabled: false, overlayFormat: OverlayFormat.Code,
polygon: Color.Black, text: Color.Black, textContainer: Color.White,
highlightedPolygonColor: Color.Red, highlightedTextColor: Color.Red, highlightedTextContainerColor: Color.Black);
}

protected override void OnAppearing()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<ItemGroup>
<PackageReference Include="Xamarin.Forms" Version="5.0.0.2545" />
<PackageReference Include="Xamarin.Essentials" Version="1.7.4" />
<PackageReference Include="ScanbotSDK.Xamarin.Forms" Version="3.9.0" />
<PackageReference Include="ScanbotSDK.Xamarin.Forms" Version="3.10.0" />
</ItemGroup>
<ItemGroup>
<Folder Include="Views\" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
<Version>2.5.0</Version>
</PackageReference>
<PackageReference Include="ScanbotSDK.Xamarin.Forms">
<Version>3.9.0</Version>
<Version>3.10.0</Version>
</PackageReference>
<PackageReference Include="Xamarin.Google.Android.Material">
<Version>1.7.0.1</Version>
Expand Down
2 changes: 1 addition & 1 deletion Scanbot.SDK.Example.Forms.iOS/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<string>UIInterfaceOrientationLandscapeRight</string>
</array>
<key>MinimumOSVersion</key>
<string>14.0</string>
<string>13.0</string>
<key>CFBundleDisplayName</key>
<string>Scanbot.SDK.Example.Forms</string>
<key>CFBundleVersion</key>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@
<Version>5.0.0.2545</Version>
</PackageReference>
<PackageReference Include="ScanbotSDK.Xamarin.Forms">
<Version>3.9.0</Version>
<Version>3.10.0</Version>
</PackageReference>
</ItemGroup>
<ItemGroup>
Expand Down
33 changes: 32 additions & 1 deletion Scanbot.SDK.Example.Forms/MainPage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public MainPage()
ViewUtils.CreateCell("Generic Document Recognizer", GenericDocumentRecognizerClicked),
ViewUtils.CreateCell("Check Recognizer", CheckRecognizerClicked),
ViewUtils.CreateCell("Text Data Scanner", TextDataScannerClicked),
ViewUtils.CreateCell("Vin Data Scanner", VinDataScannerClicked),
});
table.Root.Add(new TableSection("MISCELLANEOUS")
{
Expand Down Expand Up @@ -128,7 +129,7 @@ async void BarcodeScannerClicked(object sender, EventArgs e)

var config = new BarcodeScannerConfiguration();
config.BarcodeFormats = BarcodeTypes.Instance.AcceptedTypes;
config.BarcodeImageGenerationType = BarcodeImageGenerationType.FromVideoFrame;
config.ConfirmationDialogConfiguration = GetConfirmationDialog();
config.OverlayConfiguration = GetSelectionOverlayConfig();
var result = await SBSDK.UI.LaunchBarcodeScannerAsync(config);
if (result.Status == OperationResult.Ok)
Expand Down Expand Up @@ -333,5 +334,35 @@ private SelectionOverlayConfiguration GetSelectionOverlayConfig()
Color.Red, Color.Red, Color.Black);
return config;
}

private async void VinDataScannerClicked(object sender, EventArgs e)
{
var configuration = new VINScannerConfiguration();
configuration.FinderAspectRatio = new AspectRatio(7, 1);
configuration.GuidanceText = "Please place the number inside finder area.";
var result = await SBSDK.UI.LaunchVINScannerAsync(configuration);
if (result?.Status == OperationResult.Ok && result?.ValidationSuccessful == true)
{
var message = result.Text;
ViewUtils.Alert(this, "VIN Scanner result", message);
}
}

/// <summary>
/// Init the Confirmation Dialog.
/// </summary>
/// <returns></returns>
private BarcodeConfirmationDialogConfiguration GetConfirmationDialog()
{
return new BarcodeConfirmationDialogConfiguration
{
Title = "Confirmation Dialog",
Message = "Your barcode is scanned.",
ConfirmButtonTitle = "Confirm",
RetryButtonTitle = "Retry",
DialogTextFormat = BarcodeDialogFormat.TypeAndCode,
ResultWithConfirmation = true,
};
}
}
}
4 changes: 2 additions & 2 deletions Scanbot.SDK.Example.Forms/Utils/SDKUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ public static string ParseMRZResult(MrzScannerResult result)
{
var builder = new StringBuilder();
builder.AppendLine($"DocumentType: {result.DocumentType}");
foreach (var field in result.Fields)
foreach (var field in result.Document.Fields)
{
builder.AppendLine($"{field.Name}: {field.Value} ({field.Confidence:F2})");
builder.AppendLine($"{field.Type.Name}: {field.Value.Text} ({field.Value.Confidence:F2})");
}
return builder.ToString();
}
Expand Down