接着上一篇,与黑白相机相同的初始化就掠过了。
看代码:
// //以下针对彩色相机20220427//已经ok2592*1944//都是以大华相机测试的,也就是说,海康的相机sdk兼容了大华
if (!checkBoxBW.Checked)
{
Bitmap cutPicrgb = new Bitmap(FrameInfo.nWidth, FrameInfo.nHeight,
System.Drawing.Imaging.PixelFormat.Format24bppRgb);
BitmapData _cutPicrgb
= cutPicrgb.LockBits(new Rectangle(0, 0, FrameInfo.nWidth, FrameInfo.nHeight),
ImageLockMode.ReadWrite, cutPicrgb.PixelFormat);
IntPtr ptrrgb
= _cutPicrgb.Scan0;//得到首地址
byte[] rgbValues = new byte[FrameInfo.nWidth * FrameInfo.nHeight * 3];//24位彩色图
把cutvalues数组给ptr
System.Runtime.InteropServices.Marshal.Copy(data, 0, ptrrgb, FrameInfo.nWidth * FrameInfo.nHeight * 3);
System.Runtime.InteropServices.Marshal.Copy(ptrrgb, rgbValues, 0, FrameInfo.nWidth * FrameInfo.nHeight * 3);
cutPicrgb.UnlockBits(_cutPicrgb);
byte[] data2 = new byte[FrameInfo.nHeight * FrameInfo.nWidth];
byte[] data3 = new byte[FrameInfo.nHeight * FrameInfo.nWidth*3];
for (int i = 0; i < FrameInfo.nHeight; i++)
for (int j = 0; j < FrameInfo.nWidth; j++)
{
int nn = i * FrameInfo.nWidth + j;
int mm=((FrameInfo.nHeight - 1 - i) * FrameInfo.nWidth + j) * 3;
data3[nn * 3] = data2[nn] = rgbValues[mm];
data3[nn * 3+1] = rgbValues[mm+1];
data3[nn * 3 + 2] = rgbValues[mm + 2];
}
int bytes = ww * hh * 3;//1024*768*3//20220507
byte[] bufercopy = new byte[bytes];
int yy = (FrameInfo.nHeight - hh) / 2;//FrameInfo.nHeight
int xx = (FrameInfo.nWidth - ww) / 2;// FrameInfo.nWidth
for (int ii = yy; ii < yy + hh; ii++)//图像复原,即由8位图变成24位图像
{
for (int j = xx; j < xx + ww; j++)
{//bmp的z序已经处理正常
int n = ii * FrameInfo.nWidth + j;
int k = (ii - yy) * ww + j - xx;
int m = 3 * k;
rgbbufercopyB[k]= rgbbufercopy[m] = data3[3*n];
rgbbufercopyG[k]= rgbbufercopy[m + 1] = data3[3 * n + 1];
rgbbufercopyR[k]= rgbbufercopy[m + 2] = data3[3 * n + 2];
glob_buffer8[k] = (byte)(0.3 * rgbbufercopyR[k] + 0.59 * rgbbufercopyG[k] + 0.11 * rgbbufercopyB[k]);//这里是1024*768图像
glob_buffer8[k] = checkBoxConvertBW.Checked ? (byte)(255 - glob_buffer8[k]) : glob_buffer8[k];
}
}
showbuffer2pict(glob_buffer8, 1024, 768, pictureBox12rgb361);
showbuffer2pict(rgbbufercopyR, 1024, 768, pictureBox12R);//202208190948 rgb bitmap处理
showbuffer2pict(rgbbufercopyG, 1024, 768, pictureBox13G);//202208190948 rgb bitmap处理
showbuffer2pict(rgbbufercopyB, 1024, 768, pictureBox14B);//202208190948 rgb bitmap处理
showrgbbuffer2pict(data3, FrameInfo.nWidth, FrameInfo.nHeight, pictureBoxrgb);//202208190948 rgb bitmap处理
}
需要解释的是:我们输出了五种灰度图,一种彩色图:
a,0.3r+0.6g+0.1b
b,红色分量灰度图
c,绿色分量灰度图
d,蓝色分量灰度图
e,黑白反色灰度图
另外解释 showrgbbuffer2pict函数:
void showrgbbuffer2pict(byte[] buffer, int ww, int hh, PictureBox destImg)
{
// destImg.Size = new System.Drawing.Size(ww, hh);
//顯示
byte[] cutvalues = new byte[ww * hh * 3];
int bytes = ww * hh * 3;
Bitmap cutPic24 = new Bitmap(ww, hh, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
BitmapData _cutPic = cutPic24.LockBits(new Rectangle(0, 0, ww, hh), ImageLockMode.ReadWrite,
cutPic24.PixelFormat);
IntPtr ptr = _cutPic.Scan0;//得到首地址
for (int i = 0; i < hh*ww*3; i++)
{
cutvalues[i] = buffer[i];
//for (int j = 0; j < ww; j++)
//{
// int n = i * ww + j;
// int m = 3 * n;
// cutvalues[m] = buffer[n];
// cutvalues[m + 1] = buffer[n];
// cutvalues[m + 2] = buffer[n];
//}
}
System.Runtime.InteropServices.Marshal.Copy(cutvalues, 0, ptr, bytes);
cutPic24.UnlockBits(_cutPic);
destImg.Image = cutPic24;
}
最后,退出程序要关闭相机:
Marshal.FreeHGlobal(pBufForDriver);
Marshal.FreeHGlobal(pBufForSaveImage);
// ch:停止抓图 | en:Stop grab image
nRet = device.MV_CC_StopGrabbing_NET();
// ch:关闭设备 | en:Close device
nRet = device.MV_CC_CloseDevice_NET();
// ch:销毁设备 | en:Destroy device
nRet = device.MV_CC_DestroyDevice_NET();
ok,结束。