调用打印机拍照的工具类

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

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

kiki·

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

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

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

打赏作者

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

抵扣说明:

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

余额充值