QRCodeScanner.cs 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. using System;
  2. using System.Text;
  3. using System.Threading;
  4. using System.Threading.Tasks;
  5. using tool;
  6. namespace tool
  7. {
  8. public class QRCodeScanner : ScannerInterface
  9. {
  10. Vbarapi vbarapi;
  11. string decoderesult = string.Empty;
  12. bool m_statu;
  13. public QRCodeScanner()
  14. {
  15. m_statu = false;
  16. //this.vbarapi = vbarapi;
  17. vbarapi = new Vbarapi();
  18. if (vbarapi.openDevice())
  19. {
  20. //StatusSettings.scanStatus = true;
  21. Log.Instance.WriteLog(LogType.PROCESS, LogFile.LOG, "扫描设备连接成功");
  22. }
  23. else
  24. {
  25. //StatusSettings.scanStatus = false;
  26. Log.Instance.WriteLog(LogType.PROCESS, LogFile.LOG, "扫描设备连接失败");
  27. }
  28. vbarapi.backlight(true);//打开背光
  29. vbarapi.controlScan(true);//打开扫码
  30. }
  31. public void ScanInit()
  32. {
  33. m_statu = true;
  34. }
  35. private string Decoder()
  36. {
  37. byte[] result;
  38. string sResult = null;
  39. int size;
  40. if (vbarapi.getResultStr(out result, out size))
  41. {
  42. Log.Instance.WriteLog(LogType.PROCESS, LogFile.LOG, "vbar扫描结果:" + result);
  43. //以默认的方式读取结果字节
  44. string msg = System.Text.Encoding.UTF8.GetString(result);
  45. byte[] buffer = Encoding.UTF8.GetBytes(msg);
  46. sResult = Encoding.UTF8.GetString(buffer, 0, buffer.Length);
  47. }
  48. else
  49. {
  50. Log.Instance.WriteLog(LogType.PROCESS, LogFile.LOG, "vbar未扫描到结果");
  51. sResult = null;
  52. }
  53. return sResult;
  54. }
  55. public string Decode()
  56. {
  57. Task decodeTask = Task.Factory.StartNew(() =>
  58. {
  59. ScanInit();
  60. while (m_statu)
  61. {
  62. decoderesult = Decoder();
  63. //if (!(decoderesult.Length == 0))
  64. if (decoderesult != null && decoderesult != "" && decoderesult != string.Empty)
  65. {
  66. Log.Instance.WriteLog(LogType.PROCESS, LogFile.LOG, "扫描结果:" + decoderesult);
  67. break;
  68. }
  69. Thread.Sleep(200);
  70. }
  71. });
  72. decodeTask.Wait();
  73. return decoderesult;
  74. }
  75. public void Close()
  76. {
  77. vbarapi.backlight(false);//关闭背光
  78. vbarapi.controlScan(false);//关闭扫码
  79. }
  80. }
  81. }