tijian_tieying/winform/cn.xinelu.MedicalCheckup.Client/Common/extend.cs
2025-02-20 12:01:17 +08:00

152 lines
6.3 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
namespace cn.xinelu.MedicalCheckup.Client.Common
{
public static class extend
{
#region API声明
//二代证相关API---------------------------------------------------------------------------------------
[DllImport("sdtapi.dll", CallingConvention = CallingConvention.StdCall)]
public static extern int SDT_GetCOMBaud(int iPort, ref uint puiBaudRate);
[DllImport("sdtapi.dll", CallingConvention = CallingConvention.StdCall)]
public static extern int SDT_SetCOMBaud(int iPort, uint uiCurrBaud, uint uiSetBaud);
[DllImport("sdtapi.dll", CallingConvention = CallingConvention.StdCall)]
public static extern int SDT_StartFindIDCard(int iPort, byte[] pucManaInfo, int iIfOpen);
[DllImport("sdtapi.dll", CallingConvention = CallingConvention.StdCall)]
public static extern int SDT_SelectIDCard(int iPort, byte[] pucManaMsg, int iIfOpen);
[DllImport("sdtapi.dll", CallingConvention = CallingConvention.StdCall)]
public static extern int SDT_ReadBaseMsg(int iPort, byte[] pucCHMsg, ref uint puiCHMsgLen, byte[] pucPHMsg, ref uint puiPHMsgLen, int iIfOpen);
[DllImport("sdtapi.dll", CallingConvention = CallingConvention.StdCall)]
public static extern int SDT_ReadBaseFPMsg(int iPort, byte[] pucCHMsg, ref uint puiCHMsgLen, byte[] pucPHMsg, ref uint puiPHMsgLen, byte[] pucFPMsg, ref uint puiFPMsgLen, int iIfOpen);
[DllImport("sdtapi.dll", CallingConvention = CallingConvention.StdCall)]
public static extern int SDT_GetSAMIDToStr(int iPort, StringBuilder pcSAMID, int iIfOpen);
[DllImport("sdtapi.dll", CallingConvention = CallingConvention.StdCall)]
public static extern int SDT_ReadNewAppMsg(int iPort, byte[] pucAppMsg, ref uint puiAppMsgLen, int iIfOpen);
//照片解码API---------------------------------------------------------------------------------------
[DllImport("DLL_File.dll", CallingConvention = CallingConvention.Cdecl)]//注意这里的调用方式为Cdecl
public static extern int unpack(byte[] pi_szSrcWltData, byte[] szDstPicData, int iIsSaveToBmp);
#endregion
}
class GFunction
{
/// <summary>
/// 将字节数组写入文件
/// </summary>
/// <param name="pi_strFileName">文件路径</param>
/// <param name="pi_byData">写入字节数组</param>
/// <param name="pi_iDataSize">字节数组大小</param>
/// <returns></returns>
static public bool tool_WriteOneFile(string pi_strFileName, byte[] pi_byData, int pi_iDataSize)
{
if (pi_iDataSize == 0)
{
return false;
}
FileStream fileStream = new FileStream(pi_strFileName, FileMode.Create);
fileStream.Write(pi_byData, 0, pi_iDataSize);
fileStream.Flush();
fileStream.Close();
return true;
}
/// <summary>
/// 对BGR格式数据进行B、R转换只支持24位深度图像
/// </summary>
/// <param name="pi_bySrc">bgr数据</param>
/// <param name="pi_iSrcSize">bgr数据大小</param>
/// <param name="po_byDst">转换后的rgb格式数据需要开((pi_iWidth * 3 + 3) / 4) * 4 * pi_iHeight字节空间</param>
/// <param name="pi_iDstSize">开辟空间大小</param>
/// <param name="pi_iWidth">图片宽度(像素)</param>
/// <param name="pi_iHeight">图片高度(像素)</param>
/// <returns>>0 执行成功函数执行成功后返回转换后的rgb格式数据大小</returns>
static public int bgr2rgb(byte[] pi_bySrc, int pi_iSrcSize, byte[] po_byDst, int pi_iDstSize, int pi_iWidth, int pi_iHeight)
{
int iWidthSize = pi_iWidth * 3;
int iDstWidthSize = ((pi_iWidth * 3 + 3) / 4) * 4;
int iExternSize = ((pi_iWidth * 3 + 3) / 4) * 4 - pi_iWidth * 3;
int iDstSize = iDstWidthSize * pi_iHeight;
int iPosX = 0;
int iPosY = 0;
if (pi_iSrcSize != (iWidthSize * pi_iHeight))
{
return -1;
}
if (pi_iDstSize < iDstSize)
{
return -2;
}
for (iPosY = 0; iPosY < pi_iHeight; iPosY++)
{
for (iPosX = 0; iPosX < pi_iWidth * 3; iPosX += 3)
{
po_byDst[(iWidthSize + iExternSize) * iPosY + iPosX + 0] = pi_bySrc[iWidthSize * iPosY + iPosX + 2];
po_byDst[(iWidthSize + iExternSize) * iPosY + iPosX + 1] = pi_bySrc[iWidthSize * iPosY + iPosX + 1];
po_byDst[(iWidthSize + iExternSize) * iPosY + iPosX + 2] = pi_bySrc[iWidthSize * iPosY + iPosX + 0];
}
}
return iDstSize;
}
/// <summary>
/// 字节数组转16进制字符串
/// </summary>
/// <param name="pi_bySrc">字节数组</param>
/// <param name="pi_iSrcPosIdx">字节数组偏移量</param>
/// <param name="pi_iSrcSize">转换字节数</param>
/// <returns>16进制字符串</returns>
public static string ByteArryToHexString(byte[] pi_bySrc, int pi_iSrcPosIdx, int pi_iCount)
{
int iPos = 0;
StringBuilder strResult = new StringBuilder();
for (iPos = 0; iPos < pi_iCount; iPos++)
{
strResult.Append(pi_bySrc[pi_iSrcPosIdx + iPos].ToString("X2"));
}
return strResult.ToString();
}
/// 16进制字符串转字节数组
/// </summary>
/// <param name="pi_strSrc">16进制字符串</param>
/// <param name="po_byDstBuffer">字节数组Buffer</param>
/// <param name="pi_iDstBufferSize">字节数组Buffer大小</param>
/// <returns>实际转换大小</returns>
public static int HexStringToByteArray(string pi_strSrc, byte[] po_byDstBuffer, int pi_iDstBufferSize)
{
int iPos = 0;
int iDstSize = pi_strSrc.Length / 2;
for (iPos = 0; (iPos < iDstSize) && (iPos < pi_iDstBufferSize); iPos++)
{
po_byDstBuffer[iPos] = Convert.ToByte(pi_strSrc.Substring(iPos * 2, 2), 16);
}
return iDstSize;
}
}
}