Advertisement
is_a_cdr

WiaScan.psm1

Jun 3rd, 2012
338
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #Written for HP DJ F2280.
  2. #No proper exception handling yet.
  3.  
  4. #Does not seem to be loaded by default in a module.
  5. Add-Type -Assembly PresentationCore
  6.  
  7. function Set-Scanner {
  8.     #Hope, all scanners do support BMP.
  9.     $script:wiaFormatBMP = "{B96B3CAB-0728-11D3-9D7B-0000F81EF32E}"
  10.     $script:wiaUnspecifiedDeviceType = 0
  11.  
  12.     $script:dialog = New-Object -ComObject Wia.CommonDialog
  13.     $script:device = $script:dialog.ShowSelectDevice($script:wiaUnspecifiedDeviceType, $true, $true)
  14.     $script:item = $script:device.Items.Item(1)
  15.     $script:dialog.ShowItemProperties($script:item)    
  16. }
  17.  
  18. function Read-ScanPage {
  19.     #Format seems to be mandatory here. I get random crashes if no format specified.
  20.     $image = $script:dialog.ShowTransfer($script:item, $script:wiaFormatBMP, $true)
  21.     $imageBytes = $image.FileData.BinaryData
  22.     $imageStream = New-Object IO.MemoryStream(,$imageBytes)
  23.     #Could use GDI+ here, just don't want to.
  24.     $bitmapFrame = [System.Windows.Media.Imaging.BitmapFrame]::Create($imageStream, [System.Windows.Media.Imaging.BitmapCreateOptions]::None, [System.Windows.Media.Imaging.BitmapCacheOption]::Default)
  25.     $jpegEncoder = New-Object System.Windows.Media.Imaging.JpegBitmapEncoder
  26.     $jpegEncoder.Frames.Add($bitmapFrame)
  27.     #Default is 75, but due to a bug it produces huge files.
  28.     #http://social.msdn.microsoft.com/Forums/en-US/windowswic/thread/c96a69d3-cf4f-4417-b694-9a9c288a6c20/
  29.     $jpegEncoder.QualityLevel = 76
  30.     $location = Get-Location
  31.     $fileStream = New-Object IO.FileStream([IO.Path]::Combine($location, ("{0:D2}.jpg" -f $script:i)), [IO.FileMode]::Create)
  32.     $jpegEncoder.Save($fileStream)
  33.     $fileStream.Close()
  34. }
  35.  
  36. function Read-ScanPages
  37. {
  38.     Set-Scanner
  39.    
  40.     $script:i = 1
  41.     while($true)
  42.     {
  43.         $command = Read-Host "Scan page $i (default)/[Q]uit"
  44.         if($command -eq "q" -or $command -eq "Q") {
  45.             break
  46.         }    
  47.         Read-ScanPage  
  48.         $script:i += 1
  49.     }
  50. }
  51.  
  52. Export-ModuleMember Read-ScanPages
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement