Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Non-Stop Scanner with sound
- # Settings
- $local:imageFileNameTemplate = "ScannedImage_%IMGNUM%_%DT%" # Use %IMGNUM% for seqence number; Use %DT% for datetime
- $local:imageFileFormat = 'TIFF' #Format of file: PNG, TIFF, JPEG, BMP
- $local:HorizontalResolution = 600
- $local:VerticalResolution = 600
- if ($Global:ImageCounter -ne $null) {
- } else {
- $Global:ImageCounter = 1; #BEginning of image numbering
- }
- Function Invoke-PlaySound {
- Param (
- $SoundProgram
- )
- if ($Script:WMP -ne $null) {
- #OK
- } else {
- $Script:WMP = New-Object -TypeName 'System.Media.SoundPlayer'
- Write-Host -ForegroundColor Gray "Created WMP"
- }
- ForEach ($local:soundData in $SoundProgram) {
- if ($local:soundData -match '^D:(?<Delay>\d+)$') {
- Write-Host -ForegroundColor Gray "Sleeping $($Matches['Delay']) msec"
- Start-Sleep -Milliseconds ([Int]::Parse( $Matches['Delay'] ));
- } elseif ($local:soundData -match '^M:(?<Filename>.+)$') {
- $local:mediaPath = Join-Path -Path $env:SystemRoot -ChildPath 'Media'
- $local:soundFilePath = Join-Path -Path $local:mediaPath -ChildPath $Script:AlarmSounds[$Matches['Filename']]
- Write-Host -ForegroundColor Gray "Playing $($local:soundFilePath) file"
- $Script:WMP.SoundLocation = $local:soundFilePath
- $Script:WMP.PlaySync();
- } else {
- Write-Host -ForegroundColor Yellow "Unable to get data: [$($local:soundData)]"
- }
- }
- }
- $Script:AlarmSounds = @{
- 'COUNTDOWN_LITE' = 'CHIMES.wav';
- 'COUNTDOWN_WARN' = 'CHORD.wav';
- 'ERROR' = 'RINGOUT.WAV'
- }
- $local:AlarmPrograms = @{
- 'READY_STEADY_GO' = @('D:1000', 'M:COUNTDOWN_LITE', 'D:1000', 'M:COUNTDOWN_LITE', 'D:1000', 'M:COUNTDOWN_LITE', 'D:1000', 'M:COUNTDOWN_LITE', 'D:1000', 'M:COUNTDOWN_LITE', 'D:1000', 'M:COUNTDOWN_WARN', 'D:1000', 'M:COUNTDOWN_WARN', 'D:1000', 'M:COUNTDOWN_WARN', 'D:1000' )
- 'ERROR' = @('D:5000', 'M:ERROR') * 100
- 'TEST' = @('D:1000', 'M:COUNTDOWN_LITE', 'D:1000', 'M:COUNTDOWN_WARN', 'D:1000', 'M:ERROR' )
- }
- $local:saveFolderPath = 'D:\ScanData'
- Write-Host -ForegroundColor Green "Selected folder is [$($local:saveFolderPath)]"
- $local:imageFileFormatGuid = $null
- Switch ($local:imageFileFormat.ToUpper()) {
- 'PNG' { $local:imageFileFormatGuid = '{B96B3CAF-0728-11D3-9D7B-0000F81EF32E}'; break; }
- 'TIFF' {$local:imageFileFormatGuid = '{B96B3CB1-0728-11D3-9D7B-0000F81EF32E}'; break; }
- 'JPEG' {$local:imageFileFormatGuid = '{B96B3CAE-0728-11D3-9D7B-0000F81EF32E}'; break; }
- 'BMP' {$local:imageFileFormatGuid = '{B96B3CAB-0728-11D3-9D7B-0000F81EF32E}'; break; }
- default { Write-Host -ForegroundColor Yellow "Unknown format [$($local:imageFileFormat)]"; return $null; break; }
- }
- Write-Host -ForegroundColor Green "Selected File format is [$($local:imageFileFormat.ToUpper())]"
- Write-Host -ForegroundColor Green "Selected save path is [$($local:saveFolderPath)]"
- Write-Host -ForegroundColor Gray "Initiating scanner"
- $local:WiaManager = New-Object -ComObject 'WIA.DeviceManager'
- if ($local:WiaManager.DeviceInfos.Count -le 0) {
- Write-Host -ForegroundColor Yellow "No scanner devices found!"
- return $false
- }
- $local:scannerDeviceInfo = $local:WiaManager.DeviceInfos.Item(1);
- Write-Host -ForegroundColor Green "Device selected: $($local:scannerDeviceInfo.Properties.Item("Name").Value)";
- $local:scannerDevice = $local:scannerDeviceInfo.Connect()
- $local:scannerTray = $local:scannerDevice.Items.Item(1)
- #Set scanning props
- @($local:scannerTray.Properties | Where-Object {$_.PropertyID -eq 6147})[0].Value = $local:HorizontalResolution
- @($local:scannerTray.Properties | Where-Object {$_.PropertyID -eq 6148})[0].Value = $local:VerticalResolution
- $local:imageProcessor = New-Object -ComObject 'WIA.ImageProcess'
- $local:imageProcessor.Filters.Add($local:imageProcessor.FilterInfos.Item("Convert").FilterId)
- $local:imageProcessor.Filters.Item(1).Properties.Item("FormatID").Value = $local:imageFileFormatGuid
- Write-Host -ForegroundColor Gray "We will now test beep sounds"
- Invoke-PlaySound -SoundProgram $local:AlarmPrograms.TEST
- Write-Host -ForegroundColor White "`r`n`r`n`r`n`t`t*** GO TO SCANNER NOW! * * * `r`n`r`n`r`n";
- While ($true) {
- Invoke-PlaySound -SoundProgram $local:AlarmPrograms.READY_STEADY_GO
- Write-Host -ForegroundColor Gray "Scanning..."
- $local:image = $local:scannerTray.Transfer($local:imageFileFormatGuid)
- if ($local:image.FormatId -ne $local:imageFileFormatGuid) {
- Write-Host "Changing format"
- $local:image = $local:imageProcessor.Apply($local:image)
- }
- $local:dt = Get-Date -UFormat "%Y-%m-%d %H-%M-%S"
- $local:seqnum = $Global:ImageCounter.ToString().PadLeft(10,'0')
- $local:imageFileName = Join-Path -Path $local:saveFolderPath -ChildPath $( $local:imageFileNameTemplate.Replace('%IMGNUM%',$local:seqnum).Replace('%DT%',$local:dt) + '.' + $local:imageFileFormat )
- Write-Host -ForegroundColor Gray "Saving to [$($local:imageFileName)]"
- $local:image.SaveFile($local:imageFileName)
- $Global:ImageCounter++
- }
Add Comment
Please, Sign In to add comment