c# - 如何将 ISampleGrabber::BufferCB 的缓冲区转换为位图

标签 c# image-processing bitmap directshow directshow.net

我正在尝试使用 ISampleGrabberCB::BufferCB 使用以下代码将当前帧转换为位图:

int ISampleGrabberCB.BufferCB(double sampleTime, IntPtr buffer, int bufferLength)
    {
        try
        {

            Form1 form1 = new Form1("", "", "");
            if (pictureReady == null)
            {
                Debug.Assert(bufferLength == Math.Abs(pitch) * videoHeight, "Wrong Buffer Length");
            }

            Debug.Assert(imageBuffer != IntPtr.Zero, "Remove Buffer");

            Bitmap bitmapOfCurrentFrame = new Bitmap(width, height, capturePitch, PixelFormat.Format24bppRgb, buffer);
            MessageBox.Show("Works");
            form1.changepicturebox3(bitmapOfCurrentFrame);

            pictureReady.Set();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }

        return 0;
    }

但是这似乎不起作用。

此外,当我按下运行以下代码的按钮时,它似乎会调用此函数:

public IntPtr getFrame()
    {
        int hr;
        try
        {
            pictureReady.Reset();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }
        imageBuffer = Marshal.AllocCoTaskMem(Math.Abs(pitch) * videoHeight);

        try
        {
            gotFrame = true;

            if (videoControl != null)
            {
                hr = videoControl.SetMode(stillPin, VideoControlFlags.Trigger);
                DsError.ThrowExceptionForHR(hr);
            }

            if (!pictureReady.WaitOne(9000, false))
            {
                throw new Exception("Timeout waiting to get picture");
            }

        }
        catch
        {
            Marshal.FreeCoTaskMem(imageBuffer);
            imageBuffer = IntPtr.Zero;
        }

        return imageBuffer;

    }

运行此代码后,我会收到一个显示“Works”的消息框,这意味着我的 BufferCB 必须被调用,但不会用当前图像更新我的图片框。

BufferCB 不是在每个新帧之后调用吗?如果是这样,为什么我没有收到“Works”消息框?

最后,是否有可能使用 BufferCB 将每个新帧转换为位图(这用于以后的处理),如果可以,如何实现?

编辑后的代码:

int ISampleGrabberCB.BufferCB(double sampleTime, IntPtr buffer, int bufferLength)
    {           

            Debug.Assert(bufferLength == Math.Abs(pitch) * videoHeight, "Wrong Buffer Length"); 
            Debug.Assert(imageBuffer != IntPtr.Zero, "Remove Buffer");
            CopyMemory(imageBuffer, buffer, bufferLength);
            Decode(buffer);   


        return 0;
    }

public Image Decode(IntPtr imageData)
    {
        var bitmap = new Bitmap(width, height, pitch, PixelFormat.Format24bppRgb, imageBuffer);
        bitmap.RotateFlip(RotateFlipType.RotateNoneFlipY);
        Form1 form1 = new Form1("", "", "");
        form1.changepicturebox3(bitmap);
        bitmap.Save("C:\\Users\\...\\Desktop\\A2 Project\\barcode.jpg");
        return bitmap;
    }

按钮代码:

public void getFrameFromWebcam()
{
   if (iPtr != IntPtr.Zero)
   {
       Marshal.FreeCoTaskMem(iPtr);
       iPtr = IntPtr.Zero;
   }

        //Get Image
        iPtr = sampleGrabberCallBack.getFrame();
        Bitmap bitmapOfFrame = new Bitmap(sampleGrabberCallBack.width, sampleGrabberCallBack.height, sampleGrabberCallBack.capturePitch, PixelFormat.Format24bppRgb, iPtr);
        bitmapOfFrame.RotateFlip(RotateFlipType.RotateNoneFlipY);
        barcodeReader(bitmapOfFrame);
}

public IntPtr getFrame()
    {
        int hr;

        try
        {
            pictureReady.Reset();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }
        imageBuffer = Marshal.AllocCoTaskMem(Math.Abs(pitch) * videoHeight);

        try
        {
            gotFrame = true;

            if (videoControl != null)
            {
                hr = videoControl.SetMode(stillPin, VideoControlFlags.Trigger);
                DsError.ThrowExceptionForHR(hr);
            }

            if (!pictureReady.WaitOne(9000, false))
            {
                throw new Exception("Timeout waiting to get picture");
            }

        }
        catch
        {
            Marshal.FreeCoTaskMem(imageBuffer);
            imageBuffer = IntPtr.Zero;
        }

        return imageBuffer;

    }

我还需要按下按钮来运行 BufferCB

感谢阅读。

最佳答案

BufferCB 为相机捕获的每个新帧调用。您看不到消息框,因为该方法是从另一个线程(不是 ui 线程)调用的。参见 this question了解详情。

在我的代码中,我使用 AutoResetEvent 来捕获帧:

#region samplegrabber
/// <summary>
///   buffer callback, COULD BE FROM FOREIGN THREAD.
/// </summary>
int ISampleGrabberCB.BufferCB (double sampleTime,
                              IntPtr pBuffer,
                              int bufferLen)
{
  if (_sampleRequest)
  {
    _sampleRequest = false;

    if (bufferLen > _bufferSize)
      throw new Exception ("buffer is wrong size");

    Win32.CopyMemory (_buffer, pBuffer, _bufferSize);

    // Picture is ready.
    _resetEvent.Set ();
  }
  else
    _dropped++;
  return 0;
}

然后可以使用另一个函数从 IntPtr 解码图像:

public Image Decode (IntPtr data)
{
  var bitmap = new Bitmap (_width, _height, _stride, PixelFormat.Format24bppRgb, data);

  bitmap.RotateFlip (RotateFlipType.RotateNoneFlipY);

  return bitmap;
}

关于c# - 如何将 ISampleGrabber::BufferCB 的缓冲区转换为位图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19622345/

相关文章:

c# - 具有继承类的属性列表的 PropertyGrid 编辑器

c# - 如何使用 jquery 和 Web API 上传文件

c# - 使用 Open Xml 写入 Excel 在某些列之后出现问题

image-processing - 用激光射击毛毛虫的神经网络和图像处理

c# - 为什么这个 AJAX 调用返回整个页面内容?

matlab - 在matlab中,如何在图像上绘制网格

c#-4.0 - 查找直方图的局部最大值/峰值和最小值/谷值

android - 将图层列表转换为位图可能不起作用

c++ - 一种在一维数组(位图)内迭代矩形区域的算法

Android - 将位图快速传递给 Activity 的好方法