181 lines
7.5 KiB
C#
181 lines
7.5 KiB
C#
using DevExpress.XtraEditors;
|
|
using System;
|
|
using System.Drawing;
|
|
using System.Runtime.InteropServices;
|
|
using ThoughtWorks.QRCode.Codec;
|
|
using ZXing;
|
|
using ZXing.Common;
|
|
using ZXing.QrCode;
|
|
|
|
namespace HL_FristAidPlatform_Public
|
|
{
|
|
public partial class Form_QRcode : XtraForm
|
|
{
|
|
string patientGUID;
|
|
string name;
|
|
string gender;
|
|
string age;
|
|
public Form_QRcode(string _patientGUID = "", string _name = "", string _gender = "", string _age = "")
|
|
{
|
|
patientGUID = _patientGUID;
|
|
name = _name;
|
|
gender = _gender;
|
|
age = _age;
|
|
InitializeComponent();
|
|
}
|
|
|
|
private void Form_QRcode_Load(object sender, EventArgs e)
|
|
{
|
|
labelControl2.Text = name;
|
|
labelControl4.Text = gender;
|
|
labelControl6.Text = age;
|
|
|
|
pictureEdit1.Image = GetDimensionalCode(patientGUID);
|
|
}
|
|
|
|
public Bitmap CodeImage(string str)
|
|
{
|
|
//实例化一个生成二维码的对象
|
|
QRCodeEncoder qrEncoder = new QRCodeEncoder();
|
|
//设置二维码的编码模式
|
|
qrEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE;
|
|
//二维码像素宽度
|
|
qrEncoder.QRCodeScale = 4;
|
|
//设置版本
|
|
qrEncoder.QRCodeVersion = 0;
|
|
//根据内容生成二维码图像
|
|
Bitmap image = null;
|
|
image = qrEncoder.Encode(str);
|
|
return image;
|
|
}
|
|
/// <summary>
|
|
/// 根据链接获取二维码
|
|
/// </summary>
|
|
/// <param name="link">链接</param>
|
|
/// <returns>返回二维码图片</returns>
|
|
private Bitmap GetDimensionalCode(string link)
|
|
{
|
|
Bitmap bmp = null;
|
|
try
|
|
{
|
|
QRCodeEncoder qrCodeEncoder = new QRCodeEncoder();
|
|
qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE;
|
|
qrCodeEncoder.QRCodeScale = 4;
|
|
//int version = Convert.ToInt16(cboVersion.Text);
|
|
qrCodeEncoder.QRCodeVersion = 7;
|
|
qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M;
|
|
bmp = qrCodeEncoder.Encode(link);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
//MessageBox.Show("Invalid version !");
|
|
}
|
|
return bmp;
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 生成条形码
|
|
/// </summary>
|
|
/// <param name="barcodeContent">需要生成条码的内容</param>
|
|
/// <param name="barcodeWidth">条码宽度</param>
|
|
/// <param name="barcodeHeight">条码长度</param>
|
|
/// <returns>返回条码图形</returns>
|
|
public static Bitmap GetBarcodeBitmap(string barcodeContent, int barcodeWidth, int barcodeHeight)
|
|
{
|
|
BarcodeWriter barcodeWriter = new BarcodeWriter();
|
|
barcodeWriter.Format = BarcodeFormat.CODE_39;//设置编码格式
|
|
EncodingOptions encodingOptions = new EncodingOptions();
|
|
encodingOptions.Width = barcodeWidth;//设置宽度
|
|
encodingOptions.Height = barcodeHeight;//设置长度
|
|
encodingOptions.Margin = 2;//设置边距
|
|
barcodeWriter.Options = encodingOptions;
|
|
Bitmap bitmap = barcodeWriter.Write(barcodeContent);
|
|
return bitmap;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 生成二维码
|
|
/// </summary>
|
|
/// <param name="qrCodeContent">要生成二维码的内容</param>
|
|
/// <param name="qrCodeWidth">二维码宽度</param>
|
|
/// <param name="qrCodeHeight">二维码高度</param>
|
|
/// <returns>返回二维码图片</returns>
|
|
public static Bitmap GetQRCodeBitmap(string qrCodeContent, int qrCodeWidth, int qrCodeHeight)
|
|
{
|
|
BarcodeWriter barcodeWriter = new BarcodeWriter();
|
|
barcodeWriter.Format = BarcodeFormat.QR_CODE;
|
|
QrCodeEncodingOptions qrCodeEncodingOptions = new QrCodeEncodingOptions();
|
|
qrCodeEncodingOptions.DisableECI = true;
|
|
qrCodeEncodingOptions.CharacterSet = "UTF-8";//设置编码
|
|
qrCodeEncodingOptions.Width = qrCodeWidth;//设置二维码宽度
|
|
qrCodeEncodingOptions.Height = qrCodeHeight;//设置二维码高度
|
|
qrCodeEncodingOptions.Margin = 0;//设置二维码边距
|
|
|
|
barcodeWriter.Options = qrCodeEncodingOptions;
|
|
Bitmap bitmap = barcodeWriter.Write(qrCodeContent);//写入内容
|
|
return bitmap;
|
|
}
|
|
|
|
|
|
#region 打印二维码
|
|
[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 printQrCode(string barcode)
|
|
//{
|
|
// //TSCLIB_DLL.about(); //Show the DLL version
|
|
// BarcodeHelper.openport("TSC TTP-342 PRO"); //Open specified printer driver
|
|
// BarcodeHelper.setup("77.5", "50", "2", "15", "0", "6", "0"); //Setup the media size and sensor type info
|
|
// BarcodeHelper.clearbuffer();
|
|
// BarcodeHelper.sendcommand("DIRECTION 0");
|
|
// BarcodeHelper.windowsfont(137, 305, 40, 0, 0, 0, "宋体", barcode);
|
|
// BarcodeHelper.sendcommand("QRCODE 212,58,L,10,A,0,M2,S7,\"" + barcode + "\"" + "\r");
|
|
// BarcodeHelper.printlabel("1", "1"); //Print labels
|
|
// BarcodeHelper.closeport();
|
|
//}
|
|
////关闭打印机端口
|
|
//public static void closeportExt()
|
|
//{
|
|
// BarcodeHelper.closeport();
|
|
//}
|
|
|
|
|
|
#endregion
|
|
}
|
|
} |