Platforms to show: All Mac Windows Linux Cross-Platform

/AVFoundation/Live picture processing


Required plugins for this example: MBS MacBase Plugin, MBS AVFoundation Plugin, MBS MacCG Plugin, MBS MacCF Plugin, MBS Main Plugin

You find this example project in your Plugins Download as a Xojo project file within the examples folder: /AVFoundation/Live picture processing

This example is the version from Sat, 21th Jun 2019.

Project "Live picture processing.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 AVFoundation = new MyAVFoundationMBS End EventHandler
Property AVFoundation As MyAVFoundationMBS
End Class
Class MainWindow Inherits Window
Control OutputCanvas Inherits Canvas
ControlInstance OutputCanvas Inherits Canvas
EventHandler Sub Paint(g As Graphics, areas() As REALbasic.Rect) if currentPicture <> nil then g.DrawPicture currentPicture, 0, 0, g.Width, g.Height, 0, 0, currentPicture.width, currentPicture.height end if End EventHandler
End Control
Control BevelButton1 Inherits BevelButton
ControlInstance BevelButton1 Inherits BevelButton
EventHandler Sub Action() // search the video connection dim videoConnection as AVCaptureConnectionMBS dim connections() as AVCaptureConnectionMBS = stillImageOutput.connections for each connection as AVCaptureConnectionMBS in connections dim inputPorts() as AVCaptureInputPortMBS = connection.inputPorts for each port as AVCaptureInputPortMBS in inputPorts if port.mediaType = AVFoundationMBS.AVMediaTypeVideo then videoConnection = connection exit for connection end if next next StillImageOutput.captureStillImageAsynchronously videoConnection, true, 0 exception m as NSExceptionMBS MsgBox m.message End EventHandler
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 videoSettings.Value(CVPixelBufferMBS.kCVPixelBufferWidthKey) = me.Width videoSettings.Value(CVPixelBufferMBS.kCVPixelBufferHeightKey) = me.Height ' e.g. kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange, ' kCVPixelFormatType_420YpCbCr8BiPlanarFullRange or ' kCVPixelFormatType_32BGRA. LiveImageOutput = New AVCaptureVideoDataOutputMBS LiveImageOutput.videoSettings = videoSettings LiveImageOutput.alwaysDiscardsLateVideoFrames = True app.AVFoundation.win = Self session.addOutput LiveImageOutput // let the plugin do heavy work on preemptive thread AVCaptureVideoDataOutputMBS.PrepareCIImage = true AVCaptureVideoDataOutputMBS.PrepareNSImage = True session.startRunning End Sub
Property CurrentImage As NSImageMBS
Property LastTicks As Double
Property LiveImageOutput As AVCaptureVideoDataOutputMBS
Property StillImageOutput As AVCaptureStillImageOutputMBS
Property currentPicture As Picture
Property devicesCount As Integer
Property input As AVCaptureDeviceInputMBS
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
End Class
Class MyAVFoundationMBS Inherits AVFoundationMBS
EventHandler Sub captureOutputDidOutputSampleBuffer(captureOutput as AVCaptureOutputMBS, OutputSampleBuffer as CMSampleBufferMBS, connection as AVCaptureConnectionMBS, features as variant) if win <> nil then dim ImageBuffer as CVImageBufferMBS = OutputSampleBuffer.ImageBuffer if ImageBuffer <> nil then dim image as NSImageMBS = ImageBuffer.NSImage if image <> nil then // this may request size of window, so image may get reduced in size image.setSize win.Width, win.Height dim currentPicture as Picture = image.CopyPicture'WithAlpha // show in window win.currentImage = image win.currentPicture = currentPicture win.OutputCanvas.Invalidate end if dim now as Double = Microseconds dim delta as Double = (now - win.LastTicks) / 1000000.0 win.LastTicks = now win.Title = Format(1.0/delta, "0.0")+" fps" end if 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.


The biggest plugin in space...