调用打印机拍照的工具类

2022-09-29 08:08:23 浏览数 (1)

代码语言:javascript复制
using AForge.Controls;
using AForge.Video;
using AForge.Video.DirectShow;
using Desktop.Protocol.Models.WhCommModels;
using Destop.Infrastucture.Helper.Log;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Media.Imaging;

namespace  ATM.Launcher.Helper
{
    /// <summary>
    /// 摄像头拍照
    /// </summary>
    public class TakePhotoHelper
    {
        private VideoSourcePlayer VideoSrcPlayer = new VideoSourcePlayer();
        private static readonly object lockobj = new object();
        private static TakePhotoHelper instance = null;
        private VideoCaptureDevice videoSource0 ;
        private TakePhotoHelper() { }

        public static TakePhotoHelper Instance
        {
            get
            {
                lock (lockobj)
                {
                    if (instance == null)
                    {
                        instance = new TakePhotoHelper();
                    }

                    return instance;
                }
            }
        }

        public bool Start()
        {
            try
            {
                FilterInfoCollection devices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
                if (devices.Count <= 0)
                {
                    return false;
                }
                videoSource0 = new VideoCaptureDevice(devices[0].MonikerString);
                videoSource0.DesiredFrameRate = 30;
                this.OpenVideoSource(videoSource0);
            }
            catch (Exception ex)
            {
                WMSLog.WriteLog(LogType.Error, ex.Message);
                return false;
            }
            return true;
        }

        private void OpenVideoSource(IVideoSource source)
        {
            VideoSrcPlayer.SignalToStop();
            VideoSrcPlayer.WaitForStop();
            VideoSrcPlayer.VideoSource = source;
            VideoSrcPlayer.Start();
        }

        /// <summary>
        /// 拍照
        /// </summary>
        /// <returns></returns>
        public bool TakePhoto(ref WhPhoto photoShotModel)
        {
            if (videoSource0 == null || !videoSource0.IsRunning)
            {
                return false;
            }
            try
            {
                if (VideoSrcPlayer == null) return false;
                System.Drawing.Bitmap bitmap = VideoSrcPlayer.GetCurrentVideoFrame();
                if (bitmap == null) return false;
                IntPtr ip = Image.FromHbitmap(bitmap.GetHbitmap()).GetHbitmap();
                BitmapSource bitmapSource = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
                    ip, IntPtr.Zero, Int32Rect.Empty, System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());
                bitmap.Dispose();

                BitmapEncoder encoder = new JpegBitmapEncoder();
                encoder.Frames.Add(BitmapFrame.Create(bitmapSource));

                System.IO.MemoryStream pngMemStream = new MemoryStream();
                encoder.Save(pngMemStream);
                pngMemStream.Close();

                byte[] imageBytes = pngMemStream.ToArray();
                string url = Convert.ToBase64String(imageBytes);
                photoShotModel = new WhPhoto();
                photoShotModel.photo = url;
                return true;
            }
            catch (Exception ex)
            {
                WMSLog.WriteLog(LogType.Error, "拍照失败:"   ex.ToString());
                return false;
            }
        }

        /// <summary>
        /// 关闭摄像头
        /// </summary>
        public void Stop()
        {
            try
            {
                if (VideoSrcPlayer != null)
                {
                    //停止VideoSrcPlayer线程,否则无法关闭窗体
                    VideoSrcPlayer.SignalToStop();
                    VideoSrcPlayer.WaitForStop();
                    VideoSrcPlayer.Dispose();
                    videoSource0 = null;
                }
            }
            catch (Exception ex)
            {
                WMSLog.WriteLog(LogType.Error, "关闭摄像头失败:"   ex.ToString());
            }
        }
    }
}

0 人点赞