UsrCamera.cs 4.2 KB
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using BLL;
using DocumentFormat.OpenXml.Spreadsheet;
using Model;

namespace SmartScan
{
    public partial class UsrCamera : UserControl, ISetMenu
    {
        private Bitmap bmp = null;

        public UsrCamera()
        {
            InitializeComponent();
            Asa.FaceControl.Language.SetLanguage(this);
            BtnCameraImage.Enabled = BLLCommon.cameraVision == null ? false : BLLCommon.cameraVision.Count > 0;
        }

        public Asa.FaceControl.FacePanel GetPanel()
        {
            return facePanel1;
        }

        public void Save() { }



        private void BtnOpenLight_Click(object sender, EventArgs e)
        {
            BLLCommon.lightSource.TurnOn();
        }

        private void BtnCloseLight_Click(object sender, EventArgs e)
        {
            BLLCommon.lightSource.TurnOff();
        }

        private void BtnCameraImage_Click(object sender, EventArgs e)
        {
            BLLCommon.cameraVision.Open();
            Bitmap[] result = BLLCommon.cameraVision.GetImage();
            BLLCommon.cameraVision.Close();
            if (result.Length == 0) return;
            bmp = result[0];
            LstCode.Items.Clear();
            PicShow.CodeCenterClear();
            PicShow.Image = bmp;
        }

        private void BtnLocalImage_Click(object sender, EventArgs e)
        {
            using OpenFileDialog dlg = new() { Filter = "Image Files|*.jpg;*.jpeg;*.png;*.bmp;*.gif|All Files|*.*" };
            if (dlg.ShowDialog() != DialogResult.OK) return;
            bmp = ObjConversion.ReadImageFile(dlg.FileName);

            LstCode.Items.Clear();
            PicShow.CodeCenterClear();
            PicShow.Image = bmp;
        }

        private void BtnScanCode_Click(object sender, EventArgs e)
        {
            if (bmp == null) return;
            List<CameraVisionLib.Model.BarcodeInfo> info = BLLCommon.cameraVision.GetBarCode(bmp);
            LstCode.Items.Clear();
            PicShow.CodeCenterClear();
            string[] arr = new string[info.Count];
            PointF[] lst = new PointF[info.Count];
            for (int i = 0; i < info.Count; i++)
            {
                arr[i] = string.Format("({0}) ", i + 1) + info[i].Text.Replace("\r\n", "");
                lst[i] = info[i].Center;
            }
            List<string> ocrs = new List<string>();
            int startidx = info.Count + 1;
            if (Config.Func_EnabledOCR)
            {
                //保存需要识别ocr的区域
                bmp?.Save(@"ocrt.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
                string[] regOcrCodes = PaddleOCRHelper.StartTest("..\\ocrt.jpg").Split(';');
                foreach (var ocr in regOcrCodes)
                {
                    if (string.IsNullOrEmpty(ocr)) continue;
                    ocrs.Add($"({startidx}) {ocr} <OCR>");
                    startidx++;
                }
            }
            if (info.Count == 0)
            {
                string text = Asa.FaceControl.Language.Dialog(Model.LanguageDialogKey.CODE_COUNT);
                new Asa.FaceControl.FaceMessageBox("", text, MessageBoxButtons.OK).ShowDialog();
                return;
            }
            LstCode.Items.AddRange(arr);
            if (ocrs.Count > 0)
                LstCode.Items.AddRange(ocrs.ToArray());
            PicShow.AddCodeCenter(lst);
        }

        private void BtnSaveImage_Click(object sender, EventArgs e)
        {
            SaveFileDialog dlg = new() { Filter = "JPEG图片|*.jpg|BMP图片|*.bmp|PNG图片|*.png" };
            if (dlg.ShowDialog() != DialogResult.OK) return;

            switch (dlg.FilterIndex)
            {
                case 0: bmp.Save(dlg.FileName, System.Drawing.Imaging.ImageFormat.Jpeg); break;
                case 1: bmp.Save(dlg.FileName, System.Drawing.Imaging.ImageFormat.Bmp); break;
                case 2: bmp.Save(dlg.FileName, System.Drawing.Imaging.ImageFormat.Png); break;
            }

            string text = Asa.FaceControl.Language.Dialog(LanguageDialogKey.SAVE_SUCCEED);
            new Asa.FaceControl.FaceMessageBox("", text, MessageBoxButtons.OK).ShowDialog();
        }
    }
}