UsrCamera.cs 3.4 KB
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using Model;

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

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

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

        public void Save() { }



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

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

        private void BtnCameraImage_Click(object sender, EventArgs e)
        {
            Common.cameraVision.Open();
            Bitmap[] result = Common.cameraVision.GetImage();
            Common.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 = Common.cameraVision.GetBarCode(bmp);
            LstCode.Items.Clear();
            PicShow.CodeCenterClear();
            if (info.Count == 0)
            {
                string text = Asa.FaceControl.Language.Dialog(Model.LanguageDialogKey.CODE_COUNT);
                new Asa.FaceControl.FaceMessageBox("", text, MessageBoxButtons.OK).ShowDialog();
                return;
            }

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

            LstCode.Items.AddRange(arr);
            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();
        }
    }
}