Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Written for HP DJ F2280.
- #No proper exception handling yet.
- #Does not seem to be loaded by default in a module.
- Add-Type -Assembly PresentationCore
- function Set-Scanner {
- #Hope, all scanners do support BMP.
- $script:wiaFormatBMP = "{B96B3CAB-0728-11D3-9D7B-0000F81EF32E}"
- $script:wiaUnspecifiedDeviceType = 0
- $script:dialog = New-Object -ComObject Wia.CommonDialog
- $script:device = $script:dialog.ShowSelectDevice($script:wiaUnspecifiedDeviceType, $true, $true)
- $script:item = $script:device.Items.Item(1)
- $script:dialog.ShowItemProperties($script:item)
- }
- function Read-ScanPage {
- #Format seems to be mandatory here. I get random crashes if no format specified.
- $image = $script:dialog.ShowTransfer($script:item, $script:wiaFormatBMP, $true)
- $imageBytes = $image.FileData.BinaryData
- $imageStream = New-Object IO.MemoryStream(,$imageBytes)
- #Could use GDI+ here, just don't want to.
- $bitmapFrame = [System.Windows.Media.Imaging.BitmapFrame]::Create($imageStream, [System.Windows.Media.Imaging.BitmapCreateOptions]::None, [System.Windows.Media.Imaging.BitmapCacheOption]::Default)
- $jpegEncoder = New-Object System.Windows.Media.Imaging.JpegBitmapEncoder
- $jpegEncoder.Frames.Add($bitmapFrame)
- #Default is 75, but due to a bug it produces huge files.
- #http://social.msdn.microsoft.com/Forums/en-US/windowswic/thread/c96a69d3-cf4f-4417-b694-9a9c288a6c20/
- $jpegEncoder.QualityLevel = 76
- $location = Get-Location
- $fileStream = New-Object IO.FileStream([IO.Path]::Combine($location, ("{0:D2}.jpg" -f $script:i)), [IO.FileMode]::Create)
- $jpegEncoder.Save($fileStream)
- $fileStream.Close()
- }
- function Read-ScanPages
- {
- Set-Scanner
- $script:i = 1
- while($true)
- {
- $command = Read-Host "Scan page $i (default)/[Q]uit"
- if($command -eq "q" -or $command -eq "Q") {
- break
- }
- Read-ScanPage
- $script:i += 1
- }
- }
- Export-ModuleMember Read-ScanPages
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement