前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >调用打印机拍照的工具类

调用打印机拍照的工具类

作者头像
kiki.
发布2022-09-29 08:08:23
4360
发布2022-09-29 08:08:23
举报
文章被收录于专栏:web全栈之路
代码语言: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());
            }
        }
    }
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2017-08-31,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档