/// <summary> /// Takes a converted input buffer and copy it into the output buffer. /// </summary> private unsafe void CopyFrame(BGAPI2.Image image, int length) { // At this point the image is either in Mono8, Bayer**8 or BGR8. fixed(byte *p = frameBuffer) { IntPtr ptrDst = (IntPtr)p; NativeMethods.memcpy(ptrDst.ToPointer(), image.Buffer.ToPointer(), length); } }
//private void imageProvider_GrabbingStartedEvent() //{ // grabbing = true; // if (GrabbingStatusChanged != null) // GrabbingStatusChanged(this, EventArgs.Empty); //} private void BaumerProvider_BufferProduced(object sender, BufferEventArgs e) { BGAPI2.Buffer buffer = e.Buffer; if (buffer == null || buffer.IsIncomplete || buffer.MemPtr == IntPtr.Zero) { return; } int payloadLength = (int)buffer.SizeFilled; // Wrap the buffer in an image, convert if needed. BGAPI2.Image image = imgProcessor.CreateImage((uint)buffer.Width, (uint)buffer.Height, buffer.PixelFormat, buffer.MemPtr, buffer.MemSize); bool ready = imageFormat == ImageFormat.JPEG || (imageFormat == ImageFormat.Y800 && BaumerHelper.IsY800(image.PixelFormat)); if (!ready) { // Color conversion is required. BGAPI2.Image transformedImage = GetTransformedImage(image); image.Release(); image = transformedImage; int bpp = BaumerHelper.IsY800(image.PixelFormat) ? 1 : 3; payloadLength = (int)(image.Width * image.Height * bpp); } CopyFrame(image, payloadLength); image.Release(); if (imageFormat != ImageFormat.JPEG && finishline.Enabled) { bool flush = finishline.Consolidate(frameBuffer); if (flush) { ComputeDataRate(finishline.BufferOutput.Length); if (FrameProduced != null) { FrameProduced(this, new FrameProducedEventArgs(finishline.BufferOutput, finishline.BufferOutput.Length)); } } } else { ComputeDataRate(payloadLength); if (FrameProduced != null) { FrameProduced(this, new FrameProducedEventArgs(frameBuffer, payloadLength)); } } }
/// <summary> /// Takes a raw buffer and copy it into an existing RGB24 Bitmap. /// </summary> public unsafe bool FillRGB24(BGAPI2.Buffer buffer, Bitmap outputImage) { if (buffer == null || buffer.IsIncomplete || buffer.MemPtr == IntPtr.Zero) { return(false); } if (buffer.Width != (ulong)outputImage.Width || buffer.Height != (ulong)outputImage.Height) { return(false); } bool filled = false; // If the input image is a bayer pattern it will be debayered by default by the image processor. // If it's Mono it will be converted to RGB by the image processor. // The input image cannot be JPEG because we temporarily switch off that option before acquisition. BGAPI2.ImageProcessor imgProcessor = new BGAPI2.ImageProcessor(); BGAPI2.Image img = imgProcessor.CreateImage((uint)buffer.Width, (uint)buffer.Height, buffer.PixelFormat, buffer.MemPtr, buffer.SizeFilled); BGAPI2.Image transformedImage = imgProcessor.CreateTransformedImage(img, "BGR8"); img.Release(); img = transformedImage; // Push the transformed image into the passed output bitmap. Rectangle rect = new Rectangle(0, 0, outputImage.Width, outputImage.Height); BitmapData bmpData = null; try { bmpData = outputImage.LockBits(rect, ImageLockMode.WriteOnly, outputImage.PixelFormat); IntPtr[] ptrBmp = new IntPtr[] { bmpData.Scan0 }; NativeMethods.memcpy(bmpData.Scan0.ToPointer(), img.Buffer.ToPointer(), rect.Width * rect.Height * 3); filled = true; } catch (Exception e) { log.ErrorFormat("Error while copying bitmap. {0}", e.Message); } finally { if (bmpData != null) { outputImage.UnlockBits(bmpData); } } img.Release(); return(filled); }
private BGAPI2.Image GetTransformedImage(BGAPI2.Image image) { BGAPI2.Image transformedImage = null; if (!demosaicing && image.PixelFormat.StartsWith("Bayer")) { // HDR Bayer. Convert to 8-bit while retaining the format. // Transformation from a bayer pattern to another is not supported by the API. if (image.PixelFormat.StartsWith("BayerBG")) { transformedImage = imgProcessor.CreateTransformedImage(image, "BayerBG8"); } else if (image.PixelFormat.StartsWith("BayerGB")) { transformedImage = imgProcessor.CreateTransformedImage(image, "BayerGB8"); } else if (image.PixelFormat.StartsWith("BayerGR")) { transformedImage = imgProcessor.CreateTransformedImage(image, "BayerGR8"); } else { transformedImage = imgProcessor.CreateTransformedImage(image, "BayerRG8"); } } else { // HDR Mono and all other cases (RGB & YUV). if (image.PixelFormat.StartsWith("Mono")) { transformedImage = imgProcessor.CreateTransformedImage(image, "Mono8"); } else { transformedImage = imgProcessor.CreateTransformedImage(image, "BGR8"); } } return(transformedImage); }
public void mDataStream_NewBufferEvent(object sender, BGAPI2.Events.NewBufferEventArgs mDSEvent) { try { Stopwatch sw = new Stopwatch(); sw.Restart(); BGAPI2.Buffer mBufferFilled = null; mBufferFilled = mDSEvent.BufferObj; if (mBufferFilled == null) { } else if (mBufferFilled.IsIncomplete == true) { mBufferFilled.QueueBuffer(); } else { int width = (int)mBufferFilled.Width; int height = (int)mBufferFilled.Height; m_nImageWidth = width; m_nImageHeight = height; m_sPixelFormat = mBufferFilled.PixelFormat; if (mBufferFilled.PixelFormat == "Mono8") { ulong size = mBufferFilled.SizeFilled; IntPtr pBuffer = mBufferFilled.MemPtr; m_pImgData = new byte[width * height]; Marshal.Copy(pBuffer, m_pImgData, 0, width * height); } else //color { ulong size = mBufferFilled.SizeFilled; BGAPI2.Image mImage = imgProcessor.CreateImage((uint)mBufferFilled.Width, (uint)mBufferFilled.Height, (string)mBufferFilled.PixelFormat, mBufferFilled.MemPtr, (ulong)mBufferFilled.MemSize); // BGAPI2.Image mImageTransfer = mImage.TransformImage("BGR8"); BGAPI2.Image mTransformImage = imgProcessor.CreateTransformedImage(mImage, "BGR8"); try { m_pImgData = new byte[(uint)((uint)mImage.Width * (uint)mImage.Height * 3.0)]; Marshal.Copy(mTransformImage.Buffer, m_pImgData, 0, (int)((int)mImage.Width * (int)mImage.Height * 3.0)); } catch (Exception) { GrabComplete.Set(); MessageBox.Show("拍照处理异常"); } if (mImage != null) { mImage.Release(); mTransformImage.Release(); } } string mode = mDevice.RemoteNodeList["TriggerSource"].Value; if (mode == "Line0") { HTRGNum++; } OnRan(m_pImgData, width, height, m_sPixelFormat); GrabComplete.Set(); mBufferFilled.QueueBuffer(); sw.Stop(); AcqTime = sw.ElapsedMilliseconds; //MessageBox.Show(AcqTime.ToString()); } } catch (BGAPI2.Exceptions.IException ex) { GrabComplete.Set(); string str; str = string.Format("ExceptionType:{0}! ErrorDescription:{1} in function:{2}", ex.GetType(), ex.GetErrorDescription(), ex.GetFunctionName()); MessageBox.Show("002" + str); } }