Platforms to show: All Mac Windows Linux Cross-Platform
/AVFoundation/Live Barcode Detection with Vision/Live Barcode Detection with Vision
Required plugins for this example: MBS MacBase Plugin, MBS Main Plugin, MBS MacFrameworks Plugin, MBS AVFoundation Plugin, MBS MacCI Plugin, MBS MacCG Plugin, MBS MacCF Plugin
You find this example project in your Plugins Download as a Xojo project file within the examples folder: /AVFoundation/Live Barcode Detection with Vision/Live Barcode Detection with Vision
This example is the version from Sat, 13th Jan 2023.
Project "Live Barcode Detection with Vision.xojo_binary_project"
Class App Inherits Application
Const kEditClear = "&Löschen"
Const kFileQuit = "Beenden"
Const kFileQuitShortcut = ""
EventHandler Sub Open()
if AVFoundation.available = false then
MsgBox "Please run on Mac OS X 10.7 or newer."
quit
end if
#if Target64Bit and TargetMacOS then
// okay
#else
MsgBox "please run as 64-bit Mac app for best results.
#endif
AVFoundation = new MyAVFoundationMBS
End EventHandler
Property AVFoundation As MyAVFoundationMBS
End Class
Class MainWindow Inherits Window
Control OutputCanvas Inherits Canvas
ControlInstance OutputCanvas Inherits Canvas
End Control
Control List Inherits Listbox
ControlInstance List Inherits Listbox
End Control
EventHandler Sub Close()
app.AVFoundation.Win = nil
if session<>nil then
session.stopRunning
session = nil
end if
window = nil
End EventHandler
EventHandler Sub Open()
configure
End EventHandler
Sub Configure()
// Create a capture session
session = new AVCaptureSessionMBS
// Set the session preset
dim preset as string = AVFoundationMBS.AVCaptureSessionPreset640x480
session.SessionPreset = preset
dim error as NSErrorMBS
// Find video devices
dim device as AVCaptureDeviceMBS = AVCaptureDeviceMBS.defaultDeviceWithMediaType(AVFoundationMBS.AVMediaTypeVideo)
if device = nil then
device = AVCaptureDeviceMBS.defaultDeviceWithMediaType(AVFoundationMBS.AVMediaTypeMuxed)
end if
if device = nil then
MsgBox "No device found for video!"
return
end if
// Create a device input with the device and add it to the session
input = AVCaptureDeviceInputMBS.deviceInputWithDevice(device, error)
if error<>Nil then
MsgBox "deviceInputWithDevice: "+error.localizedDescription
return
end if
session.addInputWithNoConnections input
dim outputSettings as new Dictionary
outputSettings.Value(AVFoundationMBS.AVVideoCodecKey) = AVFoundationMBS.AVVideoCodecJPEG
StillImageOutput = new AVCaptureStillImageOutputMBS
StillImageOutput.outputSettings = outputSettings
session.addOutput StillImageOutput
dim videoSettings as new Dictionary
videoSettings.Value(CVPixelBufferMBS.kCVPixelBufferPixelFormatTypeKey) = CVPixelBufferMBS.kCVPixelFormatType_32ARGB
' e.g. kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange,
' kCVPixelFormatType_420YpCbCr8BiPlanarFullRange or
' kCVPixelFormatType_32BGRA.
CIContext = New CIContextMBS
BarcodeOutput = New AVCaptureVideoDataOutputMBS
BarcodeOutput.videoSettings = videoSettings
BarcodeOutput.alwaysDiscardsLateVideoFrames = True
app.AVFoundation.win = Self
app.AVFoundation.List = Self.List
session.addOutput BarcodeOutput
// Attach preview to session
previewViewLayer = outputCanvas.CALayerMBS
previewViewLayer.BackgroundColor = CGColorMBS.Black
newPreviewLayer = new AVCaptureVideoPreviewLayerMBS(session)
newPreviewLayer.frame = previewViewLayer.bounds
newPreviewLayer.autoresizingMask = newPreviewLayer.kCALayerWidthSizable + newPreviewLayer.kCALayerHeightSizable
previewViewLayer.addSublayer newPreviewLayer
session.startRunning
End Sub
Property BarcodeOutput As AVCaptureVideoDataOutputMBS
Property CIContext As CIContextMBS
Property CurrentImage As NSImageMBS
Property LastTicks As Double
Property StillImageOutput As AVCaptureStillImageOutputMBS
Property currentPicture As Picture
Property devicesCount As Integer
Property input As AVCaptureDeviceInputMBS
Property newPreviewLayer As AVCaptureVideoPreviewLayerMBS
Property previewViewLayer As CALayerMBS
Property session As AVCaptureSessionMBS
Property window As NSWindowMBS
End Class
MenuBar MenuBar1
MenuItem FileMenu = "&Ablage"
MenuItem FileQuit = "#App.kFileQuit"
MenuItem EditMenu = "&Bearbeiten"
MenuItem EditUndo = "&Rückgängig"
MenuItem UntitledMenu1 = "-"
MenuItem EditCut = "&Ausschneiden"
MenuItem EditCopy = "&Kopieren"
MenuItem EditPaste = "&Einfügen"
MenuItem EditClear = "#App.kEditClear"
MenuItem UntitledMenu0 = "-"
MenuItem EditSelectAll = "&Alles auswählen"
End MenuBar
Class PhotoWindow Inherits Window
Control Output Inherits Canvas
ControlInstance Output Inherits Canvas
End Control
End Class
Class MyAVFoundationMBS Inherits AVFoundationMBS
EventHandler Sub captureOutputDidOutputSampleBuffer(captureOutput as AVCaptureOutputMBS, OutputSampleBuffer as CMSampleBufferMBS, connection as AVCaptureConnectionMBS, features as variant)
If win <> Nil And Not RequestRunning Then
Dim ImageBuffer As CVImageBufferMBS = OutputSampleBuffer.ImageBuffer
If barcodeDetectRequest = Nil Then
barcodeDetectRequest = New VNDetectBarcodesRequestMBS(WeakAddressOf RequestCompleted)
// Restrict detection to most common symbologies.
Dim symbologies() As String
symbologies.append VNBarcodeObservationMBS.VNBarcodeSymbologyQR
symbologies.append VNBarcodeObservationMBS.VNBarcodeSymbologyEAN13
symbologies.append VNBarcodeObservationMBS.VNBarcodeSymbologyEAN8
symbologies.append VNBarcodeObservationMBS.VNBarcodeSymbologyUPCE
barcodeDetectRequest.setSymbologies symbologies
End If
Dim requests() As VNRequestMBS
Requests.Append barcodeDetectRequest
imageRequestHandler = VNImageRequestHandlerMBS.RequestWithCVPixelBuffer(ImageBuffer)
imageRequestHandler.performRequestsAsync(requests, WeakAddressOf performRequestsCompleted )
RequestRunning = True
end if
End EventHandler
EventHandler Sub captureStillImageAsynchronouslyCompleted(CaptureStillImageOutput as AVCaptureStillImageOutputMBS, prepareJpegStillImage as boolean, tag as variant, error as NSErrorMBS, imageDataSampleBuffer as CMSampleBufferMBS, JpegStillImage as memoryblock)
dim pic as Picture = Picture.FromData(JpegStillImage)
dim p as new PhotoWindow
p.Backdrop = pic
p.Width = pic.Width
p.Height = pic.Height
p.Left = MainWindow.Left + 20 + MainWindow.Width
p.top = MainWindow.top
p.show
End EventHandler
Sub Log(s as string)
If s = last Then Return // ignore duplicate
List.AddRow s
last = s
End Sub
Sub RequestCompleted(request as VNRequestMBS, error as NSErrorMBS, tag as Variant)
Dim list As Listbox = win.List
If error <> Nil Then
Log request.className+" failed: "+error.LocalizedDescription
Else
'Log request.className+" succeeded"
if request <> nil then
Dim results() As VNObservationMBS = request.results
If results.Ubound >= 0 Then
For Each result As VNObservationMBS In results
If result IsA VNBarcodeObservationMBS Then
Dim bresult As VNBarcodeObservationMBS = VNBarcodeObservationMBS(result)
Log bresult.symbology+": "+bresult.payloadStringValue
End If
Next
End If
End If
end if
End Sub
Sub performRequestsCompleted(Requests() as VNRequestMBS, result as boolean, error as NSErrorMBS, tag as Variant)
Dim list As Listbox = win.List
If result Then
'Log "performRequests succeeded."
Else
Log "performRequests failed."
End If
If error <> Nil Then
Log "Error: " + error.LocalizedDescription
End If
RequestRunning = False
End Sub
Property List As Listbox
Property RequestRunning As Boolean
Property barcodeDetectRequest As VNDetectBarcodesRequestMBS
Property imageRequestHandler As VNImageRequestHandlerMBS
Property last As string
Property win As MainWindow
End Class
ExternalFile info
End ExternalFile
End Project
See also:
The items on this page are in the following plugins: MBS AVFoundation Plugin.