Platforms to show: All Mac Windows Linux Cross-Platform
/MacFrameworks/Image Capture/Scanner Browser
Required plugins for this example: MBS MacOSX Plugin, MBS AVFoundation Plugin, MBS MacCocoa Plugin, MBS MacBase Plugin, MBS Main Plugin, MBS MacCG Plugin, MBS MacCF Plugin, MBS MacControls Plugin
You find this example project in your Plugins Download as a Xojo project file within the examples folder: /MacFrameworks/Image Capture/Scanner Browser
This example is the version from Wed, 15th Jul 2014.
Project "Scanner Browser.xojo_binary_project"
Class App Inherits Application
Const kEditClear = "&Löschen"
Const kFileQuit = "Beenden"
Const kFileQuitShortcut = ""
End Class
Class MainWindow Inherits Window
Control List Inherits Listbox
ControlInstance List Inherits Listbox
EventHandler Function CellTextPaint(g As Graphics, row As Integer, column As Integer, x as Integer, y as Integer) As Boolean
if column = 0 then
dim pic as Picture = me.CellTag(row, column)
if pic<>Nil then
g.DrawPicture pic, 0, 0, 50, 50, 0, 0, pic.Width, pic.Height
Return true
end if
end if
End EventHandler
EventHandler Sub Change()
OpenButton.Enabled = me.ListIndex >= 0
End EventHandler
End Control
Control Label1 Inherits Label
ControlInstance Label1 Inherits Label
End Control
Control Image Inherits ImageWell
ControlInstance Image Inherits ImageWell
End Control
Control PopupMode Inherits PopupMenu
ControlInstance PopupMode Inherits PopupMenu
EventHandler Sub Change()
if me.Enabled and me.ListIndex>=0 then
dim scanner as ICScannerDeviceMBS = self.selectedScanner
dim selectedType as integer = me.RowTag(me.ListIndex)
if selectedType <> scanner.selectedFunctionalUnit.type then
scanner.requestSelectFunctionalUnit selectedType
end if
end if
End EventHandler
End Control
Control Label2 Inherits Label
ControlInstance Label2 Inherits Label
End Control
Control OverviewButton Inherits PushButton
ControlInstance OverviewButton Inherits PushButton
EventHandler Sub Action()
startOverviewScan
End EventHandler
End Control
Control ScanButton Inherits PushButton
ControlInstance ScanButton Inherits PushButton
EventHandler Sub Action()
startscan
End EventHandler
End Control
Control OpenButton Inherits PushButton
ControlInstance OpenButton Inherits PushButton
EventHandler Sub Action()
openCloseSession
End EventHandler
End Control
Control ProgressBar1 Inherits ProgressBar
ControlInstance ProgressBar1 Inherits ProgressBar
End Control
Control StatusMessage Inherits Label
ControlInstance StatusMessage Inherits Label
End Control
Control ProgressTimer Inherits Timer
ControlInstance ProgressTimer Inherits Timer
EventHandler Sub Action()
dim scanner as ICScannerDeviceMBS = selectedScanner
if scanner<>Nil then
dim fu as ICScannerFunctionalUnitMBS = scanner.selectedFunctionalUnit
if fu<>nil then
dim d as Double = fu.scanProgressPercentDone
ProgressBar1.Value = d
end if
end if
End EventHandler
End Control
EventHandler Sub Open()
events = new MyImageCaptureEvents
PopupMode.DeleteAllRows
PopupMode.Enabled = false
DeviceBrowser = new ICDeviceBrowserMBS
DeviceBrowser.browsedDeviceTypeMask = bitwise.BitOr(ICDeviceMBS.ICDeviceLocationTypeMaskLocal, ICDeviceMBS.ICDeviceLocationTypeMaskRemote, ICDeviceMBS.ICDeviceTypeMaskScanner)
DeviceBrowser.start
End EventHandler
Sub AddDevice(scanner as ICScannerDeviceMBS)
Scanners.Append scanner
List.AddRow "", scanner.Name
List.RowTag(List.LastIndex) = scanner
dim image as CGImageMBS = scanner.Icon
dim pic as Picture = image.Picture
List.CellTag(List.LastIndex,0) = pic
End Sub
Sub StartScan()
dim scanner as ICScannerDeviceMBS = self.selectedScanner
dim fu as ICScannerFunctionalUnitMBS = scanner.selectedFunctionalUnit
if ( ( fu.scanInProgress = false ) and ( fu.overviewScanInProgress = false ) ) then
if ( fu.type = ICScannerFunctionalUnitMBS.ICScannerFunctionalUnitTypeDocumentFeeder ) then
dim dfu as ICScannerFunctionalUnitDocumentFeederMBS = ICScannerFunctionalUnitDocumentFeederMBS(fu)
dfu.documentType = ICScannerFunctionalUnitMBS.ICScannerDocumentTypeUSLetter
else
dim s as NSSizeMBS = fu.physicalSize
fu.measurementUnit = ICScannerFunctionalUnitMBS.ICScannerMeasurementUnitInches
fu.scanArea = new NSRectMBS( 0.0, 0.0, s.width, s.height )
end if
fu.resolution = fu.supportedResolutions.indexGreaterThanOrEqualToIndex(100)
fu.bitDepth = ICScannerFunctionalUnitMBS.ICScannerBitDepth8Bits
fu.pixelDataType = ICScannerFunctionalUnitMBS.ICScannerPixelDataTypeRGB
scanner.transferMode = ICScannerDeviceMBS.ICScannerTransferModeFileBased
scanner.downloadsDirectory = SpecialFolder.Desktop.URLPath
scanner.documentName = "Scan"
scanner.documentUTI = UTTypeMBS.kUTTypeJPEG
scanner.requestScan
ProgressBar1.Visible = true
ProgressBar1.Value = 0
ProgressBar1.Maximum = 100
ProgressTimer.mode = 2
else
scanner.cancelScan
end if
End Sub
Sub deviceDidBecomeReady(scanner as ICScannerDeviceMBS)
dim availabeTypes() as integer = scanner.availableFunctionalUnitTypes
dim functionalUnit as ICScannerFunctionalUnitMBS = scanner.selectedFunctionalUnit
PopupMode.Enabled = false
PopupMode.DeleteAllRows
if availabeTypes.Ubound >= 0 then
dim selectedfunctionalUnit as integer = -1
if functionalUnit<>nil then
selectedfunctionalUnit = functionalUnit.type
end if
for each n as integer in availabeTypes
Select case n
case ICScannerFunctionalUnitMBS.ICScannerFunctionalUnitTypeFlatbed
PopupMode.AddRow "Flatbed", ICScannerFunctionalUnitMBS.ICScannerFunctionalUnitTypeFlatbed
if selectedfunctionalUnit = ICScannerFunctionalUnitMBS.ICScannerFunctionalUnitTypeFlatbed then
PopupMode.ListIndex = PopupMode.ListCount-1
end if
case ICScannerFunctionalUnitMBS.ICScannerFunctionalUnitTypePositiveTransparency
PopupMode.AddRow "Postive Transparency", ICScannerFunctionalUnitMBS.ICScannerFunctionalUnitTypePositiveTransparency
if selectedfunctionalUnit = ICScannerFunctionalUnitMBS.ICScannerFunctionalUnitTypePositiveTransparency then
PopupMode.ListIndex = PopupMode.ListCount-1
end if
case ICScannerFunctionalUnitMBS.ICScannerFunctionalUnitTypeNegativeTransparency
PopupMode.AddRow "Negative Transparency", ICScannerFunctionalUnitMBS.ICScannerFunctionalUnitTypeNegativeTransparency
if selectedfunctionalUnit = ICScannerFunctionalUnitMBS.ICScannerFunctionalUnitTypeNegativeTransparency then
PopupMode.ListIndex = PopupMode.ListCount-1
end if
case ICScannerFunctionalUnitMBS.ICScannerFunctionalUnitTypeDocumentFeeder
PopupMode.AddRow "Document Feeder", ICScannerFunctionalUnitMBS.ICScannerFunctionalUnitTypeDocumentFeeder
if selectedfunctionalUnit = ICScannerFunctionalUnitMBS.ICScannerFunctionalUnitTypeDocumentFeeder then
PopupMode.ListIndex = PopupMode.ListCount-1
end if
end select
next
PopupMode.Enabled = true
end if
scanButton.Enabled = true
overviewButton.Enabled = true
End Sub
Sub didCompleteOverviewScan(scanner as ICScannerDeviceMBS)
ProgressBar1.Visible = false
ProgressTimer.mode = 0
if scanner<>Nil then
dim v as Variant = scanner.selectedFunctionalUnit.overviewImage
if v isa CGImageMBS then
dim img as CGImageMBS = v
dim pic as Picture = img.Picture
image.Image = pic.ProportinalScaled(image.Width, image.Height)
Return
end if
end if
image.Image = nil
End Sub
Sub didCompleteScan()
ProgressBar1.Visible = false
ProgressTimer.mode = 0
End Sub
Sub didReceiveStatusInformation(Status as Dictionary)
dim StatusValue as string = status.Lookup(ICDeviceMBS.ICStatusNotificationKey, "")
if StatusValue = ICScannerDeviceMBS.ICScannerStatusWarmingUp then
ProgressBar1.Maximum = 0
ProgressBar1.Value = 0
ProgressTimer.mode = 0
StatusMessage.Text = status.lookup(ICDeviceMBS.ICLocalizedStatusNotificationKey, "?")
ProgressBar1.Visible = True
elseif StatusValue = ICScannerDeviceMBS.ICScannerStatusWarmUpDone then
StatusMessage.Text = ""
ProgressBar1.Value = 0
ProgressBar1.Maximum = 100
ProgressBar1.Visible = True
ProgressTimer.mode = 2
end if
End Sub
Sub didRemoveDevice(d as ICDeviceMBS)
dim u as integer = List.ListCount-1
for i as integer = 0 to u
if list.RowTag(i) = d then
List.RemoveRow i
end if
next
End Sub
Sub openCloseSession()
dim s as ICScannerDeviceMBS = selectedScanner
if s<>nil then
if s.HasOpenSession then
s.requestCloseSession
else
s.requestOpenSession
end if
end if
End Sub
Function selectedScanner() As ICScannerDeviceMBS
if list.listindex >= 0 then
Return List.RowTag(List.ListIndex)
end if
End Function
Sub startOverviewScan()
dim scanner as ICScannerDeviceMBS = self.selectedScanner
dim fu as ICScannerFunctionalUnitMBS = scanner.selectedFunctionalUnit
if ( fu.canPerformOverviewScan and ( fu.scanInProgress = false ) and ( fu.overviewScanInProgress = false ) ) then
fu.overviewResolution = fu.supportedResolutions.indexGreaterThanOrEqualToIndex(72)
scanner.requestOverviewScan
ProgressBar1.Visible = true
ProgressBar1.Value = 0
ProgressBar1.Maximum = 100
ProgressTimer.mode = 2
else
scanner.cancelScan
end if
End Sub
Property DeviceBrowser As ICDeviceBrowserMBS
Property Scanners() As ICScannerDeviceMBS
Property events As MyImageCaptureEvents
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 MyImageCaptureEvents Inherits ImageCaptureEventsMBS
EventHandler Sub deviceBrowserDeviceDidChangeName(browser as ICDeviceBrowserMBS, device as ICDeviceMBS)
System.DebugLog CurrentMethodName
End EventHandler
EventHandler Sub deviceBrowserDeviceDidChangeSharingState(browser as ICDeviceBrowserMBS, device as ICDeviceMBS)
System.DebugLog CurrentMethodName
End EventHandler
EventHandler Sub deviceBrowserDidAddDevice(browser as ICDeviceBrowserMBS, device as ICDeviceMBS, moreComing as boolean)
System.DebugLog CurrentMethodName
if BitwiseAnd(device.type, device.ICDeviceTypeMaskScanner) = device.ICDeviceTypeMaskScanner then
MainWindow.AddDevice ICScannerDeviceMBS(device)
end if
End EventHandler
EventHandler Sub deviceBrowserDidEnumerateLocalDevices(browser as ICDeviceBrowserMBS)
System.DebugLog CurrentMethodName
End EventHandler
EventHandler Sub deviceBrowserDidRemoveDevice(browser as ICDeviceBrowserMBS, device as ICDeviceMBS, moreGoing as boolean)
System.DebugLog CurrentMethodName
End EventHandler
EventHandler Sub deviceBrowserRequestsSelectDevice(browser as ICDeviceBrowserMBS, device as ICDeviceMBS)
System.DebugLog CurrentMethodName
End EventHandler
EventHandler Sub deviceDidBecomeReady(device as ICDeviceMBS)
System.DebugLog CurrentMethodName
if device isa ICScannerDeviceMBS then
MainWindow.deviceDidBecomeReady ICScannerDeviceMBS(Device)
end if
End EventHandler
EventHandler Sub deviceDidChangeName(device as ICDeviceMBS)
System.DebugLog CurrentMethodName
End EventHandler
EventHandler Sub deviceDidChangeSharingState(device as ICDeviceMBS)
System.DebugLog CurrentMethodName
End EventHandler
EventHandler Sub deviceDidCloseSessionWithError(device as ICDeviceMBS, error as NSErrorMBS)
System.DebugLog CurrentMethodName
End EventHandler
EventHandler Sub deviceDidEncounterError(device as ICDeviceMBS, error as NSErrorMBS)
System.DebugLog CurrentMethodName
MsgBox error.LocalizedDescription
End EventHandler
EventHandler Sub deviceDidOpenSessionWithError(device as ICDeviceMBS, error as NSErrorMBS)
System.DebugLog CurrentMethodName
End EventHandler
EventHandler Sub deviceDidReceiveButtonPress(device as ICDeviceMBS, buttonType as String)
System.DebugLog CurrentMethodName
End EventHandler
EventHandler Sub deviceDidReceiveCustomNotification(device as ICDeviceMBS, notification as Dictionary, data as Memoryblock)
System.DebugLog CurrentMethodName
End EventHandler
EventHandler Sub deviceDidReceiveStatusInformation(device as ICDeviceMBS, status as Dictionary)
System.DebugLog CurrentMethodName
MainWindow.didReceiveStatusInformation Status
End EventHandler
EventHandler Sub deviceDidRemove(device as ICDeviceMBS)
System.DebugLog CurrentMethodName
MainWindow.didRemoveDevice device
End EventHandler
EventHandler Sub deviceDidSendMessage(messageCode as UInt32, data as Memoryblock, error as NSErrorMBS, device as ICDeviceMBS)
System.DebugLog CurrentMethodName
End EventHandler
EventHandler Sub scannerDeviceDidBecomeAvailable(scanner as ICScannerDeviceMBS)
System.DebugLog CurrentMethodName
scanner.requestOpenSession
End EventHandler
EventHandler Sub scannerDeviceDidCompleteOverviewScanWithError(scanner as ICScannerDeviceMBS, error as NSErrorMBS)
System.DebugLog CurrentMethodName
MainWindow.didCompleteOverviewScan scanner
End EventHandler
EventHandler Sub scannerDeviceDidCompleteScanWithError(scanner as ICScannerDeviceMBS, error as NSErrorMBS)
System.DebugLog CurrentMethodName
MainWindow.didCompleteScan
End EventHandler
EventHandler Sub scannerDeviceDidScanToBandData(scanner as ICScannerDeviceMBS, Data as ICScannerBandDataMBS)
System.DebugLog CurrentMethodName
End EventHandler
EventHandler Sub scannerDeviceDidScanToURL(scanner as ICScannerDeviceMBS, URL as string, file as folderitem, data as MemoryBlock)
System.DebugLog CurrentMethodName
End EventHandler
EventHandler Sub scannerDeviceDidSelectFunctionalUnit(scanner as ICScannerDeviceMBS, functionalUnit as variant, Error as NSErrorMBS)
System.DebugLog CurrentMethodName
End EventHandler
End Class
Module UtilModule
Sub AddRow(extends p as PopupMenu, text as string, tag as Variant)
p.AddRow text
p.RowTag(p.ListCount-1) = tag
End Sub
Function ProportinalScaled(extends pic as Picture, Width as integer, Height as Integer) As Picture
// Calculate scale factor
dim faktor as Double = min( Height / Pic.Height, Width / Pic.Width)
// Calculate new size
dim w as integer = Pic.Width * faktor
dim h as integer = Pic.Height * faktor
// create new picture
dim NewPic as new Picture(w,h,32)
// draw picture in the new size
NewPic.Graphics.DrawPicture Pic, 0, 0, w, h, 0, 0, Pic.Width, Pic.Height
// return result
Return NewPic
End Function
End Module
End Project
See also:
The items on this page are in the following plugins: MBS MacFrameworks Plugin.