'------------------------------------------- LETTORE BARCODE/QRCODE -----------------------------------
Private Sub CreateDetector (Codes As List)
Dim ctxt As JavaObject
ctxt.InitializeContext
Dim builder As JavaObject
builder.InitializeNewInstance("com/google/android/gms/vision/barcode/BarcodeDetector.Builder".Replace("/", "."), Array(ctxt))
Dim barcodeClass As String = "com/google/android/gms/vision/barcode/Barcode".Replace("/", ".")
Dim barcodeStatic As JavaObject
barcodeStatic.InitializeStatic(barcodeClass)
Dim format As Int
For Each formatName As String In Codes
format = Bit.Or(format, barcodeStatic.GetField(formatName))
Next
builder.RunMethod("setBarcodeFormats", Array(format))
detector = builder.RunMethod("build", Null)
Dim operational As Boolean = detector.RunMethod("isOperational", Null)
If operational = False Then
ToastMessageShow("Failed to create detector", True)
End If
End Sub
Private Sub StopCamera
Capturing = False
If camEx.IsInitialized Then
camEx.Release
End If
End Sub
Private Sub StartCamera
rp.CheckAndRequest(rp.PERMISSION_CAMERA)
Wait For Activity_PermissionResult (Permission As String, Result As Boolean)
If Result = False Then
ToastMessageShow("No permission!", False)
Return
End If
StartCameraShared
camEx.Initialize(qrcodeReader, False, Me, "Camera1")
Wait For Camera1_Ready (Success As Boolean)
If Success Then
camEx.SetContinuousAutoFocus
camEx.CommitParameters
camEx.StartPreview
'Log(camEx.GetSupportedFlashModes)
Sleep(200)
qrcodeReader.Visible = True
Else
ToastMessageShow("Error opening camera", False)
StopCamera
End If
End Sub
Private Sub StartCameraShared
qrcodeReader.Visible = True
Capturing = True
End Sub
Private Sub Camera1_Preview (data() As Byte)
If DateTime.Now > LastPreview + IntervalBetweenPreviewsMs Then
'Dim n As Long = DateTime.Now
Dim frameBuilder As JavaObject
Dim bb As JavaObject
bb = bb.InitializeStatic("java.nio.ByteBuffer").RunMethod("wrap", Array(data))
frameBuilder.InitializeNewInstance("com/google/android/gms/vision/Frame.Builder".Replace("/", "."), Null)
Dim cs As CameraSize = camEx.GetPreviewSize
frameBuilder.RunMethod("setImageData", Array(bb, cs.Width, cs.Height, 842094169))
Dim frame As JavaObject = frameBuilder.RunMethod("build", Null)
Dim SparseArray As JavaObject = detector.RunMethod("detect", Array(frame))
LastPreview = DateTime.Now
Dim Matches As Int = SparseArray.RunMethod("size", Null)
If Matches > 0 Then
StopCamera
Dim Barcode As JavaObject = SparseArray.RunMethod("valueAt", Array(0))
Dim raw As String = Barcode.GetField("rawValue")
QrCodeOK(raw)
End If
End If
End Sub