在 powershell 脚本中更改 WIA 扫描仪源
Change WIA scanner source in powershell script
所以我正在使用脚本来自动扫描,一切都很好,除了我的扫描仪总是使用平板扫描,而且我似乎无法将源更改为 Feeder。这是我正在使用的代码
# Create object to access the scanner
$deviceManager = new-object -ComObject WIA.DeviceManager
$device = $deviceManager.DeviceInfos.Item(1).Connect()
# Create object to access the scanned image later
$imageProcess = new-object -ComObject WIA.ImageProcess
# Store file format GUID strings
$wiaFormatBMP = "{B96B3CAB-0728-11D3-9D7B-0000F81EF32E}"
$wiaFormatPNG = "{B96B3CAF-0728-11D3-9D7B-0000F81EF32E}"
$wiaFormatGIF = "{B96B3CB0-0728-11D3-9D7B-0000F81EF32E}"
$wiaFormatJPEG = "{B96B3CAE-0728-11D3-9D7B-0000F81EF32E}"
$wiaFormatTIFF = "{B96B3CB1-0728-11D3-9D7B-0000F81EF32E}"
# Scan the image from scanner as BMP
foreach ($item in $device.Items) {
$image = $item.Transfer()
}
# set type to JPEG and quality/compression level
$imageProcess.Filters.Add($imageProcess.FilterInfos.Item("Convert").FilterID)
$imageProcess.Filters.Item(1).Properties.Item("FormatID").Value = $wiaFormatJPEG
$imageProcess.Filters.Item(1).Properties.Item("Quality").Value = 5
$image = $imageProcess.Apply($image)
# Build filepath from desktop path and filename 'Scan 0'
$filename = "$([Environment]::GetFolderPath("Desktop"))\Scan {0}.jpg"
# If a file named 'Scan 0' already exists, increment the index as long as needed
$index = 0
while (test-path ($filename -f $index)) {[void](++$index)}
$filename = $filename -f $index
# Save image to 'C:\Users\<username>\Desktop\Scan {x}'
$image.SaveFile($filename)
# Show image
& $filename
我只需要更改扫描源,然后保存所有图像即可。
所以经过大量的研究和实验。我终于成功了。
foreach($prop in $device.Properties){
if( $prop.Name -eq "Document Handling Select"){
$prop.Value=1
}
}
但这仍然只给了我 1 张图片,所以我不得不对我之前给出的所有代码使用 while 循环。现在完美运行!
所以我正在使用脚本来自动扫描,一切都很好,除了我的扫描仪总是使用平板扫描,而且我似乎无法将源更改为 Feeder。这是我正在使用的代码
# Create object to access the scanner
$deviceManager = new-object -ComObject WIA.DeviceManager
$device = $deviceManager.DeviceInfos.Item(1).Connect()
# Create object to access the scanned image later
$imageProcess = new-object -ComObject WIA.ImageProcess
# Store file format GUID strings
$wiaFormatBMP = "{B96B3CAB-0728-11D3-9D7B-0000F81EF32E}"
$wiaFormatPNG = "{B96B3CAF-0728-11D3-9D7B-0000F81EF32E}"
$wiaFormatGIF = "{B96B3CB0-0728-11D3-9D7B-0000F81EF32E}"
$wiaFormatJPEG = "{B96B3CAE-0728-11D3-9D7B-0000F81EF32E}"
$wiaFormatTIFF = "{B96B3CB1-0728-11D3-9D7B-0000F81EF32E}"
# Scan the image from scanner as BMP
foreach ($item in $device.Items) {
$image = $item.Transfer()
}
# set type to JPEG and quality/compression level
$imageProcess.Filters.Add($imageProcess.FilterInfos.Item("Convert").FilterID)
$imageProcess.Filters.Item(1).Properties.Item("FormatID").Value = $wiaFormatJPEG
$imageProcess.Filters.Item(1).Properties.Item("Quality").Value = 5
$image = $imageProcess.Apply($image)
# Build filepath from desktop path and filename 'Scan 0'
$filename = "$([Environment]::GetFolderPath("Desktop"))\Scan {0}.jpg"
# If a file named 'Scan 0' already exists, increment the index as long as needed
$index = 0
while (test-path ($filename -f $index)) {[void](++$index)}
$filename = $filename -f $index
# Save image to 'C:\Users\<username>\Desktop\Scan {x}'
$image.SaveFile($filename)
# Show image
& $filename
我只需要更改扫描源,然后保存所有图像即可。
所以经过大量的研究和实验。我终于成功了。
foreach($prop in $device.Properties){
if( $prop.Name -eq "Document Handling Select"){
$prop.Value=1
}
}
但这仍然只给了我 1 张图片,所以我不得不对我之前给出的所有代码使用 while 循环。现在完美运行!