Platforms to show: All Mac Windows Linux Cross-Platform
/AVFoundation/Live Barcode Detection
Required plugins for this example: MBS MacBase Plugin, MBS Main 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
This example is the version from Sat, 21th Jun 2019.
Project "Live Barcode Detection.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
CIDetector = new CIDetectorMBS(CIDetectorMBS.CIDetectorTypeQRCode, CIContext)
BarcodeOutput = new AVCaptureVideoDataOutputMBS(CIDetector)
BarcodeOutput.videoSettings = videoSettings
BarcodeOutput.alwaysDiscardsLateVideoFrames = True
app.AVFoundation.win = self
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 CIDetector As CIDetectorMBS
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 features <> nil then
dim list as listbox = win.list
list.DeleteAllRows
dim cifeatures() as variant = features
for each cifeature as CIFeatureMBS in cifeatures
list.AddRow cifeature.type
dim bounds as CGRectMBS = cifeature.bounds
list.AddRow str(bounds.Left)+"/"+str(bounds.Top)+"-"+str(bounds.Width)+"/"+str(bounds.Height)
if cifeature isa CIQRCodeFeatureMBS then
dim f as CIQRCodeFeatureMBS = CIQRCodeFeatureMBS(cifeature)
list.AddRow f.messageString
end if
next
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
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.