欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#实现扫描枪扫描二维码并打印(实例代码)

程序员文章站 2023-02-20 20:43:42
1.使用usb口输入的扫描枪,这里实现使用了winform 首先创建一个cs文件 using system; using system.collections.gener...

1.使用usb口输入的扫描枪,这里实现使用了winform

首先创建一个cs文件

using system;
using system.collections.generic;
using system.linq;
using system.reflection;
using system.runtime.interopservices;
using system.text;
namespace am_sign
{
 class bardcodehook
 {
  public delegate void bardcodedeletegate(barcodes barcode);
  public event bardcodedeletegate barcodeevent;
  public struct barcodes
  {
   public int virtkey;//虚拟吗
   public int scancode;//扫描码
   public string keyname;//键名
   public uint ascll;//ascll
   public char chr;//字符
   public string barcode;//条码信息
   public bool isvalid;//条码是否有效
   public datetime time;//扫描时间
  }
  private struct eventmsg
  {
   public int message;
   public int paraml;
   public int paramh;
   public int time;
   public int hwnd;
  }
  [dllimport("user32.dll", charset = charset.auto, callingconvention = callingconvention.stdcall)]
  private static extern int setwindowshookex(int idhook, hookproc lpfn, intptr hinstance, int threadid);
  [dllimport("user32.dll", charset = charset.auto, callingconvention = callingconvention.stdcall)]
  private static extern bool unhookwindowshookex(int idhook);
  [dllimport("user32.dll", charset = charset.auto, callingconvention = callingconvention.stdcall)]
  private static extern int callnexthookex(int idhook, int ncode, int32 wparam, intptr lparam);
  [dllimport("user32", entrypoint = "getkeynametext")]
  private static extern int getkeynametext(int iparam, stringbuilder lpbuffer, int nsize);
  [dllimport("user32", entrypoint = "getkeyboardstate")]
  private static extern int getkeyboardstate(byte[] pbkeystate);
  [dllimport("user32", entrypoint = "toascii")]
  private static extern bool toascii(int virtualkey, int scancode, byte[] lpkeysate, ref uint lpchar, int uflags);
  delegate int hookproc(int ncode, int32 wparam, intptr lparam);
  barcodes barcode = new barcodes();
  int hkeyboardhook = 0;
  string strbarcode = "";
  private int keyboardhookproc(int ncode, int32 wparam, intptr lparam)
  {
   if (ncode == 0)
   {
    eventmsg msg = (eventmsg)marshal.ptrtostructure(lparam, typeof(eventmsg));
    if (wparam == 0x100)//wm_keydown=0x100
    {
     barcode.virtkey = msg.message & 0xff;//虚拟吗
     barcode.scancode = msg.paraml & 0xff;//扫描码
     stringbuilder strkeyname = new stringbuilder(225);
     if (getkeynametext(barcode.scancode * 65536, strkeyname, 255) > 0)
     {
      barcode.keyname = strkeyname.tostring().trim(new char[] { ' ', '\0' });
     }
     else
     {
      barcode.keyname = "";
     }
     byte[] kbarray = new byte[256];
     uint ukey = 0;
     getkeyboardstate(kbarray);
     if (toascii(barcode.virtkey, barcode.scancode, kbarray, ref ukey, 0))
     {
      barcode.ascll = ukey;
      barcode.chr = convert.tochar(ukey);
     }
     timespan ts = datetime.now.subtract(barcode.time);
     if (ts.totalmilliseconds > 50)
     {
      strbarcode = barcode.chr.tostring();
     }
     else
     {
      if ((msg.message & 0xff) == 13 && strbarcode.length > 3)
      {
       barcode.barcode = strbarcode;
       barcode.isvalid = true;
      }
      strbarcode += barcode.chr.tostring();
     }
     barcode.time = datetime.now;
     if (barcodeevent != null) barcodeevent(barcode);//触发事件
     barcode.isvalid = false;
    }
   }
   return callnexthookex(hkeyboardhook, ncode, wparam, lparam);
  }
  //安装钩子
  public bool start()
  {
   if (hkeyboardhook == 0)
   {
    //wh_keyboard_ll=13
    hkeyboardhook = setwindowshookex(13, new hookproc(keyboardhookproc), marshal.gethinstance(assembly.getexecutingassembly().getmodules()[0]), 0);
   }
   return (hkeyboardhook != 0);
  }
  //卸载钩子
  public bool stop()
  {
   if (hkeyboardhook != 0)
   {
    return unhookwindowshookex(hkeyboardhook);
   }
   return true;
  }
 }
}

2.在winform中调用

 bardcodehook barcode = new bardcodehook();
  public form1()
  {
   initializecomponent();
   barcode.barcodeevent += new bardcodehook.bardcodedeletegate(barcode_barcodeevent);
  }
  string value = ""; //value为扫码枪获取的内容,以enter结尾
  private delegate void showinfodelegate(bardcodehook.barcodes barcode);
  private void showinfo(bardcodehook.barcodes barcode)
  {
   if (this.invokerequired)
   {
    this.begininvoke(new showinfodelegate(showinfo), new object[] { barcode });
   }
   else
   {
    if (barcode.keyname.equals("enter"))
    {
     hook_keydown(value);
     value = "";
    }
    else
    {
     value += barcode.chr.tostring();
    }
   }
  }
  void barcode_barcodeevent(bardcodehook.barcodes barcode)
  {
   showinfo(barcode);
  }

3.打印前,需要将tslib.dll文件放入c:\\windows\system下

using system.runtime.interopservices;
namespace am_sign
{
 class tsclib_dll
 {
  [dllimport("tsclib.dll", entrypoint = "about")]
  public static extern int about();
  [dllimport("tsclib.dll", entrypoint = "openport")]
  public static extern int openport(string printername);
  [dllimport("tsclib.dll", entrypoint = "barcode")]
  public static extern int barcode(string x, string y, string type,
      string height, string readable, string rotation,
      string narrow, string wide, string code);
  [dllimport("tsclib.dll", entrypoint = "clearbuffer")]
  public static extern int clearbuffer();
  [dllimport("tsclib.dll", entrypoint = "closeport")]
  public static extern int closeport();
  [dllimport("tsclib.dll", entrypoint = "downloadpcx")]
  public static extern int downloadpcx(string filename, string image_name);
  [dllimport("tsclib.dll", entrypoint = "formfeed")]
  public static extern int formfeed();
  [dllimport("tsclib.dll", entrypoint = "nobackfeed")]
  public static extern int nobackfeed();
  [dllimport("tsclib.dll", entrypoint = "printerfont")]
  public static extern int printerfont(string x, string y, string fonttype,
       string rotation, string xmul, string ymul,
       string text);
  [dllimport("tsclib.dll", entrypoint = "printlabel")]
  public static extern int printlabel(string set, string copy);
  [dllimport("tsclib.dll", entrypoint = "sendcommand")]
  public static extern int sendcommand(string printercommand);
  [dllimport("tsclib.dll", entrypoint = "setup")]
  public static extern int setup(string width, string height,
      string speed, string density,
      string sensor, string vertical,
      string offset);
  [dllimport("tsclib.dll", entrypoint = "windowsfont")]
  public static extern int windowsfont(int x, int y, int fontheight,
   int rotation, int fontstyle, int fontunderline,
       string szfacename, string content);
  //打开打印机端口,并进行相关设置
  public static void openportext()
  {
   tsclib_dll.openport("gprinter gp-3120tu");//找打打印机端口
   tsclib_dll.sendcommand("size 70 mm,50 mm");//设置条码大小
   tsclib_dll.sendcommand("gap 2 mm,0");//设置条码间隙
   tsclib_dll.sendcommand("speed 5");//设置打印速度
   tsclib_dll.sendcommand("density 8");//设置墨汁浓度
   tsclib_dll.sendcommand("derection 1");//设置相对起点
   tsclib_dll.sendcommand("reference 3 mm,3 mm");//设置偏移边框
   tsclib_dll.sendcommand("cls");//清除记忆(每次打印新的条码时先清除上一次的打印记忆)
  }
  //打印在二维码
  public static void printvehiclecode(string name, string department, string seat_area,string qrcode)
  {
   tsclib_dll.sendcommand("cls");//需要清除上一次的打印记忆
   tsclib_dll.sendcommand("qrcode 207,180,l,6,a,0,m2,s3,\"" + qrcode + "\"");
   tsclib_dll.windowsfont(200, 30, 60, 0, 2, 0, "microsoft yahei", name);
   tsclib_dll.windowsfont(210, 100, 30, 0, 0, 0, "microsoft yahei", department);
   tsclib_dll.windowsfont(20, 320, 40, 0, 0, 0, "microsoft yahei", seat_area);
   tsclib_dll.printlabel("1", "1");
  }
  //关闭打印机端口
  public static void closeportext()
  {
   tsclib_dll.closeport();
  }
 }
}

4.调用打印

 tsclib_dll.openportext();           //open specified printer driver
 tsclib_dll.printvehiclecode(“测试”, “测试”, "测试", “二维码内容”);
 tsclib_dll.closeport();

总结

以上所述是小编给大家介绍的c#实现扫描枪扫描二维码并打印,希望对大家有所帮助