C#自动获取剪切(粘贴)板的内容(图片、文字、路径等)并批量识别出二维码条型码

46 篇文章 1 订阅

 

 

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);
                }
            }
        }
    }
}

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小黄人软件

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值