C#教程

关注公众号 jb51net

关闭
首页 > 软件编程 > C#教程 > C# Twain开发高扫仪对接

C#使用Twain协议开发一个高扫仪对接功能

作者:搬砖的诗人Z

这篇文章主要为大家详细介绍了C#如何使用Twain协议开发一个高扫仪对接功能,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下

请先安装TwainDotNet

实现代码

        private static AreaSettings AreaSettings = new AreaSettings(Units.Centimeters, 0.1f, 5.7f, 0.1F + 2.6f, 5.7f + 2.6f);

        Twain _twain;
        ScanSettings _settings;

  public ScanForm(string tName, string cNumber, List<string> names, CaseSelectForm form)
  {
      InitializeComponent();

      try
      {
          SelectForm = form;

          this.DoubleBuffered = true;//设置本窗体
          SetStyle(ControlStyles.UserPaint, true);
          SetStyle(ControlStyles.AllPaintingInWmPaint, true); // 禁止擦除背景.
          SetStyle(ControlStyles.DoubleBuffer, true); // 双缓冲

          myNames = names;
          _typeName = tName;
          _caseNumber = cNumber;
          this.Text = "扫描文件 - " + tName + " - " + cNumber;

          _twain = new Twain(new WinFormsWindowMessageHook(this));
          _twain.TransferImage += delegate (Object sender, TransferImageEventArgs args)
          {
              if (args.Image != null)
              {
                  Bitmap image = new Bitmap(args.Image);

                  string temp = DateTime.Now.ToString("yyyyMMddHHmmssffff") + ".jpg";
                  //缓存图片到磁盘
                  string path = tempPath + temp;
                  image.Save(path, ImageFormat.Jpeg);
                  tempPicUrl.Add(path);

                  var resizeImg = ImageHelper.KiResizeImage(image, 0.2f);
                  //缓存图片到磁盘
                  string path2 = resizePath + temp;
                  resizeImg.Save(path2, ImageFormat.Jpeg);

                  //scanImages.Add(image);
                  imageList1.Images.Add(image);

                  image.Dispose();
              }
              args.Image.Dispose();
          };
          _twain.ScanningComplete += delegate
          {
              for (int i = LastScanCount; i < imageList1.Images.Count; i++)
              {
                  ListViewItem li = new ListViewItem();

                  li.Text = i.ToString();
                  li.ImageIndex = i;
                  mlistview_Image.Items.Add(li);
                  ImageStateModels.Add(new ImageStateModel
                  {
                      Checked = false,
                      Person = "无",
                      tempPicUrl = tempPicUrl[i],
                      Name = i.ToString()
                  });
              }
              LastScanCount = ImageStateModels.Count;
              mlbl_PageCount.Text = string.Format("本次扫描 {0} 页", ImageStateModels.Count);
              Enabled = true;
              this.Activate();
              this.WindowState = FormWindowState.Maximized;
          };
          // ocr = new TesseractEngine("./tessdata", "chi_sim");

          InitInfo();
          InitCut();
          InitSearch();
          InitWords();
          DeleteFileByDir();
          InitMatchImg();
      }
      catch (Exception ex)
      {
          log4netHelper.Error("---报错方法--ScanForm");
          log4netHelper.Error(ex.Message);
          log4netHelper.Error(ex.StackTrace);
          log4netHelper.Error(ex.ToString());
          log4netHelper.Error("---end---");
          MessageBox.Show("请检查设备是否开启或者链接!");
      }
  }
   private void Init()
   {
       try
       {
           mcb_userName.DataSource = myNames;

           mtbPage_Set.SelectedIndex = 0;

           string savetype = ConfigHelper.ReadConfigPath("savetype.txt");
           if (savetype.Trim() == "1")
           {
               mcBox_savetype.Text = ".jpg";
           }
           else if (savetype.Trim() == "0")
           {
               mcBox_savetype.Text = ".PDF";
           }
           _saveType = savetype;
       }
       catch (Exception ex)
       {
           log4netHelper.Error("---报错方法--Init");
           log4netHelper.Error(ex.Message);
           log4netHelper.Error(ex.StackTrace);
           log4netHelper.Error(ex.ToString());
           log4netHelper.Error("---end---");
       }
   }

到此这篇关于C#使用Twain协议开发一个高扫仪对接功能的文章就介绍到这了,更多相关C# Twain开发高扫仪对接内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文