C#实现扫描枪扫描二维码并打印(实例代码)
1.使用usb口输入的扫描枪,这里实现使用了winform
首先创建一个CS文件
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Reflection;
usingSystem.Runtime.InteropServices;
usingSystem.Text;
namespaceam_sign
{
classBardCodeHooK
{
publicdelegatevoidBardCodeDeletegate(BarCodesbarCode);
publiceventBardCodeDeletegateBarCodeEvent;
publicstructBarCodes
{
publicintVirtKey;//虚拟吗
publicintScanCode;//扫描码
publicstringKeyName;//键名
publicuintAscll;//Ascll
publiccharChr;//字符
publicstringBarCode;//条码信息
publicboolIsValid;//条码是否有效
publicDateTimeTime;//扫描时间
}
privatestructEventMsg
{
publicintmessage;
publicintparamL;
publicintparamH;
publicintTime;
publicinthwnd;
}
[DllImport("user32.dll",CharSet=CharSet.Auto,CallingConvention=CallingConvention.StdCall)]
privatestaticexternintSetWindowsHookEx(intidHook,HookProclpfn,IntPtrhInstance,intthreadId);
[DllImport("user32.dll",CharSet=CharSet.Auto,CallingConvention=CallingConvention.StdCall)]
privatestaticexternboolUnhookWindowsHookEx(intidHook);
[DllImport("user32.dll",CharSet=CharSet.Auto,CallingConvention=CallingConvention.StdCall)]
privatestaticexternintCallNextHookEx(intidHook,intnCode,Int32wParam,IntPtrlParam);
[DllImport("user32",EntryPoint="GetKeyNameText")]
privatestaticexternintGetKeyNameText(intIParam,StringBuilderlpBuffer,intnSize);
[DllImport("user32",EntryPoint="GetKeyboardState")]
privatestaticexternintGetKeyboardState(byte[]pbKeyState);
[DllImport("user32",EntryPoint="ToAscii")]
privatestaticexternboolToAscii(intVirtualKey,intScanCode,byte[]lpKeySate,refuintlpChar,intuFlags);
delegateintHookProc(intnCode,Int32wParam,IntPtrlParam);
BarCodesbarCode=newBarCodes();
inthKeyboardHook=0;
stringstrBarCode="";
privateintKeyboardHookProc(intnCode,Int32wParam,IntPtrlParam)
{
if(nCode==0)
{
EventMsgmsg=(EventMsg)Marshal.PtrToStructure(lParam,typeof(EventMsg));
if(wParam==0x100)//WM_KEYDOWN=0x100
{
barCode.VirtKey=msg.message&0xff;//虚拟吗
barCode.ScanCode=msg.paramL&0xff;//扫描码
StringBuilderstrKeyName=newStringBuilder(225);
if(GetKeyNameText(barCode.ScanCode*65536,strKeyName,255)>0)
{
barCode.KeyName=strKeyName.ToString().Trim(newchar[]{'','\0'});
}
else
{
barCode.KeyName="";
}
byte[]kbArray=newbyte[256];
uintuKey=0;
GetKeyboardState(kbArray);
if(ToAscii(barCode.VirtKey,barCode.ScanCode,kbArray,refuKey,0))
{
barCode.Ascll=uKey;
barCode.Chr=Convert.ToChar(uKey);
}
TimeSpants=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;
}
}
returnCallNextHookEx(hKeyboardHook,nCode,wParam,lParam);
}
//安装钩子
publicboolStart()
{
if(hKeyboardHook==0)
{
//WH_KEYBOARD_LL=13
hKeyboardHook=SetWindowsHookEx(13,newHookProc(KeyboardHookProc),Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().GetModules()[0]),0);
}
return(hKeyboardHook!=0);
}
//卸载钩子
publicboolStop()
{
if(hKeyboardHook!=0)
{
returnUnhookWindowsHookEx(hKeyboardHook);
}
returntrue;
}
}
}
2.在winform中调用
BardCodeHooKBarCode=newBardCodeHooK();
publicForm1()
{
InitializeComponent();
BarCode.BarCodeEvent+=newBardCodeHooK.BardCodeDeletegate(BarCode_BarCodeEvent);
}
stringvalue="";//value为扫码枪获取的内容,以Enter结尾
privatedelegatevoidShowInfoDelegate(BardCodeHooK.BarCodesbarCode);
privatevoidShowInfo(BardCodeHooK.BarCodesbarCode)
{
if(this.InvokeRequired)
{
this.BeginInvoke(newShowInfoDelegate(ShowInfo),newobject[]{barCode});
}
else
{
if(barCode.KeyName.Equals("Enter"))
{
hook_KeyDown(value);
value="";
}
else
{
value+=barCode.Chr.ToString();
}
}
}
voidBarCode_BarCodeEvent(BardCodeHooK.BarCodesbarCode)
{
ShowInfo(barCode);
}
3.打印前,需要将TSLIB.dll文件放入c:\\windows\system下
usingSystem.Runtime.InteropServices;
namespaceam_sign
{
classTSCLIB_DLL
{
[DllImport("TSCLIB.dll",EntryPoint="about")]
publicstaticexternintabout();
[DllImport("TSCLIB.dll",EntryPoint="openport")]
publicstaticexternintopenport(stringprintername);
[DllImport("TSCLIB.dll",EntryPoint="barcode")]
publicstaticexternintbarcode(stringx,stringy,stringtype,
stringheight,stringreadable,stringrotation,
stringnarrow,stringwide,stringcode);
[DllImport("TSCLIB.dll",EntryPoint="clearbuffer")]
publicstaticexternintclearbuffer();
[DllImport("TSCLIB.dll",EntryPoint="closeport")]
publicstaticexternintcloseport();
[DllImport("TSCLIB.dll",EntryPoint="downloadpcx")]
publicstaticexternintdownloadpcx(stringfilename,stringimage_name);
[DllImport("TSCLIB.dll",EntryPoint="formfeed")]
publicstaticexternintformfeed();
[DllImport("TSCLIB.dll",EntryPoint="nobackfeed")]
publicstaticexternintnobackfeed();
[DllImport("TSCLIB.dll",EntryPoint="printerfont")]
publicstaticexternintprinterfont(stringx,stringy,stringfonttype,
stringrotation,stringxmul,stringymul,
stringtext);
[DllImport("TSCLIB.dll",EntryPoint="printlabel")]
publicstaticexternintprintlabel(stringset,stringcopy);
[DllImport("TSCLIB.dll",EntryPoint="sendcommand")]
publicstaticexternintsendcommand(stringprintercommand);
[DllImport("TSCLIB.dll",EntryPoint="setup")]
publicstaticexternintsetup(stringwidth,stringheight,
stringspeed,stringdensity,
stringsensor,stringvertical,
stringoffset);
[DllImport("TSCLIB.dll",EntryPoint="windowsfont")]
publicstaticexternintwindowsfont(intx,inty,intfontheight,
introtation,intfontstyle,intfontunderline,
stringszFaceName,stringcontent);
//打开打印机端口,并进行相关设置
publicstaticvoidopenportExt()
{
TSCLIB_DLL.openport("GprinterGP-3120TU");//找打打印机端口
TSCLIB_DLL.sendcommand("SIZE70mm,50mm");//设置条码大小
TSCLIB_DLL.sendcommand("GAP2mm,0");//设置条码间隙
TSCLIB_DLL.sendcommand("SPEED5");//设置打印速度
TSCLIB_DLL.sendcommand("DENSITY8");//设置墨汁浓度
TSCLIB_DLL.sendcommand("DERECTION1");//设置相对起点
TSCLIB_DLL.sendcommand("REFERENCE3mm,3mm");//设置偏移边框
TSCLIB_DLL.sendcommand("CLS");//清除记忆(每次打印新的条码时先清除上一次的打印记忆)
}
//打印在二维码
publicstaticvoidprintVehicleCode(stringname,stringdepartment,stringseat_area,stringqrcode)
{
TSCLIB_DLL.sendcommand("CLS");//需要清除上一次的打印记忆
TSCLIB_DLL.sendcommand("QRCODE207,180,L,6,A,0,M2,S3,\""+qrcode+"\"");
TSCLIB_DLL.windowsfont(200,30,60,0,2,0,"MicrosoftYaHei",name);
TSCLIB_DLL.windowsfont(210,100,30,0,0,0,"MicrosoftYaHei",department);
TSCLIB_DLL.windowsfont(20,320,40,0,0,0,"MicrosoftYaHei",seat_area);
TSCLIB_DLL.printlabel("1","1");
}
//关闭打印机端口
publicstaticvoidcloseportExt()
{
TSCLIB_DLL.closeport();
}
}
}
4.调用打印
TSCLIB_DLL.openportExt();//Openspecifiedprinterdriver TSCLIB_DLL.printVehicleCode(“测试”,“测试”,"测试",“二维码内容”); TSCLIB_DLL.closeport();
总结
以上所述是小编给大家介绍的C#实现扫描枪扫描二维码并打印,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对毛票票网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。