Search This Blog

Thursday, 29 December 2016

Windows powershell scanner trigger

I have a second PC that I use as a "server" that, amongst other things, has a printer/scanner plugged into it via USB.  The script below give me a single-click that causes the scanner to scan whatever is in it and write it directly to a shared folder.

For reference, the underlying tech is "WIA" (google it) and the scanner is a Samsung CLX-3185

Powershell:
1:  [CmdletBinding()]   
2:   param (  
3:    [Parameter(Mandatory=$False)] [ValidateSet(150,300,600,1200)] [int] $DPI = 300,  
4:    [Parameter(Mandatory=$False)] [ValidateRange(0,100)]   [int] $Quality = 90,  
5:    [Parameter(Mandatory=$False)] [string] $Filename  
6:  )   
7:    
8:  if ( [string]::IsNullOrWhiteSpace($Filename) ){  
9:    $Filename = "C:\Shared\Scanner\$((Get-Date).ToString("yyyy-MM-dd-HHmmss")).jpg"  
10:  }  
11:    
12:  Write-Output "scanner - Samsung CLX-2185"  
13:  Write-Output "Settings:"  
14:  Write-Output "- Filename : '$Filename'"  
15:    
16:  $deviceManager = new-object -ComObject WIA.DeviceManager  
17:  $device = $deviceManager.DeviceInfos.Item(1).Connect()    
18:    
19:  $wiaFormatJPG = "{B96B3CAE-0728-11D3-9D7B-0000F81EF32E}"  
20:  foreach ($item in $device.Items) {   
21:    foreach($prop in $item.Properties){  
22:      if(($prop.PropertyID -eq 6147) -or ($prop.PropertyID -eq 6148)){ $prop.Value = $DPI }  
23:      Write-Output "- $($prop.Name) : $($prop.Value)"  
24:    }  
25:    Write-Output "Scanning, please wait..."  
26:    if($DPI -eq 1200){ Write-Output "scanning at 1200 DPI takes a long time, and scanner does not make any noise. Please be patient" }  
27:    $image = $item.Transfer($wiaFormatJPG)   
28:  }    
29:    
30:    
31:  $imageProcess = new-object -ComObject WIA.ImageProcess  
32:  $imageProcess.Filters.Add($imageProcess.FilterInfos.Item("Convert").FilterID)  
33:  $imageProcess.Filters.Item(1).Properties.Item("FormatID").Value = $wiaFormatJPG  
34:  $imageProcess.Filters.Item(1).Properties.Item("Quality").Value = $Quality  
35:  $image = $imageProcess.Apply($image)  
36:    
37:    
38:  $image.SaveFile("$Filename")  
39:    
40:  Write-Output "Scan complete"  
41:    
42:    
43:    
44:  # Other properties:  
45:  # Name          PropertyID  
46:  # ----          ----------  
47:  # Item Name           4098  
48:  # Full Item Name        4099  
49:  # Item Flags          4101  
50:  # Color Profile Name      4120  
51:  # Access Rights         4102  
52:  # Current Intent        6146  
53:  # Horizontal Resolution     6147  
54:  # Vertical Resolution      6148  
55:  # Horizontal Start Position   6149  
56:  # Vertical Start Position    6150  
57:  # Horizontal Extent       6151  
58:  # Vertical Extent        6152  
59:  # Rotation           6157  
60:  # Brightness          6154  
61:  # Contrast           6155  
62:  # Threshold           6159  
63:  # Item Size           4116  
64:  # Data Type           4103  
65:  # Bits Per Pixel        4104  
66:  # Compression          4107  
67:  # Channels Per Pixel      4109  
68:  # Bits Per Channel       4110  
69:  # Photometric Interpretation  6153  
70:  # Planar            4111  
71:  # Buffer Size          4118  
72:  # Filename extension      4123  
73:  # Media Type          4108  
74:  # Preferred Format       4105  
75:  # Format            4106  
76:  # Pixels Per Line        4112  
77:  # Bytes Per Line        4113  
78:  # Number of Lines        4114  
79:    
80:  # Not implemented yet, but PNG format could be added:  
81:  # $wiaFormatPNG = "{B96B3CAF-0728-11D3-9D7B-0000F81EF32E}"  

No comments:

Post a Comment