You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
241 lines
9.1 KiB
241 lines
9.1 KiB
|
|
using DevicesService.Commen; |
|
using Functions.FileExt; |
|
using System; |
|
using System.Collections.Generic; |
|
using System.Configuration; |
|
using System.Drawing; |
|
using System.Globalization; |
|
using System.IO; |
|
using System.Linq; |
|
using System.Net.Http; |
|
using System.Text; |
|
using System.Threading.Tasks; |
|
|
|
namespace DevicesService.Devices |
|
{ |
|
using HELOAMIMAGE = IntPtr; |
|
using HELOAMIMAGELIST = IntPtr; |
|
using HELOAMFTP = IntPtr; |
|
using HELOAMHTTP = IntPtr; |
|
using HELOAMDEVICE = IntPtr; |
|
using HELOAMVIDEO = IntPtr; |
|
using HELOAMVIEW = IntPtr; |
|
using HELOAMMEMORY = IntPtr; |
|
using HELOAMBASE64 = IntPtr; |
|
using HELOAMTHUMBNAIL = IntPtr; |
|
using HELOAMRECT = IntPtr; |
|
using HELOAMFONT = IntPtr; |
|
using HELOAMVIDEOCAP = IntPtr; |
|
using LPVOID = IntPtr; |
|
using LONG = Int32; |
|
using BOOL = Int32; |
|
using HWND = IntPtr; |
|
using COLORREF = UInt32; |
|
|
|
// callback |
|
public delegate void ELOAM_DEVCHANGECALLBACK(LONG type, LONG idx, LONG dbt, LPVOID userData); |
|
public delegate void ELOAM_ARRIVALCALLBACK(HELOAMVIDEO video, LONG id, LPVOID userData); |
|
public delegate void ELOAM_TOUCHCALLBACK(HELOAMVIDEO video, LPVOID userData); |
|
public delegate void ELOAM_MOVEDETECCALLBACK(HELOAMVIDEO video, LONG id, LPVOID userData); |
|
public delegate void ELOAM_ATTACHCALLBAK(HELOAMVIDEO video, LONG videoId, HELOAMVIEW view, LONG viewId, LPVOID userData); |
|
|
|
public delegate void ELOAM_IDCARDCHANGECALLBACK(LONG dbt, LPVOID userData); |
|
//ocr |
|
public delegate void ELOAM_OCRCALLBACK(LONG flag, LONG value, LPVOID userData); |
|
public class HScamera |
|
{ |
|
HELOAMVIEW m_hView; |
|
HELOAMTHUMBNAIL m_hThumb; |
|
|
|
//设别列表(专指视频) |
|
List<HELOAMDEVICE> m_vDevice; |
|
HELOAMVIDEO m_hVideo; |
|
HELOAMVIDEOCAP m_cap; |
|
//合成时的临时图片 |
|
HELOAMIMAGE m_hImageTemp; |
|
|
|
//定义系统计时器 |
|
private System.Timers.Timer timer; |
|
private int timer_value; |
|
|
|
ELOAM_DEVCHANGECALLBACK DevChangeCallBack; |
|
ELOAM_ATTACHCALLBAK attachCallback; |
|
private readonly int devIdx; |
|
private readonly int resIdx; |
|
private readonly int modeIdx; |
|
private readonly int turnRight = Convert.ToInt32(ConfigurationManager.AppSettings["xzds"].ToString()); |
|
public HScamera(int selectDevice, int selectResolution, int selectMode) |
|
{ |
|
m_hView = IntPtr.Zero; |
|
m_hThumb = IntPtr.Zero; |
|
m_hView = IntPtr.Zero; |
|
m_hImageTemp = IntPtr.Zero; |
|
m_vDevice = new List<HELOAMDEVICE>(); |
|
this.devIdx = selectDevice; |
|
this.resIdx = selectResolution; |
|
this.modeIdx = selectMode; |
|
} |
|
|
|
/// <summary> |
|
/// 打开高拍仪 |
|
/// </summary> |
|
/// <returns></returns> |
|
public string openCamera2() |
|
{ |
|
try |
|
{ |
|
string filename = string.Empty; |
|
if (m_vDevice.Count == 0) |
|
{ |
|
Console.WriteLine("初始化扫描仪"); |
|
DevChangeCallBack = new ELOAM_DEVCHANGECALLBACK(DEVCHANGECALLBACK); |
|
EloamDll.EloamGlobal_InitDevs(DevChangeCallBack, IntPtr.Zero); |
|
EloamDll.EloamGlobal_VideoCapInit(); |
|
//EloamDll.EloamGlobal_InitFaceDetect(); |
|
} |
|
//打开高拍仪 |
|
EloamDll.EloamVideo_Release(m_hVideo); |
|
HELOAMDEVICE hDev = m_vDevice[devIdx]; |
|
m_hVideo = EloamDll.EloamDevice_CreateVideo( |
|
hDev, resIdx, modeIdx, null, (IntPtr)0, null, (IntPtr)0, 0, 2); |
|
|
|
//开始拍照 |
|
HELOAMIMAGE hImg = EloamDll.EloamVideo_CreateImage(m_hVideo, 0, m_hView); |
|
if (IntPtr.Zero != hImg) |
|
{ |
|
DateTime dateTime = DateTime.Now; |
|
string time = DateTime.Now.ToString( |
|
"yyyyMMddHHmmss", DateTimeFormatInfo.InvariantInfo); |
|
var dirpath = Path.Combine(Environment.CurrentDirectory, "wwwroot", "Camera"); |
|
FileExt.MakeSureDirExist(dirpath); |
|
var filepath = Path.Combine(dirpath, time); |
|
filename = dirpath + "\\" + time + ".jpg"; |
|
//新增 |
|
LONG width = EloamDll.EloamImage_GetWidth(hImg); |
|
LONG height = EloamDll.EloamImage_GetHeight(hImg); |
|
HELOAMIMAGE hSaveImg = EloamDll.EloamGlobal_CreateImage(width, height, EloamDll.EloamImage_GetChannels(hImg)); |
|
if (null == hSaveImg) |
|
{ //释放 |
|
EloamDll.EloamImage_Release(hSaveImg); |
|
EloamDll.EloamImage_Release(hImg); |
|
openCamera2(); |
|
} |
|
else |
|
{ |
|
EloamDll.EloamImage_Copy(hSaveImg, hImg); |
|
//保存图片 |
|
if (1 == EloamDll.EloamImage_Save(hSaveImg, filename, 0)) |
|
{ |
|
EloamDll.EloamView_PlayCaptureEffect(m_hView); |
|
} |
|
//释放 |
|
EloamDll.EloamImage_Release(hSaveImg); |
|
EloamDll.EloamImage_Release(hImg); |
|
//为获取图片重新破获 |
|
if (!File.Exists(filename)) |
|
{ |
|
openCamera2(); |
|
} |
|
} |
|
} |
|
return filename; |
|
} |
|
catch (Exception ex) |
|
{ |
|
////Log.Info("扫描图片旋转异常1:" + ex.Message); |
|
return ""; |
|
} |
|
} |
|
|
|
//加载设备 |
|
public void DEVCHANGECALLBACK(LONG type, LONG idx, LONG dbt, LPVOID userData) |
|
{ |
|
HELOAMDEVICE hDev = EloamDll.EloamGlobal_CreateDevice(1, idx); |
|
m_vDevice.Add(hDev); |
|
} |
|
|
|
/// <summary> |
|
/// 文件转Base64码 |
|
/// </summary> |
|
/// <param name="fileLocation"></param> |
|
/// <returns></returns> |
|
public string ImgToBase64String(string fileLocation) |
|
{ |
|
try |
|
{ |
|
MemoryStream ms = new MemoryStream(); |
|
if (System.IO.File.Exists(fileLocation)) |
|
{ |
|
Bitmap bmp = new Bitmap(fileLocation); |
|
BitmapFlip(turnRight, ref bmp); |
|
bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); |
|
byte[] arr = new byte[ms.Length]; |
|
ms.Position = 0; |
|
ms.Read(arr, 0, (int)ms.Length); |
|
string base64 = Convert.ToBase64String(arr); |
|
DateTime dateTime = DateTime.Now; |
|
string time = DateTime.Now.ToString( |
|
"yyyyMMddHHmmss", DateTimeFormatInfo.InvariantInfo); |
|
var dirpath = Path.Combine(Environment.CurrentDirectory, "wwwroot", "Camera"); |
|
FileExt.MakeSureDirExist(dirpath); |
|
var filepath = Path.Combine(dirpath, time); |
|
string filename = dirpath + "\\" + time + ".jpg"; |
|
ConvertFromBase64ToImage(base64, filename); |
|
//ms.Close(); |
|
//File.Delete(fileLocation); |
|
return filename; |
|
} |
|
return ""; |
|
} |
|
catch (Exception ex) |
|
{ |
|
////Log.Info("扫描图片旋转异常:" + ex.Message); |
|
return ex.Message; |
|
} |
|
} |
|
|
|
/// <summary> |
|
/// 图像翻转,基于图像中心 |
|
/// </summary> |
|
public void BitmapFlip(int iFlip, ref Bitmap btDes) |
|
{ |
|
switch (iFlip) |
|
{ |
|
case 0: |
|
break; |
|
case 90: |
|
btDes.RotateFlip(RotateFlipType.Rotate90FlipNone); |
|
break; |
|
case 180: |
|
btDes.RotateFlip(RotateFlipType.Rotate180FlipNone); |
|
break; |
|
case 270: |
|
btDes.RotateFlip(RotateFlipType.Rotate270FlipNone); |
|
break; |
|
} |
|
} |
|
|
|
/// <summary> |
|
/// base64转文件 |
|
/// </summary> |
|
/// <param name="base64String"></param> |
|
/// <param name="filePath"></param> |
|
public void ConvertFromBase64ToImage(string base64String, string filePath) |
|
{ |
|
// 将Base64字符串转换为字节数组 |
|
byte[] imageBytes = Convert.FromBase64String(base64String); |
|
|
|
// 使用MemoryStream将字节数组转换为图片 |
|
using (var ms = new MemoryStream(imageBytes, 0, imageBytes.Length)) |
|
{ |
|
// 使用Bitmap从MemoryStream创建图片 |
|
using (Bitmap image = new Bitmap(ms)) |
|
{ |
|
// 保存图片到文件 |
|
image.Save(filePath, System.Drawing.Imaging.ImageFormat.Png); |
|
} |
|
} |
|
} |
|
} |
|
}
|
|
|