目前我已经使用C ++ CLR为c#构建了包装器。 C ++ clr类从视频摄像机获取帧作为uint8 []并返回到c ++类中的OnVideoFrame事件。我有这样的初始化器:
ZeroMemory(&bmi_, sizeof(bmi_));
bmi_.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmi_.bmiHeader.biPlanes = 1;
bmi_.bmiHeader.biBitCount = 32;
bmi_.bmiHeader.biCompression = BI_RGB;
bmi_.bmiHeader.biWidth = width;
bmi_.bmiHeader.biHeight = -height;
bmi_.bmiHeader.biSizeImage = width * height * (bmi_.bmiHeader.biBitCount >> 3);
image_.reset(new uint8[bmi_.bmiHeader.biSizeImage]);
所以问题是:
我已经在image_中有数据,但是如何将该数据发送到c#并将其转换为任何WPF位图对象以播放我的本地视频流?我正在寻找有关性能和我使用WPF的问题的最佳方法。我已经有了基本的win32 app的解决方案:
RECT rcClient;
GetClientRect(VideoRendererInternal->WindowHandle, &rcClient);
PAINTSTRUCT ps;
HDC hdc = BeginPaint(VideoRendererInternal->WindowHandle, &ps);
StretchDIBits(hdc,
0, 0, rcClient.right, rcClient.bottom, // destination rect
0, 0, VideoRendererInternal->bmi_.bmiHeader.biWidth, -VideoRendererInternal->bmi_.bmiHeader.biHeight, // source rect
VideoRendererInternal->image_.get(), &VideoRendererInternal->bmi_, DIB_RGB_COLORS, SRCCOPY);
EndPaint(VideoRendererInternal->WindowHandle, &ps);
但找不到WPF的任何解决方案。谢谢!
答案 0 :(得分:1)
有很多方法可以做到这一点,我建议你看看https://channel9.msdn.com/Events/Build/2015/3-82他们在哪里做到这一点。这是使用WPF从c ++显示图像的另一种解决方案。
使用MVVM,您需要XAML中的Image,您可以使用任何位图源进行设置。这样做WPF会重绘图像。这是在引擎盖下高度优化(在4k @ 60fps下测试)。
<Image x:Name="MainFrameBitmap"
Height="{Binding ImageProcessingModel.MainFrameBitmap.Height}"
Width="{Binding ImageProcessingModel.MainFrameBitmap.Width}"
Source="{Binding ImageProcessingModel.MainFrameBitmap, UpdateSourceTrigger=PropertyChanged}" Cursor="Cross" >
在ImageProcessingModel中,您可能希望使用pinvoke或管道从c ++域获取字节。这里的计时器非常有用,因为您不希望阻止UI执行其操作。 WPF能够以120fps的速度进行本机渲染,因此如果帧源不是周期性的或以低帧速率运行,这将非常有用。
public class ImageProcessingModel : INotifyPropertyChanged
{
public BitmapSource MainFrameBitmap
{
get
{
return _mainFrameBitmap;
}
private set
{
_mainFrameBitmap = value;
OnPropertyChanged("MainFrameBitmap");
}
}
public ImageProcessingModel(int updatePeriodInMilliseconds)
{
dispatcherTimer = new DispatcherTimer();
dispatcherTimer.Tick += ServiceUiDispatcherTimerTick;
dispatcherTimer.Interval = new TimeSpan(0, 0, 0, 0, updatePeriodInMilliseconds);
}
private void ServiceUiDispatcherTimerTick(object sender, EventArgs e)
{
try
{
MainFrameBitmap = ReadBitmap(FrameWidth, FrameHeight, Stride);
}
catch (Exception ex)
{
Console.WriteLine(@"UI Runtime Exception:" + ex.Message);
}
}
}
您可能希望有另一个类来跟踪这些原始图像字节
byte[] _imageData
public BitmapSource ReadBitmap(int w, int h, int s )
{
int size = w*h*3 + 54;
_imageData = new byte[size];
// Read from pipe Read(_temp, 0, size);
// Or use PInvoke to get a reference and manage that
return BitmapSource.Create(w, h, 96, 96, PixelFormats.Rgb24, null, _imageData, s);
}
在c ++中,您需要为任何给定的帧构造字节,这取决于您使用的框架。下面的示例使用OpenCV,但如果使用RGBQUAD或某些此类字节缓冲区来存储图像,则代码将类似。此处选择的图像格式(和内存对齐)需要与WPF应用程序中的BitmapSource.Create函数匹配。
std::vector<uchar> buffer
DWORD UIConnection::writeToPipe(cv::Mat inputFrame){
int buffSize = (inputFrame.rows * inputFrame.cols * 3) + 54;
cv::Mat serviceImage;
inputFrame.copyTo(serviceImage);
flip(serviceImage, serviceImage, 1);
imencode(".bmp", serviceImage, buffer);
reverse(buffer.begin(), buffer.end());
WriteFile(hPipe, &buffer[0], buffSize * sizeof(uchar), bytesWritten, NULL);
return *bytesWritten;
}
答案 1 :(得分:1)
感谢您的快速回复!通过使用你的一半答案解决。不喜欢计时器,所以只是从c ++派代表发送IntPtr:
public delegate void OnNewFrame(IntPtr bitmapBuffer, int bitmapBufferLength);
...
public:
event OnNewFrame ^OnNewFrameEvent;`
然后:
System::IntPtr buffer = System::IntPtr(VideoRendererInternal->image_.get());
OnNewFrameEvent(buffer, bufferSize);
在C#方面:
...
renderer.OnNewFrameEvent += new OnNewFrame(OnNewVideoFrame);
}
private void OnNewVideoFrame(IntPtr bitmapBuffer, int bitmapBufferLength)
{
FrameDispatcher.Instance.DispatchFrame("LocalVideo", bitmapBuffer, bitmapBufferLength);
}
帧调度员类:
class FrameDispatcher
{
private static FrameDispatcher StaticInstance;
private Dictionary<String, FrameRenderer> Renderers = new Dictionary<string, FrameRenderer>();
public static FrameDispatcher Instance {
get {
if (StaticInstance == null)
{
StaticInstance = new FrameDispatcher();
}
return StaticInstance;
}
}
public void DispatchFrame(String id, IntPtr bitmapBuffer, int bitmapBufferLength)
{
if (Renderers.ContainsKey(id))
{
BitmapSource bitmap = BitmapSource.Create(640, 480, 96, 96, PixelFormats.Pbgra32, null, bitmapBuffer, bitmapBufferLength, 640 * 8);
Renderers[id].SetBitmap(bitmap);
}
}
public void RegisterFrameRenderer(FrameRenderer renderer, String id)
{
Renderers.Add(id, renderer);
}
public void UnregisterFrameRenderer(String id)
{
Renderers.Remove(id);
}
}
帧渲染器类:
class FrameRenderer : INotifyPropertyChanged
{
private BitmapSource _Bitmap;
public BitmapSource Bitmap
{
get
{
return _Bitmap;
}
private set
{
_Bitmap = value;
OnPropertyChanged("Bitmap");
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(String info)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
try
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
catch (Exception ex)
{
Console.WriteLine(@"UI Runtime Exception:" + ex.Message);
}
}
}
public void SetBitmap(BitmapSource bitmap) {
bitmap.Freeze();
Bitmap = bitmap;
GC.Collect();
}
}
在MainWindow控制器中注册FrameRenderer:
Renderer = new FrameRenderer();
FrameDispatcher.Instance.RegisterFrameRenderer(Renderer, "LocalVideo");
DataContext = Renderer;
现在它有效,再次感谢:)