using System;
using System.IO;
using FileControl;
using System.Text;
using System.Xml;
namespace xxxxx
{
/// <summary>
/// VehiclePass 的摘要说明。
/// </summary>
public class Vehicle
{
#region 定义常数
/// <summary>
/// 常数 保存图像序列号最大值[此值与最终子目录数量相关]
/// </summary>
private const int IMAGE_MAX_SERIAL = 3000000;
/// <summary>
/// 常数 每个目录保存的最大图像组数(每组1-4幅图像)[此值与最终子目录数量相关]
/// </summary>
private const int IMAGE_MAX_GROUP_DIRECTORY = 2000;
/// <summary>
/// 常数 原始图像的宽度
/// </summary>
private const int VEHICLE_IMAGE_ORIGINAL_WIDTH = 1388;
/// <summary>
/// 常数 原始图像的高度
/// </summary>
private const int VEHICLE_IMAGE_ORIGINAL_HIGH = 1238;
/// <summary>
/// 常数 车辆通行前往车牌过滤比较的时限
/// </summary>
private const uint PASS_TICKCOUNT_OVERFLOW = 10000;
#endregion
#region 定义私有数据类型
private string sourcePath; // 源扫描路径
private string dstPath; // 目标存储路径(destination path)
private string imageFullPath; // 车辆全景图片路径(转存后)
private string imageSpecialPath; // 识别车牌图片路径(转存后)
private string xmlFilePath; // xml文件路径(转存后)
private int index; // 序号
private string passTime; // 车辆通过时间
private string location; // 地点
private int lane; // 车道
private string type; // 车型 car 小车;lorry 卡车
private string direction; // 方向 approaching 来向;receding 去向
private string license; // 车牌号码
private string licenseColor; // 车牌颜色
private string licenseColorCode; // 车牌颜色代码 [0 白,1 黄,2 蓝,3 黑,4 其它,5 绿色]
private string licenseType; // 车牌类型代码 详见本段“附加注解a ”
private int state; // 车辆状态代码 详见本段“附加注解a ”
private int length; // 车辆长度 单位:米
private int speedLimitCar; // 小车限速 单位:公里/小时
private int speedLimitLorry; // 卡车限速 单位:公里/小时
private int speedLimit; // 本车限速 单位:公里/小时
private int speed; // 测速值 单位:公里/小时
private int speedLimitTrigger; // 触发速度 单位:公里/小时
private byte[] pchLicenseBuffer; // 车牌号码[byte字符串]
private byte[] pchRecogImagePath; // 用于识别的图像路径[byte字符串]
private uint recognizeTime; // 车牌识别时间
private uint dealWholeTime; // 处理一条记录的全部时间
/// <summary>
/// 附加注解a
/// licenseType 车牌类型代码包括:
/// [00 白,01 黄,02 蓝,03 04 05 06为黑"使"/"领"/"境"/"其它",07 绿,99 未知]
/// state 车辆状态代码
/// [0 正常,1303 一般超速(不超过50%),1603 严重超速(超过50%)]
/// </summary>
#endregion
#region 定义属性
/// <summary>
/// 源扫描路径
/// </summary>
public string SourcePath
{
get
{
return sourcePath;
}
set
{
sourcePath = value;
}
}
/// <summary>
/// 目标存储路径(destination path)
/// </summary>
public string DestinationPath
{
get
{
return dstPath;
}
set
{
dstPath = value;
}
}
/// <summary>
/// 只读 车辆全景图片路径(转存后)
/// </summary>
public string ImageFullPath
{
get
{
return imageFullPath;
}
}
/// <summary>
/// 只读 识别车牌图片路径(转存后)
/// </summary>
///
public string ImageSpecialPath
{
get
{
return imageSpecialPath;
}
}
/// <summary>
/// 只读 xml文件路径(转存后)
/// </summary>
public string XmlFilePath
{
get
{
return xmlFilePath;
}
}
/// <summary>
/// 只读 序号
/// </summary>
public int Index
{
get
{
return index;
}
}
/// <summary>
/// 只读 车辆通过时间
/// </summary>
public string PassTime
{
get
{
return passTime;
}
}
/// <summary>
/// 只读 地点
/// </summary>
public string Location
{
get
{
return location;
}
}
/// <summary>
/// 只读 车道
/// </summary>
public int Lane
{
get
{
return lane;
}
}
/// <summary>
/// 只读 车型:car 小车;lorry 卡车
/// </summary>
public string Type
{
get
{
return type;
}
}
/// <summary>
/// 只读 方向:approaching 来向;receding 去向
/// </summary>
public string Direction
{
get
{
return direction;
}
}
/// <summary>
/// 只读 车牌号码
/// </summary>
public string License
{
get
{
return license;
}
}
/// <summary>
/// 只读 车牌颜色
/// </summary>
public string LicenseColor
{
get
{
return licenseColor;
}
}
/// <summary>
/// 只读 车牌颜色代码 [0 白,1 黄,2 蓝,3 黑,4 其它,5 绿色]
/// </summary>
public string LicenseColorCode
{
get
{
return licenseColorCode;
}
}
/// <summary>
/// 只读 车牌类型代码 [00 白,01 黄,02 蓝,03 04 05 06为黑"使"/"领"/"境"/"其它",07 绿,99 未知]
/// </summary>
public string LicenseType
{
get
{
return licenseType;
}
}
/// <summary>
/// 只读 车辆状态代码 [0 正常,1303 一般超速(不超过50%),1603 严重超速(超过50%)]
/// </summary>
public int State
{
get
{
return state;
}
}
/// <summary>
/// 只读 车辆长度
/// </summary>
public int Length
{
get
{
return length;
}
}
/// <summary>
/// 只读 小车限速
/// </summary>
public int SpeedLimitCar
{
get
{
return speedLimitCar;
}
}
/// <summary>
/// 只读 卡车限速
/// </summary>
public int SpeedLimitLorry
{
get
{
return speedLimitLorry;
}
}
/// <summary>
/// 只读 本车限速
/// </summary>
public int SpeedLimit
{
get
{
return speedLimit;
}
}
/// <summary>
/// 只读 测速值
/// </summary>
public int Speed
{
get
{
return speed;
}
}
/// <summary>
/// 只读 触发速度
/// </summary>
public int SpeedLimitTrigger
{
get
{
return speedLimitTrigger;
}
}
/// <summary>
/// 只读 识别花费时间
/// </summary>
public uint RecognizeTime
{
get
{
return recognizeTime;
}
}
/// <summary>
/// 只读 识别花费时间
/// </summary>
public uint DealWholeTime
{
get
{
return dealWholeTime;
}
}
#endregion
/// <summary>
/// 构造函数
/// </summary>
public Vehicle()
{
sourcePath = @"./_VehicleIn";
dstPath = @"./_PoliScanImage";
imageFullPath = "Init";
imageSpecialPath = "Init";
xmlFilePath = "Init";
index = -1;
passTime = "2000-01-01 01:00:00";
location = "Init";
lane = -1;
type = "Init";
direction = "Init";
license = "Init";
licenseColor = "Init";
licenseColorCode = "Init";
licenseType = "Init";
length = -1;
speedLimitCar = -1;
speedLimitLorry = -1;
speedLimit = -1;
speed = -1;
speedLimitTrigger = -1;
pchLicenseBuffer = new byte[32];
pchRecogImagePath = new byte[256];
}
/// <summary>
/// 关键数据重置至初始状态
/// </summary>
private void Reset()
{
pchLicenseBuffer.Initialize();
license = "Init";
licenseColor = "Init";
licenseColorCode = "Init";
licenseType = "Init";
state = 0;
recognizeTime = 0;
dealWholeTime = 0;
}
/// <summary>
/// 读取指定XML文件中的车辆信息
/// </summary>
/// <param name="xmlFileName">指定读取的XML文件名</param>
/// <returns>成功返回true;失败返回false</returns>
private bool ReadXMLInfo(string xmlFileName)
{
bool retval = false;
XmlTextReader reader = new XmlTextReader(xmlFileName);
try
{
reader.ReadToFollowing("systemName");
reader.ReadToFollowing("index");
index = int.Parse(reader.ReadString());
reader.ReadToFollowing("dateTime");
passTime = reader.ReadString();
//passTime = passTime.ToString("[yyyy-MM-dd HH:mm:ss]");
DateTime dt = DateTime.Parse(passTime);
passTime = dt.ToString("yyyy-MM-dd HH:mm:ss");
reader.ReadToFollowing("field");
location = reader.ReadString();
reader.ReadEndElement();
reader.ReadStartElement("field");
location = location + " " + reader.ReadString();
reader.ReadEndElement();
reader.ReadStartElement("field");
location = location + " " + reader.ReadString();
reader.ReadEndElement();
reader.ReadToFollowing("lane");
lane = int.Parse(reader.ReadString());
reader.ReadToFollowing("type");
type = reader.ReadString().ToLower();
if (type.Equals("car"))
{
type = "小型车";
}
else
{
type = "卡车";
}
reader.ReadToFollowing("direction");
direction = reader.ReadString().ToLower();
if (direction.Equals("approaching"))
{
direction = "来向";
}
else
{
direction = "去向";
}
reader.ReadToFollowing("value");
speedLimitCar = int.Parse(reader.ReadString());
reader.ReadEndElement();
reader.ReadToFollowing("value");
speedLimitLorry = int.Parse(reader.ReadString());
reader.ReadEndElement();
reader.ReadToFollowing("value");
speed = int.Parse(reader.ReadString());
reader.ReadEndElement();
reader.ReadToFollowing("value");
speedLimitTrigger = int.Parse(reader.ReadString());
reader.ReadEndElement();
if (type.Equals("小型车"))
{
speedLimit = speedLimitCar;
}
else
{
speedLimit = speedLimitLorry;
}
retval = true;
}
catch (XmlException e)
{
FileControl.Log.WriteToFile("Vehicle.ReadXMLInfo", "exception: " + e.ToString());
}
catch (Exception e)
{
FileControl.Log.WriteToFile("Vehicle.ReadXMLInfo", "exception: " + e.ToString());
}
finally
{
reader.Close();
}
//根据车辆的不同类型计算车辆超速状态
if (type.Equals("小型车"))
{
if ((speed > speedLimitCar) && (speed <= speedLimitCar * 1.5))
{
state = 1303;
}
else if (speed > speedLimitCar * 1.5)
{
state = 1603;
}
else
{
state = 0;
}
}
else
{
if ((speed > speedLimitLorry) && (speed <= speedLimitLorry * 1.5))
{
state = 1303;
}
else if (speed > speedLimitLorry * 1.5)
{
state = 1603;
}
else
{
state = 0;
}
}
return retval;
}
/// <summary>
/// 解析车牌照相关详细信息 [根据pchLicenseBuffer]
/// </summary>
private void GetLicenseInfo()
{
string buffer;
buffer = Encoding.Default.GetString(pchLicenseBuffer);
if (buffer.IndexOf("无车牌") != -1) //判断识别结果是不是“无车牌”
{
license = "无车牌";
licenseColor = "无";
licenseColorCode = "4";
licenseType = "99";
}
else //有车牌识别结果
{
int bufferEnd = 8;
char endSign = '/0';
bufferEnd = buffer.IndexOf(endSign);
if (bufferEnd >= 8)
{
license = buffer.Substring(1, bufferEnd - 1);
licenseColor = buffer.Substring(0, 1);
switch (licenseColor)
{
case "白":
licenseColorCode = "0";
licenseType = "00";
break;
case "黄":
licenseColorCode = "1";
licenseType = "01";
break;
case "蓝":
licenseColorCode = "2";
licenseType = "02";
break;
case "黑":
licenseColorCode = "3";
if (license.IndexOf("使") > 0)
{
licenseType = "03";
}
else if (license.IndexOf("领") > 0)
{
licenseType = "04";
}
else if (license.IndexOf("境") > 0)
{
licenseType = "05";
}
else
{
licenseType = "06";
}
break;
case "绿":
licenseColorCode = "5";
licenseType = "07";
break;
default:
licenseColorCode = "4";
licenseType = "99";
break;
}
}
else //bufferEnd < 8 车牌字符串长度不合法,识别结果置为未知
{
license = "未知";
licenseColor = "未";
licenseColorCode = "4";
licenseType = "99";
}
} //End If
} //End GetLicenseInfo()
/// <summary>
/// 扫描源路径是否有车辆信息
/// </summary>
/// <returns>存在并读取成功返回1;无记录返回0;无加密返回-3;其它返回负数</returns>
public int Scan()
{
DirectoryInfo diSearch = new DirectoryInfo(sourcePath);
FileInfo[] fiList;
int retvalScan = 0;
string filePrefix = "";
string sourceXmlFile = "";
string sourceImageFull = "";
string sourceImageSpecial = "";
int retval = -999;
bool findFileFlag = false;
fiList = diSearch.GetFiles("*.xml", SearchOption.TopDirectoryOnly);
foreach (FileInfo fi in fiList)
{
findFileFlag = true;
filePrefix = fi.Name.Substring(0, fi.Name.LastIndexOf(@"."));
sourceXmlFile = fi.FullName;
sourceImageFull = sourcePath + @"/" + filePrefix + @"-1.jpg";
sourceImageSpecial = sourcePath + @"/" + filePrefix + @"-1_lp.jpg";
break;
}
if (!findFileFlag) // 如没有发现xml文件则直接退出函数
{
return retvalScan;
}
retvalScan = -999;
uint timeStartCount = UnsafeNativeMethods.timeGetTime();
DirectoryInfo subDirectory; // 检查目标文件夹是否存在,不存在则创建
string targetDirectory; // 获取子目录字符串
if (File.Exists(sourceImageFull) && File.Exists(sourceImageSpecial))
{
pchRecogImagePath.Initialize();
pchRecogImagePath = Encoding.Default.GetBytes(sourceImageSpecial);
Reset();
retval = UnsafeNativeMethods.GDW_RecJpeg(pchRecogImagePath, pchLicenseBuffer);
if (UnsafeNativeMethods.GDW_Recog_Ok == retval || UnsafeNativeMethods.GDW_Recog_Failure == retval)
{
GetLicenseInfo();
}
else if (UnsafeNativeMethods.GDW_Recog_NotFountMicroDog == retval)
{
retvalScan = UnsafeNativeMethods.GDW_Recog_NotFountMicroDog;
return retvalScan; // 未发现加密时立即返回
}
else
{
FileControl.Log.WriteToFile("Vehicle.Scan", "GDW_RecJpeg return: " + retval.ToString());
}
recognizeTime = (uint)Math.Abs(UnsafeNativeMethods.timeGetTime() - timeStartCount); // 获得识别时间
if (ReadXMLInfo(sourceXmlFile)) // 解析XML文件成功,获得车辆通行信息
{
targetDirectory = dstPath + @"/" + passTime.Substring(0, 4) + passTime.Substring(5, 2) + passTime.Substring(8, 2);
subDirectory = new DirectoryInfo(targetDirectory);
if (!subDirectory.Exists)
{
subDirectory.Create();
}
imageFullPath = targetDirectory + @"/" + filePrefix + @"-1.jpg";
imageSpecialPath = targetDirectory + @"/" + filePrefix + @"-1_lp.jpg";
xmlFilePath = targetDirectory + @"/" + filePrefix + @".xml";
try
{
if (File.Exists(xmlFilePath)) // 如果目标文件存在则删除
{
File.Delete(xmlFilePath);
}
if (File.Exists(imageSpecialPath))
{
File.Delete(imageSpecialPath);
}
if (File.Exists(imageFullPath))
{
File.Delete(imageFullPath);
}
File.Move(sourceXmlFile, xmlFilePath);
File.Move(sourceImageSpecial, imageSpecialPath);
File.Move(sourceImageFull, imageFullPath);
retvalScan = 1;
}
catch (Exception e)
{
FileControl.Log.WriteToFile("Vehicle.Scan", "move file exception: " + e.ToString());
}
}
else // 解析XML文件失败
{
retvalScan = -1;
}
}
else // 除2幅图片都存在的另外3种情况
{
targetDirectory = dstPath + @"/_Exception";
subDirectory = new DirectoryInfo(targetDirectory);
if (!subDirectory.Exists)
{
subDirectory.Create();
}
xmlFilePath = targetDirectory + @"/" + filePrefix + @".xml";
File.Move(sourceXmlFile, xmlFilePath);
if (File.Exists(sourceImageSpecial))
{
imageSpecialPath = targetDirectory + @"/" + filePrefix + @"-1_lp.jpg";
File.Move(sourceImageSpecial, imageSpecialPath);
}
else
{
FileControl.Log.WriteToFile("Vehicle.Scan", "file not find: " + sourceImageSpecial);
}
if (File.Exists(sourceImageFull))
{
imageFullPath = targetDirectory + @"/" + filePrefix + @"-1.jpg";
File.Move(sourceImageFull, imageFullPath);
}
else
{
FileControl.Log.WriteToFile("Vehicle.Scan", "file not find: " + sourceImageFull);
}
}
dealWholeTime = (uint)Math.Abs(UnsafeNativeMethods.timeGetTime() - timeStartCount); // 获得整个过程处理时间
return retvalScan;
}
/// <summary>
/// 获取数据库Insert语句
/// </summary>
/// <returns>数据库Insert语句</returns>
public string GetInsertSqlCommand()
{
string sqlCommand;
sqlCommand = "INSERT INTO tbVehicle (" +
"[index], " +
"license, " +
"licenseColor, " +
"licenseColorCode, " +
"licenseType, " +
"passTime, " +
"location, " +
"lane, " +
"type, " +
"direction, " +
"speed, " +
"speedLimitTrigger, " +
"speedLimitCar, " +
"speedLimitLorry, " +
"speedLimit, " +
"state, " +
"imageFullPath, " +
"imageSpecialPath, " +
"xmlFilePath, " +
"recognizeTime, " +
"editFlag, " +
"flag, " +
"reserved1)" +
"VALUES (" +
index.ToString() + "," +
"'" + license + "'," +
"'" + licenseColor + "'," +
"'" + licenseColorCode + "'," +
"'" + licenseType + "'," +
"'" + passTime + "'," +
"'" + location + "'," +
lane.ToString() + "," +
"'" + type + "'," +
"'" + direction + "'," +
speed.ToString() + "," +
speedLimitTrigger.ToString() + "," +
speedLimitCar.ToString() + "," +
speedLimitLorry.ToString() + "," +
speedLimit.ToString() + "," +
state.ToString() + "," +
"'" + imageFullPath + "'," +
"'" + imageSpecialPath + "'," +
"'" + xmlFilePath + "'," +
recognizeTime.ToString() + "," +
"0, 0, '')";
return sqlCommand;
}
}
}