filimonic

Powershell: Non-Stop scanning with delays and signals

Apr 26th, 2015
383
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #Non-Stop Scanner with sound
  2.  
  3. # Settings
  4. $local:imageFileNameTemplate = "ScannedImage_%IMGNUM%_%DT%" # Use %IMGNUM% for seqence number; Use %DT% for datetime
  5. $local:imageFileFormat = 'TIFF' #Format of file: PNG, TIFF, JPEG, BMP
  6. $local:HorizontalResolution = 600
  7. $local:VerticalResolution   = 600
  8.  
  9.  
  10. if ($Global:ImageCounter -ne $null) {
  11. } else {
  12.     $Global:ImageCounter   = 1; #BEginning of image numbering
  13. }
  14.  
  15. Function Invoke-PlaySound {
  16.     Param (
  17.         $SoundProgram
  18.     )
  19.    
  20.     if ($Script:WMP -ne $null) {
  21.         #OK
  22.     } else {
  23.         $Script:WMP = New-Object -TypeName 'System.Media.SoundPlayer'
  24.         Write-Host -ForegroundColor Gray "Created WMP"
  25.     }
  26.  
  27.     ForEach ($local:soundData in $SoundProgram) {
  28.         if ($local:soundData -match '^D:(?<Delay>\d+)$') {
  29.             Write-Host -ForegroundColor Gray "Sleeping $($Matches['Delay']) msec"
  30.             Start-Sleep -Milliseconds  ([Int]::Parse( $Matches['Delay'] ));
  31.         } elseif ($local:soundData -match '^M:(?<Filename>.+)$') {
  32.             $local:mediaPath = Join-Path -Path $env:SystemRoot -ChildPath 'Media'
  33.             $local:soundFilePath = Join-Path -Path $local:mediaPath -ChildPath $Script:AlarmSounds[$Matches['Filename']]
  34.             Write-Host -ForegroundColor Gray "Playing $($local:soundFilePath) file"
  35.             $Script:WMP.SoundLocation = $local:soundFilePath
  36.             $Script:WMP.PlaySync();
  37.         } else {
  38.             Write-Host -ForegroundColor Yellow "Unable to get data: [$($local:soundData)]"
  39.         }
  40.     }
  41.  
  42. }
  43.  
  44.  
  45. $Script:AlarmSounds = @{
  46.                         'COUNTDOWN_LITE' = 'CHIMES.wav';
  47.                         'COUNTDOWN_WARN' = 'CHORD.wav';
  48.                         'ERROR'          = 'RINGOUT.WAV'
  49.  
  50. }
  51.  
  52. $local:AlarmPrograms = @{
  53.                             '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' )
  54.                             'ERROR'           = @('D:5000', 'M:ERROR') * 100
  55.                             'TEST'            = @('D:1000', 'M:COUNTDOWN_LITE', 'D:1000', 'M:COUNTDOWN_WARN', 'D:1000', 'M:ERROR' )
  56.                         }
  57.  
  58.  
  59.  
  60. $local:saveFolderPath = 'D:\ScanData'
  61.  
  62. Write-Host -ForegroundColor Green "Selected folder is [$($local:saveFolderPath)]"
  63.  
  64. $local:imageFileFormatGuid = $null
  65. Switch ($local:imageFileFormat.ToUpper()) {
  66.     'PNG' { $local:imageFileFormatGuid = '{B96B3CAF-0728-11D3-9D7B-0000F81EF32E}'; break; }
  67.     'TIFF' {$local:imageFileFormatGuid = '{B96B3CB1-0728-11D3-9D7B-0000F81EF32E}'; break; }
  68.     'JPEG' {$local:imageFileFormatGuid = '{B96B3CAE-0728-11D3-9D7B-0000F81EF32E}'; break; }
  69.     'BMP' {$local:imageFileFormatGuid  = '{B96B3CAB-0728-11D3-9D7B-0000F81EF32E}'; break; }
  70.     default { Write-Host -ForegroundColor Yellow "Unknown format [$($local:imageFileFormat)]"; return $null; break; }
  71. }
  72.  
  73. Write-Host -ForegroundColor Green "Selected File format is [$($local:imageFileFormat.ToUpper())]"
  74. Write-Host -ForegroundColor Green "Selected save path is [$($local:saveFolderPath)]"
  75.  
  76. Write-Host -ForegroundColor Gray "Initiating scanner"
  77. $local:WiaManager = New-Object -ComObject 'WIA.DeviceManager'
  78. if ($local:WiaManager.DeviceInfos.Count -le 0) {
  79.     Write-Host -ForegroundColor Yellow "No scanner devices found!"
  80.     return $false
  81. }
  82.  
  83. $local:scannerDeviceInfo = $local:WiaManager.DeviceInfos.Item(1);
  84. Write-Host -ForegroundColor Green "Device selected: $($local:scannerDeviceInfo.Properties.Item("Name").Value)";
  85.  
  86.  
  87.  
  88. $local:scannerDevice = $local:scannerDeviceInfo.Connect()
  89. $local:scannerTray   = $local:scannerDevice.Items.Item(1)
  90.  
  91. #Set scanning props
  92. @($local:scannerTray.Properties | Where-Object {$_.PropertyID -eq 6147})[0].Value = $local:HorizontalResolution
  93. @($local:scannerTray.Properties | Where-Object {$_.PropertyID -eq 6148})[0].Value = $local:VerticalResolution
  94.  
  95. $local:imageProcessor = New-Object -ComObject 'WIA.ImageProcess'
  96. $local:imageProcessor.Filters.Add($local:imageProcessor.FilterInfos.Item("Convert").FilterId)
  97. $local:imageProcessor.Filters.Item(1).Properties.Item("FormatID").Value = $local:imageFileFormatGuid
  98.  
  99. Write-Host -ForegroundColor Gray "We will now test beep sounds"
  100. Invoke-PlaySound -SoundProgram $local:AlarmPrograms.TEST
  101.  
  102.  
  103.  
  104. Write-Host -ForegroundColor White "`r`n`r`n`r`n`t`t*** GO TO SCANNER NOW! * * * `r`n`r`n`r`n";
  105. While ($true) {
  106.     Invoke-PlaySound -SoundProgram $local:AlarmPrograms.READY_STEADY_GO
  107.     Write-Host -ForegroundColor Gray "Scanning..."
  108.     $local:image = $local:scannerTray.Transfer($local:imageFileFormatGuid)
  109.  
  110.     if ($local:image.FormatId -ne $local:imageFileFormatGuid) {
  111.         Write-Host "Changing format"
  112.         $local:image = $local:imageProcessor.Apply($local:image)
  113.     }
  114.     $local:dt = Get-Date -UFormat "%Y-%m-%d %H-%M-%S"
  115.     $local:seqnum = $Global:ImageCounter.ToString().PadLeft(10,'0')
  116.     $local:imageFileName = Join-Path -Path $local:saveFolderPath -ChildPath $( $local:imageFileNameTemplate.Replace('%IMGNUM%',$local:seqnum).Replace('%DT%',$local:dt) + '.' + $local:imageFileFormat )
  117.     Write-Host -ForegroundColor Gray "Saving to [$($local:imageFileName)]"
  118.     $local:image.SaveFile($local:imageFileName)
  119.     $Global:ImageCounter++
  120. }
Add Comment
Please, Sign In to add comment