Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Twain protocol to realize continuous scanning function of scanner in C #

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "how to use Twain protocol to achieve continuous scanning function of scanner". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "how to use Twain protocol to achieve continuous scanning of scanners".

C # calls the Twain interface to realize continuous scanning of the scanner. In the listening TwainCommand.TransferReady state, the scanner is called to scan the picture. I initially tried to modify the method of TransferPictures in the twain class, but it turned out to be unworkable. In fact, this is a misunderstanding, we only need to modify the twain start scanning event, according to whether continuous scanning to the scanner to transmit different parameters to achieve this effect. With only one line of code, you can scan more than one picture at a time without changing any settings of the scanner.

Create a new ScanCommon class, and in the call interface, write down the method that calls the scan:

ScanCommon scan = new RS_ScanCommon (fileName, this.Handle); scan.PassDataBetweenForm + = new RS_ScanCommon.PassDataBetweenFormHandler (scan_PassDataBetweenForm); / / whether to scan continuously scan.bContinuousScan = true; scan.dtRow = dtRow; this.Enabled = false; scan.StartScan ()

In the ScanCommon class, the interface class that calls Twain (can be found on the Twain class network, this article is brief). The main code is as follows:

/ / add a delegate public delegate void PassDataBetweenFormHandler (object sender, PassDataEventArgs e); / / add an event of type PassDataBetweenFormHandler public event PassDataBetweenFormHandler PassDataBetweenForm; public ScanCommon (string fileName, IntPtr Handle) {this.fileName = fileName; this.Handle = Handle; / / scan initialization (TWAIN connection is used by default) tw = new Twain () Tw.Init (Handle, out rc); / / tw.Select ();} / / start scanning public void StartScan () {if (rc! = TwRC.Success) {MessageBox.Show ("device initialization failed, please check the hardware and driver!") ;} else {string path = "XXX path" + "\\ ScannerFile\" + frmName If (Scanning (path) = = false) {MessageBox.Show ("the device was not found or the device does not support the TWAIN component, unable to automatically create a scan task.") ; / / callback the main window PassDataEventArgs args = new PassDataEventArgs ("exit"); PassDataBetweenForm (this, args); GC.Collect ();}

In the ScanCommon class, the main scanning methods can be implemented by calling the twain class, and Scanning is the method that calls the driver to implement the scanning. Application.AddMessageFilter is to add the event listening of the scanner, and return the different states of the scanner in the listening event to achieve some operations of cancellation and scanning.

The main code is as follows:

# some methods of region scanning private bool Scanning (string filePath) {if (! msgfilter) {msgfilter = true; Application.AddMessageFilter (this);} ScanPath = filePath; return tw.Acquire (ref strScanErrorMessage, bContinuousScan);} / Save picture / private void ImageSave (IntPtr dibhandp) {bmprect = new Rectangle (0,0,0,0) If (dibhandp! = IntPtr.Zero) dibhand = dibhandp; bmpptr = GlobalLock (dibhand); pixptr = GetPixelInfo (bmpptr);} public void EndingScan () {if (msgfilter) {tw.Finish (); RemoveMessageFilter (this); msgfilter = false;} public static void RemoveMessageFilter (IMessageFilter value) {Application.RemoveMessageFilter (value);} # endregion

In the listening TwainCommand.TransferReady state, the scanner is called to scan the picture. I initially tried to modify the method of TransferPictures in the twain class, but it turned out to be unworkable.

In fact, this is a misunderstanding, we only need to modify the twain start scanning event, according to whether continuous scanning to the scanner to transmit different parameters to achieve this effect.

With only one line of code, you can scan more than one picture at a time without changing any settings of the scanner.

If the device supports continuous paper feeding, the scanner will scan directly until all the paper is scanned when the scan is started.

/ / this line of code is a continuous scan TwCapability cap = new TwCapability (TwCap.XferCount,-1)

The code is as follows:

/ start scanning / public bool Acquire (ref string strScanErrorMessage, bool MultiScan) {try {TwRC rc; CloseSrc (); if (appid.Id = = IntPtr.Zero) {Init (hwnd, out rc) If (appid.Id = = IntPtr.Zero) strScanErrorMessage = "scanner device not found, please check to see if scanner driver is installed!" ; return false;} rc = DSMident (appid, IntPtr.Zero, TwDG.Control, TwDAT.Identity, TwMSG.OpenDS, srcds); if (rc! = TwRC.Success) {strScanErrorMessage = "scanner device not found"; return false } if (MultiScan) {/ / if continuous scan TwCapability cap = new TwCapability (TwCap.XferCount,-1); rc = DScap (appid, srcds, TwDG.Control, TwDAT.Capability, TwMSG.Set, cap);} else {/ / otherwise scan only one TwCapability cap = new TwCapability (TwCap.XferCount, 1) Rc = DScap (appid, srcds, TwDG.Control, TwDAT.Capability, TwMSG.Set, cap);} if (rc! = TwRC.Success) {strScanErrorMessage = "Scanner device not found"; CloseSrc (); return false;} TwUserInterface guif = new TwUserInterface (); guif.ShowUI = 1; guif.ModalUI = 1 Guif.ParentHand = hwnd; rc = DSuserif (appid, srcds, TwDG.Control, TwDAT.UserInterface, TwMSG.EnableDS, guif); if (rc! = TwRC.Success) {strScanErrorMessage = "No paper in the scanner"; CloseSrc (); GC.Collect (); return false;}} catch (Exception e) {strScanErrorMessage = e.Message GC.Collect (); return false;} GC.Collect (); return true;}

After the scan is completed, the callback of the main window returns data, and the main window adds the following events:

Private void scan_PassDataBetweenForm (object sender, PassDataEventArgs e) {if (e.EventStr = = "exit") {this.Cursor = Cursors.WaitCursor; frm.dtRow = this.dtRow; / / some ways to process pictures. This.Enabled = true; this.Cursor = Cursors.Default; GC.Collect ();}} at this point, I believe you have a deeper understanding of "how to use Twain protocol to achieve continuous scanning of scanners in C#". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report