using System; using System.Collections.Generic; using System.Text; using System.IO; namespace dccdc.Selfhelp { class GFunction { /// /// 将字节数组写入文件 /// /// 文件路径 /// 写入字节数组 /// 字节数组大小 /// 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; } /// /// 对BGR格式数据进行B、R转换,只支持24位深度图像 /// /// bgr数据 /// bgr数据大小 /// 转换后的rgb格式数据,需要开((pi_iWidth * 3 + 3) / 4) * 4 * pi_iHeight字节空间 /// 开辟空间大小 /// 图片宽度(像素) /// 图片高度(像素) /// >0 执行成功,函数执行成功后返回转换后的rgb格式数据大小 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; } /// /// 字节数组转16进制字符串 /// /// 字节数组 /// 字节数组偏移量 /// 转换字节数 /// 16进制字符串 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进制字符串转字节数组 /// /// 16进制字符串 /// 字节数组Buffer /// 字节数组Buffer大小 /// 实际转换大小 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; } } }