用MVS SDK采集的图片的数据格式是:
IntPtr imageBuffer = IntPtr.Zero;
int width, height, chanles;
GetImage(out width, out height, out chanles, out imageBuffer);
如果我们在得到图片数据后,需要用到VM的算子开发。我们需要把图片生成为CMvdImage:
我们需要:
CMvdImage InputImage = new CMvdImage();
MVD_IMAGE_DATA_INFO imageDataStruct = new MVD_IMAGE_DATA_INFO();
imageDataStruct.stDataChannel[0].nRowStep = Convert.ToUInt32(width);
imageDataStruct.stDataChannel[0].nLen = Convert.ToUInt32(width * height);
imageDataStruct.stDataChannel[0].nSize = Convert.ToUInt32(width * height);
//IntPtr 转 byte[]
int size = Convert.ToInt32(width * height);
byte[] managedArray = new byte[size];
Marshal.Copy(imageBuffer, managedArray, 0, size);
imageDataStruct.stDataChannel[0].arrDataBytes = managedArray;
InputImage.InitImage((uint)width, (uint)height, VisionDesigner.MVD_PIXEL_FORMAT.MVD_PIXEL_MONO_08, imageDataStruct);
InputImage.SaveImage("E:\\百度网盘下载\\3.bmp"); //保存图片测试
这就图像数据转换成功了。
延伸课题:-------------------如果我们得到byte[]数据,怎么转为IntPtr呢----------------------------
下面提供一种方法:
IntPtr IntPtr_=ArrToPtr(managedArray);
具体方法为:
IntPtr ArrToPtr(byte[] array)
{
return System.Runtime.InteropServices.Marshal.UnsafeAddrOfPinnedArrayElement(array, 0);
}
然后可以进行测试转换是否成功:
HOperatorSet.GenImage1(out image, "byte", width, height, ArrToPtr(managedArray));
HOperatorSet.WriteImage(image, "jpg", 0, "E:\\百度网盘下载\\2.jpg");
查看本地图片效果:---->图片一样,说明转换成功。
所有的测试程序为:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using EventMgrLib;
using HikCam;
using TouchSocket.Core;
using UIImagePress;
using UpVisionService;
using VisionDesigner;
namespace UpVisionService
{
public partial class test : Form
{
/// <summary>
/// 相机SN-->1_1 1_2 1_3 1_4 2_1 2_2 2_3 2_4
/// </summary>
private List<Cam> camSnList = new List<Cam>()
{
new Cam() { Name = "相机1_1" },
};
private List<OneCam> cams = new List<OneCam>() { new OneCam() };
public test()
{
InitializeComponent();
camSnList[0].SN = "K73949038";
int i = 0;
foreach (var item in cams)
{
camSnList[i].Statue = item.Init(camSnList[i].SN);
i++;
}
}
private void btn_save_Click(object sender, EventArgs e)
{
CMvdImage InputImage = new CMvdImage();
Stopwatch sw = new Stopwatch();
sw.Restart();
HObject image = new HObject();
IntPtr imageBuffer = IntPtr.Zero;
int width, height, chanles;
if (cams[0].GetImage(out width, out height, out chanles, out imageBuffer))
{
HOperatorSet.GenImage1(out image, "byte", width, height, imageBuffer);
HOperatorSet.WriteImage(image, "jpg", 0, "E:\\百度网盘下载\\1.jpg");
MVD_IMAGE_DATA_INFO imageDataStruct = new MVD_IMAGE_DATA_INFO();
imageDataStruct.stDataChannel[0].nRowStep = Convert.ToUInt32(width);
imageDataStruct.stDataChannel[0].nLen = Convert.ToUInt32(width * height);
imageDataStruct.stDataChannel[0].nSize = Convert.ToUInt32(width * height);
//IntPtr 转 byte[]
int size = Convert.ToInt32(width * height);
byte[] managedArray = new byte[size];
Marshal.Copy(imageBuffer, managedArray, 0, size);
imageDataStruct.stDataChannel[0].arrDataBytes = managedArray;
InputImage.InitImage((uint)width, (uint)height, VisionDesigner.MVD_PIXEL_FORMAT.MVD_PIXEL_MONO_08, imageDataStruct);
InputImage.SaveImage("E:\\百度网盘下载\\3.bmp");
//另外备注一下 byte[] 转 IntPtr
HOperatorSet.GenImage1(out image, "byte", width, height, ArrToPtr(managedArray));
HOperatorSet.WriteImage(image, "jpg", 0, "E:\\百度网盘下载\\2.jpg");
Console.WriteLine($"获取图片用时:{sw.ElapsedMilliseconds}毫秒");
}
else
{
Console.WriteLine($"获取图片用时:{sw.ElapsedMilliseconds}毫秒");
}
}
IntPtr ArrToPtr(byte[] array)
{
return System.Runtime.InteropServices.Marshal.UnsafeAddrOfPinnedArrayElement(array, 0);
}
private void test_FormClosing(object sender, FormClosingEventArgs e)
{
cams[0].CloseCam();
}
}
核心图片: