Hello堆棧溢出!使用WIA和VB在同一臺計算機上自動掃描多臺掃描儀
我是編程的終極初學者。我在php和vba方面有一些經驗,在我需要的時候做我自己的腳本,特別是在excel中。
近日,在工程項目,我需要能夠從多臺掃描儀(比如2個開始)都連接到同一臺計算機掃描AUTOMATICALLY(比如每2分鐘)。 我決定用這個項目作爲開始,讓我感受一下Visual Basic。 所以,我們走了,我安裝了visual studio express 2010,並開始編寫我的腳本,試圖找到可以幫助我的代碼。我發現,WIA可能與幫助(吐溫也很好,但它似乎更晦澀我是新手)
無論如何,我終於想出了一個應用程序,能夠只時自動在設定的時間間隔掃描一個掃描儀已連接。我連接多臺掃描儀時遇到問題。然後,第一次掃描正確發生(掃描儀1掃描,然後掃描儀2掃描),但是當第二次掃描應該開始時,沒有任何反應,並且掃描儀變得不可訪問(忙碌)。 雖然我可能忘了「釋放」或「斷開」上次使用的掃描儀。或者,也許,某些東西仍然在掃描儀的緩衝存儲器中?
我一直在這個問題上停留了最後3天,不知道如何使它工作。
這裏是掃描功能:(我不過去,因爲它是在用戶界面和文件夾管理的其餘部分)
Public Sub scannerloop()
'format constants
Const wiaFormatBMP = "{B96B3CAB-0728-11D3-9D7B-0000F81EF32E}"
Const wiaFormatPNG = "{B96B3CAF-0728-11D3-9D7B-0000F81EF32E}"
Const wiaFormatGIF = "{B96B3CB0-0728-11D3-9D7B-0000F81EF32E}"
Const wiaFormatJPEG = "{B96B3CAE-0728-11D3-9D7B-0000F81EF32E}"
Const wiaFormatTIFF = "{B96B3CB1-0728-11D3-9D7B-0000F81EF32E}"
'file format
Dim fileformat As String
If Me.FileExt.SelectedItem = "TIF" Then fileformat = wiaFormatTIFF
If Me.FileExt.SelectedItem = "JPEG" Then fileformat = wiaFormatJPEG
If Me.FileExt.SelectedItem = "BMP" Then fileformat = wiaFormatBMP
If Me.FileExt.SelectedItem = "PNG" Then fileformat = wiaFormatPNG
If Me.FileExt.SelectedItem = "GIF" Then fileformat = wiaFormatGIF
'colors
Dim colorcode As Integer
If Me.Colorbox.SelectedItem = "Black and white" Then colorcode = 4
If Me.Colorbox.SelectedItem = "Greyscale" Then colorcode = 2
If Me.Colorbox.SelectedItem = "Colour" Then colorcode = 1
'Resolution
Dim dpi As Integer
dpi = Me.dpiBox.SelectedItem
Dim horizextent = dpi * 8.2
Dim vertextent = dpi * 11.6
Dim j As String = 1
Dim DeviceManager1 = CreateObject("WIA.DeviceManager") 'wia device manager
For i = 1 To DeviceManager1.DeviceInfos.Count 'loop through all devices
If DeviceManager1.DeviceInfos(i).Type = 1 Then 'Select only scanners, not webcams etc...
'startpoint to calculate how long it is to scan
Dim ScanStart = DateAndTime.Second(Now) + (DateAndTime.Minute(Now) * 60) + (DateAndTime.Hour(Now) * 3600)
'Directory + file
Dim targetdir = Me.ProjectFolderBox.Text & "\scans\Scanner" & j & "\S" & j & "_" & Me.FilePrefix.Text & Me.CurrFileIndex & "." & Me.FileExt.SelectedItem
Form2.CurrentActionLabel.Text = "Scanning from scanner #" & j
Dim Scanner As WIA.Device = DeviceManager1.DeviceInfos(i).connect
If IsNothing(Scanner) Then
Log(Me.logfilename, Now & " | Scanner #" & j & " not found")
Else
Try
Dim Img As WIA.ImageFile
With Scanner.Items(1)
.Properties("6146").Value = colorcode '4 is Black-white,gray is 2, color 1 (Color Intent)
.Properties("6147").Value = dpi 'dots per inch/horizontal
.Properties("6148").Value = dpi 'dots per inch/vertical
.Properties("6149").Value = 0 'x point where to start scan
.Properties("6150").Value = 0 'y-point where to start scan
'Following is A4 paper size. (Not 100% accurate because real A4 Ht errors)
.Properties("6151").Value = horizextent 'horizontal exent DPI x inches wide
.Properties("6152").Value = vertextent 'vertical extent DPI x inches tall
' .Properties("4104").Value = 8 'bits per pixel
End With
'transfer image
Img = Scanner.Items(1).Transfer(fileformat) 'scans the image.
'kill previous file if exists to avoid errors
If System.IO.File.Exists(targetdir) = True Then
Kill(targetdir)
End If
Img.SaveFile(targetdir)
'last scan
Form2.LastFileLabel.Text = "\Scanner" & j & "\S" & j & "_" & Me.FilePrefix.Text & Me.CurrFileIndex & "." & Me.FileExt.SelectedItem
Form2.LastScanLabel.Text = Now
Catch ex As Exception
MsgBox(ex.Message)
Finally
Scanner = Nothing
End Try
End If
'End time for the scan
Dim ScanEnd = DateAndTime.Second(Now) + (DateAndTime.Minute(Now) * 60) + (DateAndTime.Hour(Now) * 3600)
'log
Log(Me.logfilename, Now & " | Scanner #" & j & " | Scanned " & targetdir & " | duration: " & (ScanEnd - ScanStart))
j = j + 1
Next
DeviceManager1 = Nothing
Me.CurrFileIndex = Me.CurrFileIndex + 1
Me.ScanCount = Me.ScanCount + 1
Me.NextScan = DateAdd("n", Me.IntervalBox.Value, Now)
Form2.ScanCountLabel.Text = Me.ScanCount
Form2.NextScanLabel.Text = Me.NextScan
Form2.CurrentActionLabel.Text = "Waiting..."
'Increment next file index and update in config file
Me.FileIndexBox.Value = Me.CurrFileIndex
SaveCfg()
End Sub
請寬容我,我知道,代碼可能是一個噩夢編程專業人員與許多壞東西,但它是字面上我的第一個VB程序,我渴望學習。
所以基本上,其餘的程序是一個窗體,我進入掃描的目標目錄,文件名,分辨率等,當我點擊'開始掃描'時,它第一次運行scannerloop - 啓動一個'scantimer',它每次啓動scannerloop時會啓動。
正如我所說的,它完全適用於1掃描儀(文件按預期創建,日誌文件更新等),但只要我有2個掃描儀,只有第一個掃描工作,然後,當掃描儀#1應該開始掃描,它不和掃描儀#2的LED開始閃爍(彷彿它正在掃描,但它不掃描)
我希望有人能幫助我。
在此先感謝。
文斯
更新 - 件事,我想這可能感興趣: 我只是試圖從定時器添加一個for循環,使其從兩個掃描器掃描幾次(所以,independantly和該方案的基本其餘部分):
Dim DeviceManager1 = CreateObject("WIA.DeviceManager") 'wia device manager
For k = 1 To 3
Dim j As String = 1
For i = 1 To DeviceManager1.DeviceInfos.Count 'loop through all devices
[...]
Next i
Next k
DeviceManager1 = Nothing
這表明,循環作品第一次出現(從每個掃描器掃描一次),但僅此而已,掃描儀永不掃描第二次開始閃爍,所以基本上完全相同一樣的問題。 我也嘗試以包括新的循環的Devicemanager聲明:
For k = 1 To 3
Dim DeviceManager1 = CreateObject("WIA.DeviceManager") 'wia device manager
Dim j As String = 1
For i = 1 To DeviceManager1.DeviceInfos.Count 'loop through all devices
[...]
Next i
DeviceManager1 = Nothing
Next k
但它並沒有改變任何東西。
我做笏登錄循環內的事件,這樣我可以確切地知道那裏的東西停止接下來的事情:
昏暗DeviceManager1 =的CreateObject(「WIA.DeviceManager」)「WIA設備管理器 昏暗J所示字符串= 1
For i = 1 To DeviceManager1.DeviceInfos.Count 'loop through all devices
If DeviceManager1.DeviceInfos(i).Type = 1 Then 'Select only scanners, not webcams etc...
'startpoint to calculate how long it is to scan
Dim ScanStart = DateAndTime.Second(Now) + (DateAndTime.Minute(Now) * 60) + (DateAndTime.Hour(Now) * 3600)
'Directory + file
Dim targetdir = Me.ProjectFolderBox.Text & "\scans\Scanner" & j & "\S" & j & "_" & Me.FilePrefix.Text & Me.CurrFileIndex & "." & Me.FileExt.SelectedItem
Form2.CurrentActionLabel.Text = "Scanning from scanner #" & j
Dim Scanner As WIA.Device = DeviceManager1.DeviceInfos(i).connect
If IsNothing(Scanner) Then
Log(Me.logfilename, Now & " | Scanner #" & j & " not found")
Else
Try
Dim Img As WIA.ImageFile
'log
Log(Me.logfilename, Now & " | Scanner #" & j & " | Img initialized")
With Scanner.Items(1)
.Properties("6146").Value = colorcode '4 is Black-white,gray is 2, color 1 (Color Intent)
.Properties("6147").Value = dpi 'dots per inch/horizontal
.Properties("6148").Value = dpi 'dots per inch/vertical
.Properties("6149").Value = 0 'x point where to start scan
.Properties("6150").Value = 0 'y-point where to start scan
'Following is A4 paper size. (Not 100% accurate because real A4 Ht errors)
.Properties("6151").Value = horizextent 'horizontal exent DPI x inches wide
.Properties("6152").Value = vertextent 'vertical extent DPI x inches tall
' .Properties("4104").Value = 8 'bits per pixel
End With
'log
Log(Me.logfilename, Now & " | Scanner #" & j & " | properties initialized")
'transfer image
Img = Scanner.Items(1).Transfer(fileformat) 'scans the image.
'log
Log(Me.logfilename, Now & " | Scanner #" & j & " |Transfer done")
'kill previous file if exists to avoid errors
If System.IO.File.Exists(targetdir) = True Then
Kill(targetdir)
'log
Log(Me.logfilename, Now & " | Scanner #" & j & " | deleted existing " & targetdir)
End If
Img.SaveFile(targetdir)
'log
Log(Me.logfilename, Now & " | Scanner #" & j & " | saved " & targetdir)
'last scan
Form2.LastFileLabel.Text = "\Scanner" & j & "\S" & j & "_" & Me.FilePrefix.Text & Me.CurrFileIndex & "." & Me.FileExt.SelectedItem
Form2.LastScanLabel.Text = Now
Catch ex As Exception
MsgBox(ex.Message)
Finally
Scanner = Nothing
End Try
End If
'End time for the scan
Dim ScanEnd = DateAndTime.Second(Now) + (DateAndTime.Minute(Now) * 60) + (DateAndTime.Hour(Now) * 3600)
'log
Log(Me.logfilename, Now & " | Scanner #" & j & " | Scanned " & targetdir & " | duration: " & (ScanEnd - ScanStart))
j = j + 1
End If
Next i
,這裏是產生的日誌文件:
Scan starts 29/11/2012 9:24:31 AM | Interval :Start scanning with 5 min | Res:100 DPI |
29/11/2012 9:24:31 AM | Scanner #1 | Img initialized
29/11/2012 9:24:31 AM | Scanner #1 | properties initialized
29/11/2012 9:24:49 AM | Scanner #1 |Transfer done
29/11/2012 9:24:49 AM | Scanner #1 | saved C:\__2\scans\Scanner1\S1_img_1.TIF
29/11/2012 9:24:49 AM | Scanner #1 | Scanned C:\__2\scans\Scanner1\S1_img_1.TIF | duration: 18
29/11/2012 9:24:49 AM | Scanner #2 | Img initialized
29/11/2012 9:24:49 AM | Scanner #2 | properties initialized
29/11/2012 9:25:08 AM | Scanner #2 |Transfer done
29/11/2012 9:25:08 AM | Scanner #2 | saved C:\__2\scans\Scanner2\S2_img_1.TIF
29/11/2012 9:25:08 AM | Scanner #2 | Scanned C:\__2\scans\Scanner2\S2_img_1.TIF | duration: 19
29/11/2012 9:25:08 AM | Scanner #1 | Img initialized
29/11/2012 9:25:08 AM | Scanner #1 | properties initialized
似乎出問題這一行:
Img = Scanner.Items(1).Transfer(fileformat) 'scans the image.
看起來WIA很樂意從掃描儀1切換到2,但拒絕回到掃描儀1進行下一輪。另外,我應該精確地說,當第二次掃描應該發生時,掃描儀#2閃爍(而不是1令我驚訝)。 是否有可能是掃描儀#2被選爲「默認掃描儀」或類似的東西,如果是這樣,是否有辦法恢復?
感謝您的幫助
我不知道,但也許是更好地發佈您的代碼在http://codereview.stackexchange.com/ – Nianios