Coder Social home page Coder Social logo

775083111 / usbcamera Goto Github PK

View Code? Open in Web Editor NEW

This project forked from secile/usbcamera

0.0 0.0 0.0 174 KB

C# source code for using USB camera in WinForms. With only single CSharp source code. No external library required.

License: MIT License

C# 100.00%

usbcamera's Introduction

UsbCamera

C# source code for using USB camera in WinForms.
With only single CSharp source code. No external library required.

How to use

Add UsbCamera.cs to your project.

// [How to use]
// check USB camera is available.
string[] devices = UsbCamera.FindDevices();
if (devices.Length == 0) return; // no camera.
            
// check format.
int cameraIndex = 0;
UsbCamera.VideoFormat[] formats = UsbCamera.GetVideoFormat(cameraIndex);
for(int i=0; i<formats.Length; i++) Console.WriteLine("{0}:{1}", i, formats[i]);
            
// create usb camera and start.
var camera = new UsbCamera(cameraIndex, formats[0]);
camera.Start();
            
// get image.
// Immediately after starting the USB camera,
// GetBitmap() fails because image buffer is not prepared yet.
var bmp = camera.GetBitmap();
            
// show image in PictureBox.
var timer = new System.Timers.Timer(100) { SynchronizingObject = this };
timer.Elapsed += (s, ev) => pbxScreen.Image = camera.GetBitmap();
timer.Start();

// release resource when close.
this.FormClosing += (s, ev) => timer.Stop();
this.FormClosing += (s, ev) => camera.Stop();

Adjust Tilt, Zoom, Exposure, Brightness, Contrast, etc...

// adjust properties.
UsbCamera.PropertyItems.Property prop;
prop = camera.Properties[DirectShow.CameraControlProperty.Exposure];
if (prop.Available)
{
    var min = prop.Min;
    var max = prop.Max;
    var def = prop.Default;
    var step = prop.Step;
    prop.SetValue(DirectShow.CameraControlFlags.Manual, prop.Default);
}

prop = camera.Properties[DirectShow.VideoProcAmpProperty.WhiteBalance];
if (prop.Available && prop.CanAuto)
{
    prop.SetValue(DirectShow.CameraControlFlags.Auto, 0);
}

No external library required.

Previos version, this project used external library. But now, no need to add external library to your project.

Special Thanks.

This project make use of a part of source code of the project below. Thank you!

And...

You can make H264 recorder by using UsbCamera and OpenH264Lib.NET and AviWriter(in MotionJPEGWriter).
See README.md in OpenH264.NET.

usbcamera's People

Contributors

secile avatar

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.