Monday, 4 April 2016

Scanner With WIA(Flatbed and )

                                                                WIA (Flatbed and ADF)
Some important point you have to fallow  before to start the development :-
  1.  Before to start you should know something about WIA.
  2.  Read about Windows image Acquisition properties and feature.
  3. You have to import the Interop.WIA.dll
Steps you need to fallow for development :-
  1.  Open your visual studio, go to file and create windows form application.
  2.  Select language as c#.
  3.  Give your application name as you want.
  4.  Now go to the solution explorer right click and add form.
  5. Take one button control.
Now generate the click event of scan button.
 
Now you have to write the code for calling the scanner properties  initialized  the device and process , so you have to write this code into your form Initialization event.
Most Important point is-  You have to add reference of Interop.WIA.dll
public Form2()
{
      InitializeComponent();
      //Get default device Id 
     _deviceId = FindDefaultDeviceId(); 
     //Find Device 
     _deviceInfo = FindDevice(_deviceId);
     //Connect the device 
     _device = _deviceInfo.Connect();
  1. private DeviceInfo FindDevice(string deviceId)  
  2.         {  
  3.             DeviceManager manager = new DeviceManager();  
  4.             foreach (DeviceInfo info in manager.DeviceInfos)  
  5.                 if (info.DeviceID == deviceId)  
  6.                     return info;  
  7.   
  8.             return null;  
  9.         }  
  10.   //finding the device Id here
  11.         private string FindDefaultDeviceId()  
  12.         {  
  13.             string deviceId = Properties.Settings.Default.ScannerDeviceID;  
  14.             if (String.IsNullOrEmpty(deviceId))  
  15.             {  
  16.                 // Select a scanner  
  17.                 WIA.CommonDialog wiaDiag = new WIA.CommonDialog();  
  18.                 Device d = wiaDiag.ShowSelectDevice(WiaDeviceType.ScannerDeviceType, truefalse);  
  19.                 if (d != null)  
  20.                 {  
  21.                     deviceId = d.DeviceID;  
  22.                     Properties.Settings.Default.ScannerDeviceID = deviceId;  
  23.                     Properties.Settings.Default.Save();  
  24.                 }  
  25.             }  
  26.   
  27.             return deviceId;  
  28.         }  
 Copy and paste below code
  1. public List<Image> ScanPages(int dpi = 150, double width = 8.5, double height = 11)  
  2.        {  
  3.            Item item = _device.Items[1];  
  4.   
  5.            // configure item of the device   
  6.            SetDeviceItemProperty(ref item, 6146, 2); // greyscale  
  7.            SetDeviceItemProperty(ref item, 6147, dpi); // 150 dpi  
  8.            SetDeviceItemProperty(ref item, 6148, dpi); // 150 dpi  
  9.            SetDeviceItemProperty(ref item, 6151, (int)(dpi * width)); // set scan width  
  10.            SetDeviceItemProperty(ref item, 6152, (int)(dpi * height)); // set scan height  
  11.            SetDeviceItemProperty(ref item, 4104, 8); // bit depth  
  12.   
  13.            // Detect if the ADF is loaded, if not use the flatbed  
  14.   
  15.            List<Image> images = GetPagesFromScanner(ScanSource.DocumentFeeder, item);  
  16.            if (images.Count == 0)  
  17.            {  
  18.                // check the flatbed if ADF is not loaded, try from flatbed  
  19.                DialogResult dialogResult;  
  20.                do  
  21.                {  
  22.                    List<Image> singlePage = GetPagesFromScanner(ScanSource.Flatbed, item);  
  23.                    images.AddRange(singlePage);  
  24.                    dialogResult = MessageBox.Show("Do you want to scan another page?""ScanToEvernote", MessageBoxButtons.YesNo);  
  25.                }  
  26.                while (dialogResult == DialogResult.Yes);  
  27.            }  
  28.   
  29.            return images;  
  30.        }  
  31.   
  32.        private List<Image> GetPagesFromScanner(ScanSource source, Item item)  
  33.        {  
  34.            SetDeviceProperty(ref _device, 3088, (int)source);  
  35.   
  36.            List<Image> images = new List<Image>();  
  37.   
  38.            int handlingStatus = GetDeviceProperty(ref _device, WIA_DPS_DOCUMENT_HANDLING_STATUS);  
  39.            if ((source == ScanSource.DocumentFeeder && handlingStatus == FEED_READY) || (source == ScanSource.Flatbed && handlingStatus == FLATBED_READY))  
  40.            {  
  41.                do  
  42.                {  
  43.                    ImageFile wiaImage = null;  
  44.                    try  
  45.                    {  
  46.                        wiaImage = item.Transfer(WIA_FORMAT_JPEG);  
  47.                    }  
  48.                    catch (COMException ex)  
  49.                    {  
  50.                        if ((uint)ex.ErrorCode == WIA_ERROR_PAPER_EMPTY)  
  51.                            break;  
  52.                        else  
  53.                            throw;  
  54.                    }  
  55.   
  56.                    if (wiaImage != null)  
  57.                    {  
  58.   
  59.                        System.Diagnostics.Trace.WriteLine(String.Format("Image is {0} x {1} pixels", (float)wiaImage.Width / 150, (float)wiaImage.Height / 150));  
  60.                        Image image = ConvertToImage(wiaImage);  
  61.                        images.Add(image);  
  62.                    }  
  63.                }  
  64.                while (source == ScanSource.DocumentFeeder);  
  65.            }  
  66.            return images;  
  67.        }  
  68.   
  69.        private static Image ConvertToImage(ImageFile wiaImage)  
  70.        {  
  71.            byte[] imageBytes = (byte[])wiaImage.FileData.get_BinaryData();  
  72.            MemoryStream ms = new MemoryStream(imageBytes);  
  73.            Image image = Image.FromStream(ms);  
  74.            return image;  
  75.        }  
  1. #region Get/set device properties  
  2.   
  3.         private void SetDeviceProperty(ref Device device, int propertyID, int propertyValue)  
  4.         {  
  5.             foreach (Property p in device.Properties)  
  6.             {  
  7.                 if (p.PropertyID == propertyID)  
  8.                 {  
  9.                     object value = propertyValue;  
  10.                     p.set_Value(ref value);  
  11.                     break;  
  12.                 }  
  13.             }  
  14.         }  
  15.   
  16.         private int GetDeviceProperty(ref Device device, int propertyID)  
  17.         {  
  18.             int ret = -1;  
  19.   
  20.             foreach (Property p in device.Properties)  
  21.             {  
  22.                 if (p.PropertyID == propertyID)  
  23.                 {  
  24.                     ret = (int)p.get_Value();  
  25.                     break;  
  26.                 }  
  27.             }  
  28.   
  29.             return ret;  
  30.         }  
  31.   
  32.         private void SetDeviceItemProperty(ref Item item, int propertyID, int propertyValue)  
  33.         {  
  34.             foreach (Property p in item.Properties)  
  35.             {  
  36.                 if (p.PropertyID == propertyID)  
  37.                 {  
  38.                     object value = propertyValue;  
  39.                     p.set_Value(ref value);  
  40.                     break;  
  41.                 }  
  42.             }  
  43.         }  
  44.   
  45.         private int GetDeviceItemProperty(ref Item item, int propertyID)  
  46.         {  
  47.             int ret = -1;  
  48.   
  49.             foreach (Property p in item.Properties)  
  50.             {  
  51.                 if (p.PropertyID == propertyID)  
  52.                 {  
  53.                     ret = (int)p.get_Value();  
  54.                     break;  
  55.                 }  
  56.             }  
  57.   
  58.             return ret;  
  59.         }  
  60.  
  61.         #endregion  
  1. private void button1_Click(object sender, EventArgs e)  
  2.       {  
  3.          List<Image> obj = ScanPages();  
  4.   
  5.           //This code i written to upload the file  
  6.           foreach(Image aa in obj)  
  7.           {  
  8.               //aa.Save("D:\\ScanerUploadedFileWIA\\myfile.png _"  + DateTime.Now.ToLongTimeString(), ImageFormat.Png);  
  9.               aa.Save(@"D:\ScanerUploadedFileWIA\" + DateTime.Now.ToString("yyyy-MM-dd HHmmss") + ".jpeg", ImageFormat.Jpeg);  
  10.           }  
  11.   
  12.       }  
  13.   }  
  14.   
  15.   enum ScanSource  
  16.   {  
  17.       DocumentFeeder = 1,  
  18.       Flatbed = 2,  
  19.   }  
 Note :- Add the final You have to set the properties of device ID, check into below Image
 
 this is the final solution with WIA.
If you have any doubt and problems you can contact with me any time.
all the best. 

No comments:

Post a Comment