Commit 50093b71 LN

扫码修改

1 个父辈 60879914
此文件类型无法预览
using log4net; using CodeLibrary;
using log4net;
using OnlineStore.Common; using OnlineStore.Common;
using OnlineStore.DeviceLibrary; using OnlineStore.DeviceLibrary;
using OnlineStore.LoadCSVLibrary; using OnlineStore.LoadCSVLibrary;
...@@ -199,8 +200,11 @@ namespace OnlineStore.ACPackingStore ...@@ -199,8 +200,11 @@ namespace OnlineStore.ACPackingStore
IOManager.instance.CloseAllDO(); IOManager.instance.CloseAllDO();
IOManager.instance.CloseAllConnection(); IOManager.instance.CloseAllConnection();
HumitureController.CloseAllPort(); HumitureController.CloseAllPort();
ACServerManager.CloseAllPort(); ACServerManager.CloseAllPort();
if (Camera._cam != null)
{
Camera._cam.CloseAll();
}
//this.Close(); //this.Close();
System.Environment.Exit(System.Environment.ExitCode); System.Environment.Exit(System.Environment.ExitCode);
} }
...@@ -457,6 +461,10 @@ namespace OnlineStore.ACPackingStore ...@@ -457,6 +461,10 @@ namespace OnlineStore.ACPackingStore
private void 二维码学习ToolStripMenuItem_Click(object sender, EventArgs e) private void 二维码学习ToolStripMenuItem_Click(object sender, EventArgs e)
{ {
if (Camera._cam != null)
{
Camera._cam.CloseAll();
}
CodeLibrary.FrmCodeDecode frm = new CodeLibrary.FrmCodeDecode(); CodeLibrary.FrmCodeDecode frm = new CodeLibrary.FrmCodeDecode();
frm.ShowDialog(); frm.ShowDialog();
} }
......
...@@ -93,9 +93,11 @@ shelfLoc问题修改。 ...@@ -93,9 +93,11 @@ shelfLoc问题修改。
扫码问题优化。 扫码问题优化。
出库未结束时,去掉:检测到料架自动入库功能。 出库未结束时,去掉:检测到料架自动入库功能。
20200310
出库mark参数修改
20200311
扫码修改
......
...@@ -113,9 +113,7 @@ namespace OnlineStore.Common ...@@ -113,9 +113,7 @@ namespace OnlineStore.Common
} }
private static int isLog = ConfigAppSettings.GetIntValue(Setting_Init.Server_Log_Open); private static int isLog = ConfigAppSettings.GetIntValue(Setting_Init.Server_Log_Open);
public static string Post(string url, string paramData, Encoding encoding) public static string Post(string url, string paramData, Encoding encoding)
{ {
//如果Op=0或者Op=5,先拼Ip,不通不发送
if (PingURLIP(url, 100).Equals(false)) if (PingURLIP(url, 100).Equals(false))
{ {
return null; return null;
......
using CodeLibrary;
using OnlineStore.Common;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.ExceptionServices;
using System.Text;
using System.Windows.Forms;
namespace OnlineStore.DeviceLibrary
{
public class CodeManager
{
public static List<string> codeTypeList = new List<string>();
public static List<string> balserNameList = new List<string>();
public static List<string> hikNameList = new List<string>();
private static char spiltChar = '#';
/// <summary>
/// 初始化摄像机名称和二维码类型
/// </summary>
public static void LoadConfig()
{
string codeStr = ConfigAppSettings.GetValue(Setting_Init.CodeType);
codeTypeList = new List<string>();
HDLogUtil.LogName = "RollingLogFileAppender";
try
{
string[] codeArray = codeStr.Split(spiltChar);
foreach (string str in codeArray)
{
if (str.Trim().Equals(""))
{
continue;
}
LogUtil.info("加载到配置二维码类型:" + str.Trim());
codeTypeList.Add(str.Trim());
}
LoadCamera(false);
CodeLibrary.HDCodeLearnHelper.LoadConfig("", codeStr);
}
catch (Exception ex)
{
LogUtil.error("解析摄像机配置出错:" + ex.StackTrace);
}
}
private static void LoadCamera(bool isReLoad)
{
if (isReLoad)
{
try
{
CodeLibrary.HIKCamera.Instance.Load();
}
catch (Exception ex)
{
LogUtil.error("加载HIK相机出错:" + ex.ToString());
}
try
{
CodeLibrary.BaslerCamera.Instance.Load();
}
catch (Exception ex)
{
LogUtil.error("加载Basler相机出错:" + ex.ToString());
}
}
string[] names = CodeLibrary.HIKCamera.Instance.CameraName;
if (names != null)
{
foreach (string n in names)
{
if (!hikNameList.Contains(n))
{
hikNameList.Add(n);
}
}
// hikNameList.AddRange(names);
foreach (string name in hikNameList)
{
LogUtil.info("加载到HIK相机:" + name);
}
}
names = CodeLibrary.BaslerCamera.Instance.CameraName;
if (names != null)
{
foreach (string n in names)
{
if (!balserNameList.Contains(n))
{
balserNameList.Add(n);
}
}
// balserNameList.AddRange(names);
foreach (string name in balserNameList)
{
LogUtil.info("加载到Balser相机:" + name);
}
}
}
public static void CloseCamera()
{
BaslerCamera.Instance.Close();
HIKCamera.Instance.Close();
}
public static Bitmap GetCamerImage(string cameraName)
{
Bitmap bitm = null;
try
{
if (balserNameList.Contains(cameraName))
{
bool result = BaslerCamera.Instance.Open(cameraName);
LogUtil.info("相机【" + cameraName + "】打开:" + result + "");
if (result)
{
BaslerCamera.Instance.GrabOne();
bitm = BaslerCamera.Instance.Image;
BaslerCamera.Instance.Close();
}
else
{
LogUtil.error("相机【" + cameraName + "】打开失败:" + BaslerCamera.Instance.ErrInfo);
}
}
else if (hikNameList.Contains(cameraName))
{
bool result = HIKCamera.Instance.Open(cameraName);
// LogUtil.info("打开相机:" + cameraName + "(" + result + ")");
if (result)
{
HIKCamera.Instance.GrabOne();
bitm = HIKCamera.Instance.Image;
HIKCamera.Instance.Close();
}
else
{
LogUtil.error("相机【" + cameraName + "】打开失败:" + HIKCamera.Instance.ErrInfo);
}
}
else
{
LogUtil.info("未找到相机【" + cameraName + "】无法获取图片");
//若未加载到相机,需要重新加载相机
if (balserNameList.Count <= 0 && hikNameList.Count <= 0)
{
LogUtil.error("获取图片时发现未加载到相机,重新加载相机");
LoadCamera(true);
}
}
}
catch (Exception ex)
{
LogUtil.error("从相机【" + cameraName + "】获取图片出错:" + ex.ToString());
}
return bitm;
}
private static int codeCount = ConfigAppSettings.GetIntValue(Setting_Init.CodeCount);
[HandleProcessCorruptedStateExceptions]
public static List<string> CameraScan(List<string> cameraList, string deviceName)
{
if (codeCount < 1)
{
codeCount = 1;
}
List<string> codeList = new List<string>();
//List<CodeInfo> allCodeList = new List<CodeInfo>();
if (cameraList == null || cameraList.Count <= 0)
{
return codeList;
}
try
{
foreach (string cameraName in cameraList)
{
if (cameraName.Trim().Equals(""))
{
continue;
}
LogUtil.info(deviceName + " 【" + cameraName + "】开始打开相机获取图片");
using (Bitmap bitmap = GetCamerImage(cameraName))
{
if (bitmap == null)
{
LogUtil.error(deviceName + " 【" + cameraName + "】获取图片失败,关闭相机");
CloseCamera();
continue;
}
LogUtil.info(deviceName + " 【" + cameraName + "】获取图片完成,开始转换图片,并扫码");
System.Threading.Thread.Sleep(1);
//转换托盘大概100-150ms,不打印日志
HalconDotNet.HObject ho_Image = HDCodeHelper.Bitmap2HObjectBpp24(bitmap);
// LogUtil.info(" 相机【" + cameraName + "】转换图片完成,开始扫码");
List<CodeInfo> cc = new List<CodeInfo>();
string r = "";
foreach (string codeType in codeTypeList)
{
//判断是否是一维码
if (codeType.ToLower().Equals("barcode"))
{
cc = HDCodeHelper.DecodeBarCode(ho_Image);
}
else
{
cc = HDCodeHelper.DecodeCode(ho_Image, codeCount, GetCodeParamFilePath(codeType), codeType);
}
foreach (CodeInfo c in cc)
{
string str = CodeManager.ReplaceCode(c.CodeStr);
codeList.Add(str);
r = r + "##" + str;
}
// LogUtil.debug(" 相机【" + cameraName + "】【" + codeType + "】扫码完成:" + r);
}
LogUtil.info(deviceName + " 【" + cameraName + "】扫码完成:" + r);
ho_Image.Dispose();
bitmap.Dispose();
}
}
}
catch (AccessViolationException e)
{
LogUtil.error(deviceName + " 扫码出现AccessViolationException异常:" + e.ToString());
GC.Collect();
}
catch (Exception ex)
{
LogUtil.error(deviceName + " 扫码出错:"+ex.ToString());
}
return codeList;
}
public static string GetCodeParamFilePath(string codePath)
{
string appPath = Application.StartupPath;
string path = appPath + ConfigAppSettings.GetValue(Setting_Init.CodeParamPath);
string filePath = path + codePath + ".dcm";
if (File.Exists(filePath))
{
return filePath;
}
else
{
return "";
}
}
/// <summary>
/// 处理接收后的二维码
/// </summary>
/// <param name="message"></param>
/// <returns></returns>
public static string ReplaceCode(string message)
{
message = message.Trim();
message = message.Replace("\r", "");
message = message.Replace("\n", "");
char a = (char)02;
message = message.Replace(a.ToString(), "");
message = message.Trim();
System.Text.ASCIIEncoding asciiEncoding = new System.Text.ASCIIEncoding();
byte[] bytes = asciiEncoding.GetBytes(message);
List<byte> newBytes = new List<byte>();
foreach (byte by in bytes)
{
int value = (int)by;
if (value.Equals(24) || value.Equals(30) || value.Equals(29) || value.Equals(4))
{
continue;
}
if (!value.Equals(24))
{
newBytes.Add(by);
}
}
message = asciiEncoding.GetString(newBytes.ToArray());
return message;
}
public static string ProcessCode(List<string> codeList)
{
string code = "";
foreach (string cc in codeList)
{
if (string.IsNullOrEmpty(cc))
{
continue;
}
code += cc + "##";
}
return ReplaceCode(code);
}
}
}
...@@ -3,6 +3,7 @@ using OnlineStore.Common; ...@@ -3,6 +3,7 @@ using OnlineStore.Common;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Drawing; using System.Drawing;
using System.Drawing.Imaging;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Runtime.ExceptionServices; using System.Runtime.ExceptionServices;
...@@ -11,8 +12,6 @@ using System.Windows.Forms; ...@@ -11,8 +12,6 @@ using System.Windows.Forms;
namespace OnlineStore.DeviceLibrary namespace OnlineStore.DeviceLibrary
{ {
public class CodeManager public class CodeManager
{ {
public static List<string> codeTypeList = new List<string>(); public static List<string> codeTypeList = new List<string>();
...@@ -37,7 +36,8 @@ namespace OnlineStore.DeviceLibrary ...@@ -37,7 +36,8 @@ namespace OnlineStore.DeviceLibrary
{ {
continue; continue;
} }
LogUtil.info("加载到配置二维码类型:" + str.Trim()); string file = GetCodeParamFilePath(str.Trim());
LogUtil.info("加载到配置二维码类型:" + str.Trim()+",配置文件:"+file);
codeTypeList.Add(str.Trim()); codeTypeList.Add(str.Trim());
} }
...@@ -46,31 +46,28 @@ namespace OnlineStore.DeviceLibrary ...@@ -46,31 +46,28 @@ namespace OnlineStore.DeviceLibrary
} }
catch (Exception ex) catch (Exception ex)
{ {
LogUtil.error("解析摄像机配置出错:" + ex.StackTrace); LogUtil.error("解析摄像机配置出错:"+ex.ToString());
} }
} }
private static void LoadCamera(bool isReLoad) private static void LoadCamera(bool isReLoad)
{ {
if (isReLoad) if (isReLoad || Camera._cam == null)
{ {
try try
{ {
CodeLibrary.HIKCamera.Instance.Load(); if (Camera._cam != null)
} {
catch (Exception ex) Camera._cam.CloseAll();
{ }
LogUtil.error("加载HIK相机出错:" + ex.ToString()); Camera.Type = CameraType.HIK;
} Camera._cam.Load();
try
{
CodeLibrary.BaslerCamera.Instance.Load();
} }
catch (Exception ex) catch (Exception ex)
{ {
LogUtil.error("加载Basler相机出错:" + ex.ToString()); LogUtil.error("加载HIK相机出错:"+ex.ToString());
} }
} }
string[] names = CodeLibrary.HIKCamera.Instance.CameraName; string[] names = Camera._cam.Name;
if (names != null) if (names != null)
...@@ -88,27 +85,17 @@ namespace OnlineStore.DeviceLibrary ...@@ -88,27 +85,17 @@ namespace OnlineStore.DeviceLibrary
LogUtil.info("加载到HIK相机:" + name); LogUtil.info("加载到HIK相机:" + name);
} }
} }
names = CodeLibrary.BaslerCamera.Instance.CameraName;
if (names != null)
{
foreach (string n in names)
{
if (!balserNameList.Contains(n))
{
balserNameList.Add(n);
}
}
// balserNameList.AddRange(names);
foreach (string name in balserNameList)
{
LogUtil.info("加载到Balser相机:" + name);
}
}
} }
public static void CloseCamera()
public static void CloseCamera(string cameraName)
{
Camera._cam.Close(cameraName);
}
public static void CloseAllCamera()
{ {
BaslerCamera.Instance.Close(); //BaslerCamera.Instance.Close();
HIKCamera.Instance.Close(); //HIKCamera.Instance.Close();
Camera._cam.CloseAll();
} }
public static Bitmap GetCamerImage(string cameraName) public static Bitmap GetCamerImage(string cameraName)
...@@ -116,63 +103,23 @@ namespace OnlineStore.DeviceLibrary ...@@ -116,63 +103,23 @@ namespace OnlineStore.DeviceLibrary
Bitmap bitm = null; Bitmap bitm = null;
try try
{ {
if (balserNameList.Contains(cameraName)) bitm = Camera._cam.GrabOneImage(cameraName);
{
bool result = BaslerCamera.Instance.Open(cameraName);
LogUtil.info("相机【" + cameraName + "】打开:" + result + "");
if (result)
{
BaslerCamera.Instance.GrabOne();
bitm = BaslerCamera.Instance.Image;
BaslerCamera.Instance.Close();
}
else
{
LogUtil.error("相机【" + cameraName + "】打开失败:" + BaslerCamera.Instance.ErrInfo);
}
}
else if (hikNameList.Contains(cameraName))
{
bool result = HIKCamera.Instance.Open(cameraName);
// LogUtil.info("打开相机:" + cameraName + "(" + result + ")");
if (result)
{
HIKCamera.Instance.GrabOne();
bitm = HIKCamera.Instance.Image;
HIKCamera.Instance.Close();
}
else
{
LogUtil.error("相机【" + cameraName + "】打开失败:" + HIKCamera.Instance.ErrInfo);
}
}
else
{
LogUtil.info("未找到相机【" + cameraName + "】无法获取图片");
//若未加载到相机,需要重新加载相机
if (balserNameList.Count <= 0 && hikNameList.Count <= 0)
{
LogUtil.error("获取图片时发现未加载到相机,重新加载相机");
LoadCamera(true);
}
}
} }
catch (Exception ex) catch (Exception ex)
{ {
LogUtil.error("从相机【" + cameraName + "】获取图片出错:" + ex.ToString()); LogUtil.error(" 【" + cameraName + "】获取图片出错:"+ex.ToString());
} }
return bitm; return bitm;
} }
private static int codeCount = ConfigAppSettings.GetIntValue(Setting_Init.CodeCount); private static int codeCount = ConfigAppSettings.GetIntValue(Setting_Init.CodeCount);
[HandleProcessCorruptedStateExceptions] [HandleProcessCorruptedStateExceptions]
public static List<string> CameraScan(List<string> cameraList, string deviceName) public static List<string> CameraScan(List<string> cameraList, string deviceName, bool isSaveImg = false)
{ {
if (codeCount < 1) if (codeCount < 1)
{ {
codeCount = 1; codeCount = 1;
} }
List<string> codeList = new List<string>(); List<string> codeList = new List<string>();
//List<CodeInfo> allCodeList = new List<CodeInfo>();
if (cameraList == null || cameraList.Count <= 0) if (cameraList == null || cameraList.Count <= 0)
{ {
return codeList; return codeList;
...@@ -190,15 +137,16 @@ namespace OnlineStore.DeviceLibrary ...@@ -190,15 +137,16 @@ namespace OnlineStore.DeviceLibrary
{ {
if (bitmap == null) if (bitmap == null)
{ {
LogUtil.error(deviceName + " 【" + cameraName + "】获取图片失败,关闭相机"); LogUtil.error(deviceName + " 【" + cameraName + "】获取图片失败[" + Camera._cam.ErrInfo + "],关闭相机");
CloseCamera(); CloseCamera(cameraName);
continue; continue;
} }
LogUtil.info(deviceName + " 【" + cameraName + "】获取图片完成,开始转换图片,并扫码"); LogUtil.info(deviceName + " 【" + cameraName + "】获取图片完成,开始转换图片,并扫码");
System.Threading.Thread.Sleep(1); System.Threading.Thread.Sleep(1);
//转换托盘大概100-150ms,不打印日志 //转换托盘大概100-150ms,不打印日志
HalconDotNet.HObject ho_Image = HDCodeHelper.Bitmap2HObjectBpp24(bitmap); Bitmap bit = new Bitmap(bitmap);
HalconDotNet.HObject ho_Image = HDCodeHelper.Bitmap2HObjectBpp24(bit);
// LogUtil.info(" 相机【" + cameraName + "】转换图片完成,开始扫码"); // LogUtil.info(" 相机【" + cameraName + "】转换图片完成,开始扫码");
List<CodeInfo> cc = new List<CodeInfo>(); List<CodeInfo> cc = new List<CodeInfo>();
string r = ""; string r = "";
...@@ -216,13 +164,20 @@ namespace OnlineStore.DeviceLibrary ...@@ -216,13 +164,20 @@ namespace OnlineStore.DeviceLibrary
foreach (CodeInfo c in cc) foreach (CodeInfo c in cc)
{ {
string str = CodeManager.ReplaceCode(c.CodeStr); string str = CodeManager.ReplaceCode(c.CodeStr);
codeList.Add(str); if (!codeList.Contains(str))
r = r + "##" + str; {
codeList.Add(str);
r = r + "##" + str;
}
} }
// LogUtil.debug(" 相机【" + cameraName + "】【" + codeType + "】扫码完成:" + r); // LogUtil.debug(" 相机【" + cameraName + "】【" + codeType + "】扫码完成:" + r);
} }
if (String.IsNullOrEmpty(r))
{
SaveImageToFile(deviceName, cameraName, bit);
}
LogUtil.info(deviceName + " 【" + cameraName + "】扫码完成:" + r); LogUtil.info(deviceName + " 【" + cameraName + "】扫码完成:" + r);
bit.Dispose();
ho_Image.Dispose(); ho_Image.Dispose();
bitmap.Dispose(); bitmap.Dispose();
} }
...@@ -240,6 +195,26 @@ namespace OnlineStore.DeviceLibrary ...@@ -240,6 +195,26 @@ namespace OnlineStore.DeviceLibrary
return codeList; return codeList;
} }
private static void SaveImageToFile(string deviceName, string cameraName, Bitmap bitmap)
{
string date = DateTime.Now.ToString("HH-mm-ss-") + DateTime.Now.Millisecond;
string dire = @"D:\image\" + deviceName.Trim().Replace('_', '-') + @"\" + cameraName.Trim().Replace('_', '-').Replace(':', '-') + @"\";
string iamgeName = date + ".bmp";
try
{
Bitmap bit = (Bitmap)bitmap.Clone();
if (Directory.Exists(dire).Equals(false))
{
Directory.CreateDirectory(dire);
}
bit.Save(dire + iamgeName, ImageFormat.Bmp);
LogUtil.info(deviceName + " 【" + cameraName + "】扫码失败,保存图片到【" + dire + iamgeName + "】成功");
}
catch (Exception ex)
{
LogUtil.error("保存" + deviceName + " 【" + cameraName + "】的图片到【" + dire + iamgeName + "】出错"+ex.ToString());
}
}
public static string GetCodeParamFilePath(string codePath) public static string GetCodeParamFilePath(string codePath)
{ {
......
支持 Markdown 格式
你添加了 0 到此讨论。请谨慎行事。
Finish editing this message first!