using SYS_TEST.BaseClass;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using ZXing;
namespace FT_Tools
{
public partial class FormTest : Form
{
public string templatePathName = "F210/FTBZ189_F210_device_20211207.bmp"; //资源号或本地文件名
private void FormTest_Load(object sender, EventArgs e)
{
Log("模版:" + templatePathName);
pictureBox.Image = MyTag.Tag.getPictureF210Device(Log, templatePathName, "F210Q9234560001", "867990040136001", "867880040136001");
GetClipboard();
//Log("文件:" + fileName + " 识别内容为:");
Result[] rArray = (new BarcodeReader()).DecodeMultiple((Bitmap)pictureBox.Image);
//string qrCodeContent = "";
if (rArray != null)
{
foreach (Result r in rArray)
{
//qrCodeContent += r.Text;
Log(r.Text);
}
}
}
public void GetClipboard()
{
Log("自动从剪切(粘贴)板获取数据");
IDataObject iData = Clipboard.GetDataObject();
if (iData.GetDataPresent(DataFormats.Bitmap))
{
var img = Clipboard.GetImage();
//pictureBox.Tag = Guid.NewGuid();
pictureBox.Image = img;
}
else if (iData.GetDataPresent(DataFormats.MetafilePict))
{
var img = Clipboard.GetImage();
//pictureBox.Tag = Guid.NewGuid();
pictureBox.Image = img;
}
else if (iData.GetDataPresent(DataFormats.FileDrop))
{
var files = Clipboard.GetFileDropList();
if (files.Count == 0) { return; }
//pictureBox.Tag = Guid.NewGuid();
pictureBox.Image = Image.FromFile(files[0]);
}
else if (iData.GetDataPresent(DataFormats.Text))
{
var path = (String)iData.GetData(DataFormats.Text);
var chars = Path.GetInvalidPathChars();
if (path.IndexOfAny(chars) >= 0)
{
Log("路径中包含非法字符!");
return;
}
if (System.IO.File.Exists(path))
{
var name = Path.GetFileNameWithoutExtension(path);
var extension = path.Substring(path.LastIndexOf("."));
string imgType = ".png|.jpg|.jpeg|.bmp";
if (imgType.Contains(extension.ToLower()))
{
pictureBox.Image = Image.FromFile(path);
//pictureBox.Tag = Guid.NewGuid();
}
else
{
Log("格式错误!");
}
}
else
{
Log("文件不存在!");
}
}
else
{
Log("剪切(粘贴)板为空!");
}
}
private void buttonPrint_Click(object sender, EventArgs e)
{
try
{
int num = 1;
new Thread(delegate () {
Log("打印开始,数量:" + num);
for (int i = 1; i <= num; i++)
{
string sn = String.Format("F210Q9234560{0:D3}", i);
string imei1 = String.Format("867990040136{0:D3}", i);
string imei2 = String.Format("867880040136{0:D3}", i);
Log("打印:" + sn+" " +imei1 +" "+ imei2);
Image image = MyTag.Tag.getPictureF210Device(Log, templatePathName, sn, imei1, imei2);
if (null == image)
{
Log("错误 合成图片错误,退出打印" + sn);
return;
}
SYS_TEST.BaseClass.PrintDirectClass pc = new PrintDirectClass(image); //打印
if (pc.result == PrintDirectClass.PRINT_FINISH)
{
Log(sn + "打印完成,还剩" + (num - (i + 1)) + "份");
}
else
{
Log("退出打印" + sn + "原因:" + pc.result);
return;
}
}
Log("打印结束");
}).Start();
}
catch (Exception e2)
{
Log("错误 " + e2);
}
}
public FormTest()
{
InitializeComponent();
}
public void Log(string str)
{
MyLog.MyLog.WriteLogs(MyLog.MyLog.fileName, "", str);
this.Invoke((EventHandler)(delegate {
textBoxLog.Text += str + "\r\n";
textBoxLog.SelectionStart = textBoxLog.Text.Length;
textBoxLog.ScrollToCaret();
}));
}
private void pictureBox_Click(object sender, EventArgs e)
{
try
{
Process.Start(@"cmd", @"/c start result.bmp"); //打开结果文件
}
catch (Exception e2)
{
Log("未找到文件" + e2.ToString());
}
}
private void button1_Click(object sender, EventArgs e)
{
textBoxLog.Text = ""; //清空还原
OpenFileDialog file = new OpenFileDialog();
file.InitialDirectory = ".";
file.Filter = "所有文件(*.*)|*.*;*.BMP";
file.Multiselect = true;
file.ShowDialog();
if (file.FileName != string.Empty)
{
try
{
foreach (string fileName in file.FileNames)
{
pictureBox.Load(fileName);
Log("文件:" + fileName+" 识别内容为:");
Result[] rArray = (new BarcodeReader()).DecodeMultiple((Bitmap)pictureBox.Image);
//string qrCodeContent = "";
if (rArray != null)
{
foreach (Result r in rArray)
{
//qrCodeContent += r.Text;
Log(r.Text);
}
}
}
}
catch (Exception ex)
{
Log("错误 " + ex.Message);
}
}
}
}
}