private void CameraGrabberFrameCallback(IntPtr Grabber, IntPtr pFrameBuffer, ref tSdkFrameHead pFrameHead, IntPtr Context) { int w = pFrameHead.iWidth; int h = pFrameHead.iHeight; bool gray = (pFrameHead.uiMediaType == (uint)emImageFormat.CAMERA_MEDIA_TYPE_MONO8); Bitmap ImageDisp = new Bitmap(w, h, gray ? w : w * 3, gray ? PixelFormat.Format8bppIndexed : PixelFormat.Format24bppRgb, pFrameBuffer); // 如果是灰度图要设置调色板 if (gray) { ImageDisp.Palette = m_GrayPal; } using (var stream = new MemoryStream()) { ImageDisp.Save(stream, ImageFormat.Bmp); App.Current.Dispatcher.InvokeAsync(new Action(() => { SetPreviewImage(stream); })); } if (DetectFlag) { Image.Bitmap = ImageDisp; ImageQueue.Enqueue(Image); CapCount++; } }
// thread safe wrt. Pop internal void Push(Emgu.CV.UMat image) { if (image != null) { if (ImageQueue.Count < Nthreads_ * 2) { ImageQueue.Enqueue(image); } else { //we are reaching into memory limits, so dispose it to prevent //exhausting the ram image.Dispose(); } } }
private void Hooks_DispatcherInactive(object sender, EventArgs e) { using (Mat source = capture.QueryFrame()) { var frame = new Mat(); CvInvoke.Resize(source, frame, ImageSize); if (frame != null && DetectFlag && !frame.IsEmpty) { Image <Bgr, Byte> Image = new Image <Bgr, Byte>(ImageSize); Image.Bitmap = (Bitmap)frame.Bitmap.Clone(); ImageQueue.Enqueue(Image); using (var stream = new MemoryStream()) { frame.Bitmap.Save(stream, ImageFormat.Png); SetPreviewImage(stream); } } } }