GithubHelp home page GithubHelp logo

secile / usbcamera Goto Github PK

View Code? Open in Web Editor NEW
167.0 167.0 54.0 401 KB

C# source code for using usb camera and web camera in WinForms/WPF. With only single CSharp source code. No external library required.

License: MIT License

C# 100.00%

usbcamera's People

Contributors

secile avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

usbcamera's Issues

threads in thread pool may be blocked by this library

Thank you for your creative work, it's really great!

When I read the source code, I found that when SampleGrabberCallback uses the system thread pool for processing, all available threads in the thread pool (other threads not actually used by the library) will be blocked by BufferedEvent, and other modules using the system thread pool in the same application will be restricted. As a library that may be referenced by many programs, I think this approach should be avoided in design. It may be better to use a thread that implements a simple event queue to do this.
if my understanding of this program is correct?

            public SampleGrabberCallback(DirectShow.ISampleGrabber grabber, int width, int height, int stride, bool useCache)
            {
                this.BmpBuilder = new BitmapBuilder(width, height, stride, useCache);

                // create Buffered.Invoke thread.
                System.Threading.ThreadPool.QueueUserWorkItem(x =>
                {
                    while (true)
                    {
                        BufferedEvent.WaitOne(); // wait event.
                        Buffered?.Invoke(GetBitmap()); // fire!
                    }
                });

                grabber.SetCallback(this, 1); // WhichMethodToCallback = BufferCB
            }

Release taking long time sometimes

Hello @secile,
The Release is taking long time intermittently, I'm not sure why... tracking the call stack, it hangs on this line
case FILTER_STATE.Stopped: mediaControl.Stop(); break;

Any idea what could be the reason?

Steps to reproduce:

  1. Start the camera
  2. Release the camera
  3. Keep repeating steps 1 to 2, it will eventually happen....

Tested with my laptop integrated camera

On Capture Callback

Hello @secile,
What you have here is gold. Thank you for putting it together.

Is it possible to pass a callback function on capture? This is essential for cameras which have an external capture button. We need to have a way to detect it and either manually trigger a GetBitmap or the event will take one and pass it to the callback function.

Problem with showing preview

First of all, thank you for this. This is realy great.

I'm trying to develop wpf app with this and i was testing with 3 different cameras (Logitech brio, one from lg and razer kiyo).
Lg and Razer are working fine with no problem, but i cant get it to work with brio and i dont know why.
I tried it with all possible video formats that i get from GetVideoFormat() method.

Problem appears here, when it tries to subscribe to event:
image

image

Message of exception here is: "Value does not fall within the expected range."
image

Any idea, what could be or can be done? If you need any more info, let me know. Thanks.

Can screen tearing be avoided

Do you happen to know if anything can be done to avoid screen tearing while capturing at higher resolutions?

For instance, if I capture at 2592 x 1944 (~5 MP), I get this kind of artifacts:

capture_tearing

Seemingly there's not much that can be done within the class (method GetBitmapMainMain()). This is how the local data buffer is copied from the unmanaged DirectShow buffer - already teared. Any settings for DirectShow maybe, or is there not much hope?

Memory issues

Hello, when using an external video capture card, you may notice that the memory usage continues to increase, ultimately causing the program to crash. Is there a button for real-time release

Would it recognize my DSLR camera via USB port on Windows 10?

Hello,
I don't know if I am asking right question..

But for last few days, I have been looking for a way to write a device driver that can recognize connected cameras on computer/laptops via USB port, as a video input source on streaming software like OSB.

I just want to know how your code works and if there is a way to create such device driver?

Thanks in Advance..

UsbCamera

I am using your work with satisfaction and I would like to know, since some cameras have a serial code, would it be possible to read it?

Camera preview locked

Using a The Imaging Source USB 3.1 1440 x 1080 px camera sometimes, especially at the beginning, the preview freezes and you have to do Camera.Stop() + Camera.Start() to make it restart. What could it be?

需要增加对于部分非标的虚拟摄像头的过滤(It is necessary to increase the filtering of some non-standard virtual cameras)

  1. 下载并安装 非标的虚拟摄像头(例如 YY开播)
    (Download and install non-standard cameras such as 'YY开播')
    YY开播

  2. FindDevices 成功获取到 ‘YY开播’
    (find 'YY开播’ success by FindDevices )
    image

  3. GetVideoFormat 时报错
    (error on GetVideoFormat )
    image

造成这个的原因是 ‘YY开播’ 注册了一个信息不完整的USB摄像头
(The reason for this is that a USB camera with incomplete information has been registered by 'YY开播')

问题是我们没办法限制用户不要安装什么软件
(The problem is that we can't restrict users from installing any software)

我发现在 GetFiltes 时 'YY开播' 无法获取到 DevicePath
(I found that 'YY开播' could not get DevicePath when GetFiltes)
image

所以 尝试获取 DevicePath 或许是一个过滤非标虚拟摄像头的方式
(So trying to get DevicePath may be a way to filter non-standard virtual cameras)


我发现 FlashCap 并没有这个问题,他直接就过滤了非标的虚拟USB摄像头信息
(I found that FlashCap did not have this problem. It directly filtered the non-standard virtual USB camera information)

DirectShowDevices.cs

Camera preset

Hi,
How i call/set preset from/to cam?
Example
tnx

It is better do not enter critical section in callback function.

take over from issue #13

I found that it is necessary to return quickly in callback function.
if callback does not return quickly, it can interfere with playback.
https://docs.microsoft.com/en-us/windows/win32/directshow/isamplegrabber-setcallback

This is previous version document, it is prohibited to enter critical section.
https://docs.microsoft.com/en-us/previous-versions/ms786692(v=vs.85)

It is better do not enter critical section and do not wait another thread.

public int BufferCB(double SampleTime, IntPtr pBuffer, int BufferLen)
{
    if (Buffer == null || Buffer.Length != BufferLen)
    {
        Buffer = new byte[BufferLen];
    }

    var locked = false;
    try
    {
        System.Threading.Monitor.TryEnter(BufferLock, 0, ref locked);
        if (locked)
        {
            Marshal.Copy(pBuffer, Buffer, 0, BufferLen);
        }
    }
    finally
    {
        if (locked) System.Threading.Monitor.Exit(BufferLock);
    }
    return 0;
}

missing events

           
// create instance.
camera = new UsbCamera(cameraIndex, format);

// show preview on control. (works light.)
camera.SetPreviewControl(this.panVideo.Handle, this.panVideo.ClientSize);

private void panVideo_MouseMove(object sender, MouseEventArgs e)
 {
       //it never fired over the camera region.but fired over other whitespace region.
       Console.WriteLine("mouse move");
}

How to distinguish the camera with the same name?

Your code is very good.
But I have confirmed some problems.

Currently implemented code is to get a camera list, select a camera (select an index value), and connect.
But What I want is to save the name/ID of the camera and extract the matching index value.
and than I want to connect after extracting the index value. (Automatic connection)
(The index of each camera is changed every time because the camera can be changed, broken, or newly added.
It cannot be used as a specification because the camera cannot be selected every time)

At first,
I tried to use the camera name and [VideoFormat] as specifications
(Find the camera name and the [VideoFormat] Find the index value)
However, when using the cameras of the same product, Since the name is the same, the method cannot be used.

I want an ID like Aforge.video.directShow -Filtermoniker,
In the Videoo format [] GetVideoFormat (int Cameraidex) function, m_symboliclik is identified as Aforge.video.directShow -Filtermoniker.
but it could not be used becuase it is IUNKNOWN type

Is it possible to add m_symboliclink value to the [VideoFormat] for distinction between cameras?
Can you tell me if there is a better way?

Thank you very much

How to black out the camera system wide?

Hello,

I want to black out the camera. I had decent success with setting the brightness to the min value but this seems to be not working for every camera model. Can I apply a DirectShow Filter without "starting" the camera (I cannot start it if another application is already using it)? If so: What DirectShow filter should be used to make the camera stream go black?

Do you have any other suggestions?

Thanks in advance

Jannes

Memory leak

Hi, I am testing your code and I noticed that every time you take a snapshot the memory increases. Does it happen to you too?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.