| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.IO;
- using Saraff.Twain;
- using HXX.Scanner.Common;
- namespace HXX.Scanner.Biz
- {
- /// <summary>
- /// 老类,设置基础信息到扫描仪
- /// </summary>
- public class biz_property_scanner_to_engine
- {
- public static void Set(Twain32 engine, Device_Scanner scanner)
- {
- set_gray(engine);
- set_bitDepth(engine);
- set_file_mode(engine);
- set_file_type(engine);
- if (!scanner.deviceName.ToUpper().Contains("KODAK"))
- {
- set_removeBorder(engine);
- }
- //来自 界面设置 或 网端传参
- set_paper(engine, scanner);
- set_AB(engine);
-
- try { engine.Capabilities.Brightness.Set(scanner.ld); } catch (Exception ee) { }
- try { engine.Capabilities.Contrast.Set(scanner.dbd); } catch (Exception ee) { }
- engine.Capabilities.XResolution.Set(scanner.resolution);
- engine.Capabilities.YResolution.Set(scanner.resolution);
- //engine.Capabilities.XResolution.Set(200);
- //engine.Capabilities.YResolution.Set(200);
- engine.ShowUI = config_environment.use_source_ui;
- /////////////////test
- //this._twain32.CloseDataSource();
- //this._twain32.SourceIndex = index;
- //this._twain32.OpenDataSource();
- //this._twain32.Capabilities.XResolution.Set(200);
- //this._twain32.Capabilities.YResolution.Set(200);
- //this._twain32.Capabilities.PixelType.Set(TwPixelType.Gray);
- //this._twain32.Capabilities.XferMech.Set(TwSX.File);
- //this._twain32.Capabilities.ImageFileFormat.Set(TwFF.Tiff);
- //this._twain32.Capabilities.SupportedSizes.Reset();
- //this._twain32.Capabilities.SupportedSizes.Set(TwSS.A4);
- //this._twain32.Capabilities.XferCount.Set(2);
- //this._twain32.Capabilities.DuplexEnabled.Set(true);
- //this._twain32.Capabilities.Brightness.Set(0);
- //this._twain32.Capabilities.Contrast.Set(0);
- }
- /// <summary>
- /// 设置纸张格式
- /// </summary>
- /// <param name="engine"></param>
- /// <param name="scanner"></param>
- private static void set_paper(Twain32 engine, Device_Scanner scanner)
- {
- try
- {
- try
- {
- //佳能必须要先Reset,再Set才有效
- engine.Capabilities.SupportedSizes.Reset();
- }
- catch (Exception e)
- {
- LogManager.WriteLog(e);
- }
- var autoSizeCab = engine.Capabilities.AutoSize.ToString().ToLower();
- if (autoSizeCab.Contains("not"))
- {
- //var supported = engine.Capabilities.SupportedSizes.Get();
- if (config_environment.paper_size == PaperSize.A3) { engine.Capabilities.SupportedSizes.Set(TwSS.A3); }
- if (config_environment.paper_size == PaperSize.A4) { engine.Capabilities.SupportedSizes.Set(TwSS.A4); }
- if (config_environment.paper_size == PaperSize.K8) { engine.Capabilities.SupportedSizes.Set(TwSS.A3); }
- if (config_environment.paper_size == PaperSize.K16) { engine.Capabilities.SupportedSizes.Set(TwSS.A4); }
- }
- else
- {
- engine.SetCap(TwCap.AutoSize, true);
- }
- }
- catch (Exception ex)
- {
- //Console.WriteLine("SupportedSizes got exception");
- if (!ex.Message.Contains("Data parameter out of range"))
- {
- LogManager.WriteLog(ex);
- }
- }
- }
- /// <summary>
- /// 设置单双面
- /// </summary>
- /// <param name="engine"></param>
- private static void set_AB(Twain32 engine)
- {
- engine.Capabilities.FeederEnabled.Set(true);
- if (config_environment.scan_type == ScanType.Formal)
- {
- engine.Capabilities.XferCount.Set(-1);
- if (config_environment.scan_ab)
- {
- engine.Capabilities.DuplexEnabled.Set(true);
- }
- else
- {
- engine.Capabilities.DuplexEnabled.Set(false);
- }
- }
- else
- {
- if (config_environment.scan_ab)
- {
- engine.Capabilities.XferCount.Set(2);
- engine.Capabilities.DuplexEnabled.Set(true);
- }
- else
- {
- engine.Capabilities.XferCount.Set(1);
- engine.Capabilities.DuplexEnabled.Set(false);
- }
- }
- }
- ///// <summary>
- ///// 设置灰度
- ///// </summary>
- ///// <param name="engine"></param>
- //private static void set_gray(Twain32 engine)
- //{
- // Boolean hasBw = false;
- // Boolean hasGray = false;
- // Twain32.Enumeration _pixelTypes = engine.Capabilities.PixelType.Get();
- // for (int i = 0; i < _pixelTypes.Count; i++)
- // {
- // if (_pixelTypes[i].ToString().Equals("BW"))
- // {
- // hasBw = true;
- // }
- // if (_pixelTypes[i].ToString().Equals("Gray"))
- // {
- // hasGray = true;
- // }
- // }
- // //一般都支持这个模式
- // // test gray
- // if (false)
- // {
- // if (hasBw)
- // {
- // try
- // {
- // engine.Capabilities.PixelType.Set(TwPixelType.BW);
- // }
- // catch (Exception ee)
- // {
- // Console.WriteLine("Error set PixelType BW " + ee.Message);
- // }
- // }
- // else
- // {
- // Console.WriteLine("Error " + DateTime.Now.ToString() + " PixelType BW not supported");
- // }
- // }
- // //测试灰度
- // if (hasGray)
- // {
- // try
- // {
- // engine.Capabilities.PixelType.Set(TwPixelType.Gray);
- // }
- // catch (Exception ee)
- // {
- // Console.WriteLine("Error set PixelType Gray " + ee.Message);
- // }
- // }
- // else
- // {
- // Console.WriteLine("Error " + DateTime.Now.ToString() + " PixelType Gray not supported");
- // }
- //}
- /// <summary>
- /// 设置灰度
- /// </summary>
- /// <param name="engine"></param>
- private static void set_gray(Twain32 engine)
- {
- Boolean hasBw = false;
- Boolean hasGray = false;
- Boolean hasColor = false;
- Twain32.Enumeration _pixelTypes = engine.Capabilities.PixelType.Get();
- for (int i = 0; i < _pixelTypes.Count; i++)
- {
- if (_pixelTypes[i].ToString().Equals("BW"))
- {
- hasBw = true;
- }
- if (_pixelTypes[i].ToString().Equals("Gray"))
- {
- hasGray = true;
- }
- if (_pixelTypes[i].ToString().Equals("RGB"))
- {
- hasColor = true;
- }
- }
- if (config_environment.web_parameter.isColor == 0)
- {
- if (hasGray)
- {
- try
- {
- engine.Capabilities.PixelType.Set(TwPixelType.Gray);
- }
- catch (Exception ee)
- {
- LogManager.WriteLog("Error set PixelType GRAY " + ee.Message);
- }
- }
- }
- //else if (config_environment.web_parameter.paperColor.ToLower() == "black")
- //{
- // if (hasBw)
- // {
- // try
- // {
- // engine.Capabilities.PixelType.Set(TwPixelType.BW);
- // }
- // catch (Exception ee)
- // {
- // LogManager.WriteLog("Error set PixelType BW " + ee.Message);
- // }
- // }
- //}
- else if (config_environment.web_parameter.isColor == 1)
- {
- if (hasColor)
- {
- try
- {
- engine.Capabilities.PixelType.Set(TwPixelType.RGB);
- }
- catch (Exception ee)
- {
- LogManager.WriteLog("Error set PixelType RGB " + ee.Message);
- }
- }
- }
- }
- /// <summary>
- /// 设置色深
- /// </summary>
- /// <param name="engine"></param>
- private static void set_bitDepth(Twain32 engine)
- {
- if (false)
- {
- try
- {
- engine.Capabilities.BitDepth.Set(1);
- }
- catch (Exception ee)
- {
- Console.WriteLine(DateTime.Now.ToString() + " Error set BitDepth 1" + ee.Message);
- }
- }
- }
- /// <summary>
- /// 设置保存格式
- /// </summary>
- /// <param name="engine"></param>
- private static void set_file_mode(Twain32 engine)
- {
- try
- {
- engine.Capabilities.XferMech.Set(TwSX.File);
- }
- catch (Exception ee)
- {
- Console.WriteLine("Error set XferMech TwSX.File " + ee.Message);
- }
- }
- /// <summary>
- /// 设置文件格式
- /// </summary>
- /// <param name="engine"></param>
- private static void set_file_type(Twain32 engine)
- {
- //try
- //{
- // Twain32.Enumeration imageFileFormats = engine.Capabilities.ImageFileFormat.Get();
- // Boolean hasTiff = false;
- // for (int i = 0; i < imageFileFormats.Count; i++)
- // {
- // if (imageFileFormats[i].ToString().ToLower().Equals("tiff"))
- // {
- // hasTiff = true;
- // break;
- // }
- // }
- // if (hasTiff)
- // {
- // engine.Capabilities.ImageFileFormat.Set(TwFF.Tiff);
- // }
- //}
- //catch (Exception ee)
- //{
- //}
- //try
- //{
- // engine.Capabilities.ImageFileFormat.Set(TwFF.Jfif);
- // //engine.Capabilities.JpegPixelType.Set(TwPixelType.Gray);
- // //engine.Capabilities.JpegQuality.Set(TwJQ.High);
- // engine.Capabilities.Compression.Set(TwCompression.Jpeg2000);
- //}
- //catch (Exception ee)
- //{
- //}
- try
- {
- engine.Capabilities.ImageFileFormat.Set(TwFF.Bmp);
- }
- catch (Exception ee)
- {
- }
- }
- /// <summary>
- /// 不明设置
- /// </summary>
- /// <param name="engine"></param>
- private static void set_removeBorder(Twain32 engine)
- {
- try
- {
- Twain32.Enumeration bRemoveBorderCap = (Twain32.Enumeration)engine.GetCap((TwCap)TwCapExt.bRemoveBorder);
- bool hasRemoveBorder = false;
- for (int i = 0; i < bRemoveBorderCap.Count; i++)
- {
- if (bRemoveBorderCap[i].ToString().Equals("1"))
- {
- hasRemoveBorder = true;
- break;
- }
- }
- if (hasRemoveBorder)
- {
- engine.SetCap((TwCap)TwCapExt.bRemoveBorder, 1);
- }
- }
- catch (Exception ee)
- {
- Console.WriteLine(DateTime.Now.ToString() + " error set bRemoveBorder " + ee.Message);
- }
- }
- }
- }
|