Platforms to show: All Mac Windows Linux Cross-Platform
/AVFoundation/Live Barcode Detection iOS/Live Barcode Detection iOS
Required plugins for this example: MBS Main Plugin, MBS MacBase Plugin, MBS MacCI Plugin, MBS AVFoundation 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 iOS/Live Barcode Detection iOS
This example is the version from Tue, 11th Apr 2022.
Project "Live Barcode Detection iOS.xojo_binary_project"
Class App Inherits MobileApplication
EventHandler Sub Closing()
End EventHandler
EventHandler Sub Opening()
AVFoundation = New MyAVFoundationMBS
End EventHandler
Property AVFoundation As MyAVFoundationMBS
End Class
Class MainScreen Inherits MobileScreen
Control outputCanvas Inherits MobileCanvas
ControlInstance outputCanvas Inherits MobileCanvas
Constraint Constraint 1
Constraint Constraint 2
Constraint Constraint 3
Constraint Constraint 4
EventHandler Sub Paint(g As Graphics)
g.DrawingColor = &cFF0000
g.PenSize = 1
g.DrawRectangle 0, 0, g.Width-1, g.Height-1
End EventHandler
End Control
Control List Inherits iOSMobileTable
ControlInstance List Inherits iOSMobileTable
Constraint Constraint 1
Constraint Constraint 2
Constraint Constraint 3
Constraint Constraint 4
End Control
EventHandler Sub Closing()
app.AVFoundation.Win = Nil
If session<>Nil Then
session.stopRunning
session = Nil
End If
Window = Nil
End EventHandler
EventHandler Sub Opening()
// configure after Xojo made layout
timer.CallLater 0, addressof configure
End EventHandler
EventHandler Sub Resized()
// resize after Xojo made layout
timer.CallLater 0, AddressOf Resize
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
'MessageBox "No device found for video!"
else
// Create a device input with the device and add it to the session
input = AVCaptureDeviceInputMBS.deviceInputWithDevice(device, error)
if error<>Nil then
MessageBox "deviceInputWithDevice: "+error.localizedDescription
else
session.addInputWithNoConnections Input
End If
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
End If
// Attach preview to session
previewViewLayer = CALayerMBS.layerFromHandle(outputCanvas.Handle)
newPreviewLayer = New AVCaptureVideoPreviewLayerMBS(session)
newPreviewLayer.frame = previewViewLayer.frame
newPreviewLayer.BackgroundColor = CGColorMBS.Black
previewViewLayer.BackgroundColor = CGColorMBS.Black
previewViewLayer.addSublayer newPreviewLayer
session.startRunning
End Sub
Private Sub Resize()
If newPreviewLayer <> Nil Then
// resize our preview with the output Canvas:
newPreviewLayer.frame = previewViewLayer.frame
End If
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
Class LaunchScreen Inherits iosView
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 iOSMobileTable = win.list
list.RemoveAllRows
dim cifeatures() as variant = features
for each cifeature as CIFeatureMBS in cifeatures
list.AddRow cifeature.type
dim bounds as CGRectMBS = cifeature.bounds
list.AddRow _
bounds.Left.ToString + "/" + _
bounds.Top.ToString + "-" + _
bounds.Width.ToString + "/"+ _
bounds.Height.ToString
if cifeature isa CIQRCodeFeatureMBS then
dim f as CIQRCodeFeatureMBS = CIQRCodeFeatureMBS(cifeature)
list.AddRow f.messageString
end if
next
end if
End EventHandler
Property win As MainScreen
End Class
ExternalFile info
End ExternalFile
End Project
See also:
The items on this page are in the following plugins: MBS AVFoundation Plugin.