-
Notifications
You must be signed in to change notification settings - Fork 3
/
MainActivity.cs
132 lines (117 loc) · 4.45 KB
/
MainActivity.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
using Android;
using Android.Content.PM;
using Android.Runtime;
using AndroidX.AppCompat.App;
using AndroidX.Camera.MLKit.Vision;
using AndroidX.Camera.View;
using AndroidX.Core.App;
using AndroidX.Core.Content;
using AndroidX.Core.Util;
using Java.Util;
using Java.Util.Concurrent;
using Xamarin.Google.MLKit.Vision.Barcode.Common;
using Xamarin.Google.MLKit.Vision.BarCode;
using Xamarin.Google.MLKit.Vision.Interfaces;
using Object = Java.Lang.Object;
namespace CameraXMlKit
{
[Activity(Name = "com.android.example.camerax.mlkit.MainActivity", Label = "@string/app_name", Theme = "@style/AppTheme", MainLauncher = true)]
public class MainActivity : AppCompatActivity,
IConsumer
{
private PreviewView previewView;
private IExecutorService cameraExecutor;
private IBarcodeScanner barcodeScanner;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.activity_main);
// Request camera permissions
if (AllPermissionsGranted())
{
StartCamera();
}
else
{
ActivityCompat.RequestPermissions(
this, RequiredPermissions, RequestCodePermissions
);
}
cameraExecutor = Executors.NewSingleThreadExecutor();
}
public void Accept(Object t)
{
var result = t as MlKitAnalyzer.Result;
var value = result?.GetValue(barcodeScanner);
var barcodeResults = value.JavaCast<ArrayList>();
if (barcodeResults == null ||
barcodeResults.Size() == 0 ||
barcodeResults.Get(0) == null)
{
previewView.Overlay.Clear();
previewView.SetOnTouchListener(null); // no-op
return;
}
var qrCodeViewModel = new QrCodeViewModel(barcodeResults.Get(0) as Barcode);
var qrCodeDrawable = new QrCodeDrawable(qrCodeViewModel);
previewView.SetOnTouchListener(qrCodeViewModel.QrCodeTouchCallback);
previewView.Overlay.Clear();
previewView.Overlay.Add(qrCodeDrawable);
}
private void StartCamera()
{
var cameraController = new LifecycleCameraController(BaseContext);
previewView = FindViewById<PreviewView>(Resource.Id.viewFinder);
var options = new BarcodeScannerOptions.Builder()
.SetBarcodeFormats(Barcode.FormatQrCode)
.Build();
barcodeScanner = BarcodeScanning.GetClient(options);
cameraController.SetImageAnalysisAnalyzer(
ContextCompat.GetMainExecutor(this),
new MlKitAnalyzer(
new List<IDetector> { barcodeScanner },
#pragma warning disable CS0618
CameraController.CoordinateSystemViewReferenced,
#pragma warning restore CS0618
ContextCompat.GetMainExecutor(this),
this
)
);
cameraController.BindToLifecycle(this);
previewView.Controller = cameraController;
}
private bool AllPermissionsGranted() =>
RequiredPermissions.All(it =>
ContextCompat.CheckSelfPermission(
BaseContext, it) == Permission.Granted);
protected override void OnDestroy()
{
base.OnDestroy();
cameraExecutor.Shutdown();
barcodeScanner.Close();
}
private const string Tag = "CameraXMlKit";
private const int RequestCodePermissions = 10;
private static readonly string[] RequiredPermissions =
{ Manifest.Permission.Camera };
public override void OnRequestPermissionsResult(
int requestCode, string[] permissions, Permission[] grantResults)
{
base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == RequestCodePermissions)
{
if (AllPermissionsGranted())
{
StartCamera();
}
else
{
Toast.MakeText(this,
"Permissions not granted by the user.",
ToastLength.Short).Show();
Finish();
}
}
}
}
}