调用打印机拍照的工具类
工具 调用 打印机 拍照
2023-06-13 09:13:29 时间
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());
}
}
}
}
相关文章
- 抓包工具Charles下载安装使用
- java 死链检测_网站死链检测工具/网站地图生成工具「建议收藏」
- Android开发入门工具的那些事儿
- 站长第一个ShinyApp:AnnoE上线了!欢迎来使用~欢迎关注公众号,一大波小工具正在赶来~
- 数据库审核工具SQLE接口调用
- 实用的在线api接口工具
- 【愚公系列】2023年03月 .NET CORE工具案例-C#调用Python的二种方式
- 【Android 逆向】IDA 工具使用 ( 函数窗口 Function window | 创建引用图 Xrefs graph to | 创建调用图 Xrefs graph from )
- 【Android 逆向】Android 进程注入工具开发 ( 注入代码分析 | 获取 linker 中的 dlopen 函数地址 并 通过 远程调用 执行该函数 )
- 美国CISA发布勒索软件就绪评估(RRA)工具
- Redis+Jedis封装工具类详解大数据
- 工具解锁Linux脚本密码:一款强大的解密工具(linux脚本解密)
- 使用命令行工具 Graudit 来查找你代码中的安全漏洞
- 13款Linux运维比较实用的工具
- MySQL空间函数:操作地理空间数据的强大工具(mysql空间函数)
- 苹果被指两面三刀:表面配合安全研究公司 暗地起诉研究工具提供商