1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 |
- using System;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- using tool;
- namespace tool
- {
- public class QRCodeScanner : ScannerInterface
- {
- Vbarapi vbarapi;
- string decoderesult = string.Empty;
- bool m_statu;
- public QRCodeScanner()
- {
- m_statu = false;
- //this.vbarapi = vbarapi;
- vbarapi = new Vbarapi();
- if (vbarapi.openDevice())
- {
- //StatusSettings.scanStatus = true;
- Log.Instance.WriteLog(LogType.PROCESS, LogFile.LOG, "扫描设备连接成功");
- }
- else
- {
- //StatusSettings.scanStatus = false;
- Log.Instance.WriteLog(LogType.PROCESS, LogFile.LOG, "扫描设备连接失败");
- }
- vbarapi.backlight(true);//打开背光
- vbarapi.controlScan(true);//打开扫码
- }
- public void ScanInit()
- {
- m_statu = true;
- }
- private string Decoder()
- {
- byte[] result;
- string sResult = null;
- int size;
- if (vbarapi.getResultStr(out result, out size))
- {
- Log.Instance.WriteLog(LogType.PROCESS, LogFile.LOG, "vbar扫描结果:" + result);
- //以默认的方式读取结果字节
- string msg = System.Text.Encoding.UTF8.GetString(result);
- byte[] buffer = Encoding.UTF8.GetBytes(msg);
- sResult = Encoding.UTF8.GetString(buffer, 0, buffer.Length);
- }
- else
- {
- Log.Instance.WriteLog(LogType.PROCESS, LogFile.LOG, "vbar未扫描到结果");
- sResult = null;
- }
- return sResult;
- }
- public string Decode()
- {
- Task decodeTask = Task.Factory.StartNew(() =>
- {
- ScanInit();
- while (m_statu)
- {
- decoderesult = Decoder();
- //if (!(decoderesult.Length == 0))
- if (decoderesult != null && decoderesult != "" && decoderesult != string.Empty)
- {
- Log.Instance.WriteLog(LogType.PROCESS, LogFile.LOG, "扫描结果:" + decoderesult);
- break;
- }
- Thread.Sleep(200);
- }
- });
- decodeTask.Wait();
- return decoderesult;
- }
- public void Close()
- {
- vbarapi.backlight(false);//关闭背光
- vbarapi.controlScan(false);//关闭扫码
- }
- }
- }
|