Comments (11)
Hello, I read your issue translated by DeepL.
If I am making video playback application, your opinion - create single WriteableBitmap and redraw every frame updated - is good solution.
But this library is for USB camera frame capture application, I think my approch is reasonable.
For instance, when you execute GetBitmap() two times with some interval, first image must be different from second image.
However, if I implement your approach, I would get the same image.
If you have a different opinion, please let me know.
from usbcamera.
暴露 获取byte[]的方法
相机启动的时候通过 GetBitmap() 获取 WriteableBitmap
之后定时通过 GetBuffer() 获取帧byte[]
使用 UpdataWritableBitmap 更新 writableBitmap
这样相较在 wpf 上使用 camera.SetPreviewControl(handle, new Size(320, 240)) 的好处是 不用嵌入winform控件 对异形画面更友好 ,性能也比每次通过GetBitmap更新画面更好 也没有多余的GC浪费
public static void UpdataWritableBitmap(byte[] imgBuffer, ref WriteableBitmap writableBitmap)
{
if (imgBuffer != null
&& imgBuffer.Length > 0
&& writableBitmap != null)
{
writableBitmap.Lock();
Marshal.Copy(imgBuffer, 0, writableBitmap.BackBuffer, imgBuffer.Length);
writableBitmap.AddDirtyRect(new System.Windows.Int32Rect(0, 0, writableBitmap.PixelWidth, writableBitmap.PixelHeight));
writableBitmap.Unlock();
}
}
我的建议是 暴露 获取byte[]的方法 由使用的人判断在wpf程序中是使用 UpdataWritableBitmap 还是 SetPreviewControl 来播放实时画面
毕竟嵌入winform控件的方式会导致很多wpf的效果没法实现
from usbcamera.
Thank you for your response.
I'm now understand that your issue's purpose is to improve a preview under WPF.
Using SetPreviewControl requires WinForm control and it can not use WPF's merit.
Using GetBitmap requires a lot of GC and less performance.
I usually do not use WPF, so I didn't notice the problem.
I will consider how to deal with the problem.
from usbcamera.
This is my working note.
target app: SampleProject/UsbCameraWpf
conclution:
- SetPreviewControl uses less memory and less GC.
- It seems no difference of CPU performance.
from usbcamera.
This is my working note.
There are 2 lines that allocate memory.
- var result = new WriteableBitmap(width, height, dpi, dpi, PixelFormats.Bgr24, null);
- var pixels = new byte[lenght];
private static BitmapSource BufferToBitmap(byte[] buffer, int width, int height, int stride)
{
const double dpi = 96.0;
var result = new WriteableBitmap(width, height, dpi, dpi, PixelFormats.Bgr24, null); --- (1)
var lenght = height * stride;
var pixels = new byte[lenght]; --- (2)
// copy from last row.
for (int y = 0; y < height; y++)
{
var src_idx = buffer.Length - (stride * (y + 1));
Buffer.BlockCopy(buffer, src_idx, pixels, stride * y, stride);
}
result.WritePixels(new Int32Rect(0, 0, width, height), pixels, stride, 0);
// if no Freeze(), StillImageCaptured image is not displayed in WPF.
result.Freeze();
return result;
}
from usbcamera.
This is my working note.
as to No.2,
If I change below, (do not allocate memory everytime).
private static byte[] pixels = null;
private static BitmapSource BufferToBitmap(byte[] buffer, int width, int height, int stride)
{
const double dpi = 96.0;
var result = new WriteableBitmap(width, height, dpi, dpi, PixelFormats.Bgr24, null);
var lenght = height * stride;
if (pixels == null) pixels = new byte[lenght];
// copy from last row.
for (int y = 0; y < height; y++)
{
var src_idx = buffer.Length - (stride * (y + 1));
Buffer.BlockCopy(buffer, src_idx, pixels, stride * y, stride);
}
result.WritePixels(new Int32Rect(0, 0, width, height), pixels, stride, 0);
// if no Freeze(), StillImageCaptured image is not displayed in WPF.
result.Freeze();
return result;
}
- less GC.
- Process Memory increased. (I think this is result of less GC, so we dont care about it.)
conclution:
This works well.
from usbcamera.
This is my working note.
as to No.1,
This didn't go very well.
The biggest problem is, I do not know how to create/update WriteableBitmap in another thread.
If I create WriteableBitmap in non UI thread, exception happen.
for my reference:
https://stackoverflow.com/questions/9868929/how-to-edit-a-writablebitmap-backbuffer-in-non-ui-thread
from usbcamera.
It took a lot of time, but I released fix that do not allocate memory every time.
In result of that, less GC. please see picture in my last post.
Yom may expect less GC level same with SetPreviewControl,
there are some difficulties. It may not be possible.
The biggest problem is, it is not able to create WriteableBitmap instancde in Non UI thread.
WritetableBitmap must be created in UI thread, and before/after draw it, I have to lock/unlock properly.
from usbcamera.
This is my working note.
I finally find a good solution and will release in a few days.
from usbcamera.
I released fix of this issue.
10c6ca4
from usbcamera.
首先,抱歉,我现在刚想到一个注意事项,
(First of all, I'm sorry, I just thought of a note)
在某些旧型号电脑上
(On some older computers)
特别是一些使用集成显卡的十几年前的电脑
(Especially some computers that used integrated graphics cards more than ten years ago)
WriteableBitmap 似乎无法渲染画面
(WriteableBitmap seems unable to render the picture)
有可能是显卡驱动的原因使得个别非常古老的电脑对WriteableBitmap支持似乎并不是很好
(It may be due to the graphics card driver that some very old computers do not seem to support WriteableBitmap very well)
所以需要保留每帧获取新的bitmap给到UI的可能
(So we need to keep the possibility of getting a new bitmap to the UI every frame)
非常抱歉我并没有查看您的代码是否有相关的考虑
(I'm sorry I didn't check your code )
我只是正好想到了这个问题
(I just happened to think of this note)
from usbcamera.
Related Issues (20)
- Release taking long time sometimes HOT 1
- How can I get the datas of every frame? HOT 9
- missing events HOT 4
- How can I switch camera? HOT 2
- provide nuget package HOT 4
- Implement IDisposable / IObservable pattern HOT 1
- 需要增加对于部分非标的虚拟摄像头的过滤(It is necessary to increase the filtering of some non-standard virtual cameras) HOT 7
- Problem with showing preview HOT 4
- Memory issues HOT 13
- How to distinguish the camera with the same name? HOT 4
- Add a feature to get plain image data as byte array to not call lock bits over and over again HOT 15
- threads in thread pool may be blocked by this library HOT 4
- Inconsistent Image Quality When Saving Still Capture Monochrome Sensor Data as BMP HOT 8
- Can't able to set brightness and exposure at run time when camera running on different thread HOT 2
- Capture Pin not working on all cameras HOT 3
- happens ArgumentException when I dispose PictureBox's old image. HOT 1
- How can I turn off the usbcamera lights? HOT 9
- why not working in 4K? HOT 6
- Add an IsReady method to see if we can get the image? HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from usbcamera.