Xojo Conferences
XDCMay2019MiamiUSA

Platforms to show: All Mac Windows Linux Cross-Platform

/AVFoundation/Live picture processing
Function:
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 Sun, 11th Aug 2018.
Project "Live picture processing.rbp"
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 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) 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
End Project

See also:

Feedback, Comments & Corrections

The items on this page are in the following plugins: MBS AVFoundation Plugin.




Links
MBS Xojo tutorial videos