内存映射数据处理类主要函数及变量如下:

         string _filepath;
/// <summary>
/// 引用内存映射文件
/// </summary>
private MemoryMappedFile _memoryFile = null;
/// <summary>
/// 用于访问内存映射文件的存取对象
/// </summary>
private MemoryMappedViewAccessor _accessor = null;
public ScientificData _ScientificData = new ScientificData();
long _lenByte = ;
public DatFileInfo(string filepath)
{
_filepath = filepath;
_memoryFile = MemoryMappedFile.CreateFromFile(_filepath);
_accessor = _memoryFile.CreateViewAccessor();
// _stream = _memoryFile.CreateViewStream();
FileInfo finfo = new FileInfo(filepath);
_lenByte = finfo.Length;//文件字节大小
}
public void SaveRawData(string savepath)
{ int currentByteNum = ;//当前字节位置
uint ACountint = ;
uint RCountint = ;
ScientificData scientificData = new ScientificData();
byte[] data = new byte[ * ];
while (currentByteNum<= (_lenByte- * ))
{
_accessor.Read<uint>(currentByteNum, out RCountint);
_accessor.Read<uint>(currentByteNum+, out ACountint);
if (RCountint < && ACountint < && _accessor.ReadByte(currentByteNum+)==0x0a && _accessor.ReadByte(currentByteNum + ) == 0x0b)//初步判断条件,节省解析结构体时间
{
_accessor.ReadArray(currentByteNum, data, , data.Length);//读取结构体数据到字节数组
scientificData = ByteToStructure<ScientificData>(data);//字节数组解析到结构体
if((scientificData.aux_3a1 == 0x3A) && (scientificData.aux_3a3 == 0x3A))//进一步判断
{
ushort[,] sdata = scientificData.GetImageData();//得到所需的数据
saveRawData(savepath + ((int)((ACountint - )/+)).ToString()+ "_" + (ACountint-).ToString() + "_"+ACountint + "_"+scientificData.aux_num + ".raw" , sdata);
currentByteNum += * ;
}
else
currentByteNum++;
}
else
currentByteNum++; }
}
/// <summary>
/// 由byte数组转换为结构体
/// </summary>
public static T ByteToStructure<T>(byte[] dataBuffer)
{
object structure = null;
int size = Marshal.SizeOf(typeof(T));
IntPtr allocIntPtr = Marshal.AllocHGlobal(size);
try
{
Marshal.Copy(dataBuffer, , allocIntPtr, size);
structure = Marshal.PtrToStructure(allocIntPtr, typeof(T));
}
finally
{
Marshal.FreeHGlobal(allocIntPtr);
}
return (T)structure;
}
private void saveRawData(string savepath,ushort[,] data)
{
int len = data.Length*;
byte[] bdata = new byte[len];
Buffer.BlockCopy(data,,bdata,,len);
File.WriteAllBytes(savepath, bdata);
}
/// <summary>
/// 由结构体转换为byte数组
/// </summary>
public static byte[] StructureToByte<T>(T structure)
{
int size = Marshal.SizeOf(typeof(T));
byte[] buffer = new byte[size];
IntPtr bufferIntPtr = Marshal.AllocHGlobal(size);
try
{
Marshal.StructureToPtr(structure, bufferIntPtr, true);
Marshal.Copy(bufferIntPtr, buffer, , size);
}
finally
{
Marshal.FreeHGlobal(bufferIntPtr);
}
return buffer;
}

科学数据结构体定义如下:

  //一幅1036*1036字节数据定义
public struct ScientificData
{
/参数信息
[MarshalAs(UnmanagedType.ByValArray, SizeConst = )]
public byte[] RelativePacketCount;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = )]
public Byte[] AbsolutePacketCount;
........
public byte aux_3a;//填充3A H
.........
[MarshalAs(UnmanagedType.ByValArray, SizeConst = )]
public OneImageRow[] ImageData;//图像数据行
/// <summary>
/// 获取raw图数据
/// </summary>
/// <returns>图像数据</returns>
public ushort[,] GetImageData()
{
ushort[,] rawdata = new ushort[, ];
for (int i = ; i < ; i++)
{
var onerow = ImageData[i];
for (int j = ; j < ; j++)
{
rawdata[i, j] = (ushort)(((onerow.imagedata[j * ] << ) | onerow.imagedata[j * + ])) ;
}
}
return rawdata;
}
}

图像数据结构体如下:

    public struct OneImageRow
{
[MarshalAs(UnmanagedType.ByValArray, SizeConst = )]
public byte[] RelativePacketCount;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = )]
public byte[] AbsolutePacketCount;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = )]
public byte[] linehead;//行头
[MarshalAs(UnmanagedType.ByValArray, SizeConst = )]
public byte[] linenum;//行号
[MarshalAs(UnmanagedType.ByValArray, SizeConst = )]
public byte[] imagedata;//图像数据512×2=1024字节 public static string ByteToHex(byte[] bt)
{
var hex = BitConverter.ToString(bt, ).ToUpper();
return hex;
}
}

最新文章

  1. bootstrap-简洁实用的jQuery手风琴插件
  2. 算法系列:FFT 003
  3. EF架构~二级域名中共享Session
  4. NOIP2015 子串
  5. git delete repository
  6. shell案例
  7. JSBinding + SharpKit / 初体验:下载代码及运行Demo
  8. 【转】Win7环境下VS2010配置Cocos2d-x-2.1.4最新版本的开发环境(亲测)
  9. Volley框架支持HTTPS请求。
  10. iOS开发之UIApplication
  11. MyGui笔记(1)建立第一个工程
  12. HDU ACM 1065 I Think I Need a Houseboat
  13. Getting Real内容浓缩
  14. 【转载】IL指令集
  15. maven将依赖依赖打包到jar中
  16. Vue 目录结构 绑定数据 绑定属性 循环渲染数据
  17. IDEA 创建和使用tomcat
  18. ARM 非对齐的数据访问操作
  19. SWIFT Scan QRCode
  20. Android setTag()/getTag()

热门文章

  1. BZOJ4358:permu
  2. LeetCode第一题:Two Sum
  3. JSP/java 执行创建批处理文件,并执行批处理事务。
  4. java代码swing编程JPaswordField类
  5. 开发环境无错,部署至测试环境报错“NoSuchMethodError”OR&quot;NoSuchClassError&quot;
  6. Celery-4.1 用户指南: Optimizing (优化)
  7. 2015.5.9 C#编写DLL及C#调用C#DLL
  8. sqlserver 使用维护计划备份
  9. spring整合mybatis的事物管理配置
  10. 卸载sql2008r2简易版