Webcam video program in C for Windows - c

I would like to code an app that captures a video from a webcam connected to the PC. I have found tutorials in other webs but they were in C++ or C# and I'm interested in doing it using C.
Do you know some web or have some knowledge that could help me with it?
I imagine that I would have to start my code "asking permission" to the SO to allow me to connect to the webcam but.... I have no idea how to do it neither how to continue.

I've noted your tag that you are on Windows, so this c library video4linux would not work for you.
However, in many cases OpenCV(in C/C++) will do it just fine. A lot parts of OpenCV are pure C, in which "Video Analysis" part might suits part of your need. Find this: http://opencv.willowgarage.com/documentation/c/
If you have to call the C++ part of OpenCV, you can write a c wrapper for the C++ part, while your remaining code could stay pure C. Reference this: Developing C wrapper API for Object-Oriented C++ code

Capturing video input is very system dependent, as different systems handles it in different ways. You can use a library that is doing the hard work for you (either system independently or not) or you could write your own library to do what you want.
If you want a system independent solution you can take a look at OpenCV

Related

How can i write GNURadio applications in C?

I have a project wherein i need to write an application for an USRP device. But the gnuradio software which i use to interact with the device driver and ultimately the hardware provides apis in c++ and python. I am comfortable programming in c and thus would like a way which would let me to call the apis from my c program.Is there a way in which i could do so? It would be a lifesaver.
C++ adds a lot to C, but doesn't take much away. Except for a few corner cases (which can be worked around) most valid C code is also valid C++ code, so you won't encounter many problems when you compile it as C++. Just write your whole project in a C style and only use C++ features where they are necessary to interface with the C++ API of gnuradio.

Font Metrics in C

I wanted to know if there is any library in C available for calculating the Font Metrics (Basically i wanted to know the width of a string of Particular Font).
QT has QFontMetrics. Is there any way I can get similar data in C.
I wouldn't say just use FreeType, unless you are on a system that uses X as the graphics display. If you are on Windows, use the Windows API to get font metrics information and on Mac use whatever Cocoa provides.
It might also help if you told us what you are trying to do.
EDIT: Since the output of your library is intended to be consumed by a particular GUI app, you will probably want to use the same GUI library to get the font metrics information as the app is using. Even better would be to have the app provide metrics information to the library, or a callback method that can provide that information. Then the library doesn't even have to know how the font metrics were derived, reducing an unnecessary dependency. This also means that you can use Qt (C++) in your GUI app, but still write your library in C and not have to figure out a way to call C++ from C, which is very difficult, especially if you are trying to make it cross-platform.
C is (just) a programming language. By design C has no embedded functions at all, not even for File I/O.
So you will have to indicate what Graphics/GUI platform you are using.
You can use freetype2 : http://www.freetype.org/freetype2/docs/reference/ft2-base_interface.html#FT_Glyph_Metrics
Check out FreeType: http://freetype.sourceforge.net/freetype2/docs/tutorial/step2.html

Is C good for any projects beyond the command-line and learning?

This is not meant to be inflammatory or anything like that, but I am in the midst of learning C, and (think) I have a good handle on most of the basics. I've done all of the various book exercises: primes generators, Fibonacci generators, string manipulation, yadda yadda, but none of this is cool.
What is the "bridge" between command line programs and something -cool-? I've heard of various games being written in C, but how?
Forgive my exasperation, but it feels like I've been learning lots but can still only do relatively little. Thanks for any insight on what to do with C.
Relevant information: OS X leopard, PHP and web development experience (which is so great because projects are immediately in a context where you recognize how they can be powerful)
C is the concrete and the steel of modern tech
There was a time when almost everything was written in C, or in something much worse.
These days, many of the advanced languages and systems are actually implemented in C or C++, and then those things implement more systems. It is standing on the shoulders of giants, as the expression goes. Almost every OS kernel, browser, and heavy-duty-web-server is written in C/C++.
So sure, you don't see the steel in the high rise, you see the beautiful interior furnishings and the sleek glass windows. You don't want a steel or concrete desk, and if you did, it would be too expensive to build for you.
Back to your GUI question: your first C graphics program should probably use the original X Window System directly. Don't spend too much time there, but then move on to one of the more advanced Widget toolkits such as GTK+ or (the C++) Qt. Be sure to investigate your OS X system, as it has one of the most advanced of them all.
I try love to write things in Ruby these days, but I happen to know there are over 100,000 200,000 lines of C code implementing that cool Ruby language system. :-)
Summary
Ok, this post got really big, so here's a quick summary before you read it: to program GUIs, there are a number of good C/C++ libraries (for example, QT). What most people do, on Windows systems at least, is to use a .NET language (like C#) when they want GUIs, and C/C++ when they want more control/speed. It's also very common to use both in combination, i.e. make a GUI in C# and speed-critical parts in C.
I still encourage you to read the longer answer, it contains a lot more information on your options.
Longer answer
I'll start with the big question, then answer (as best I can) your specific question about creating a GUI. I think you're kind of suffering from the fact that C is used to teach programming, and it's much easier to do so only using command line programs (after all, they're much simpler to write). This doesn't mean that C can't do all of the stuff you want, like GUIs specifically. It does. I don't think there's any type of software that hasn't been done in C, usually before it was done in other languages.
All right, some answers:
Is C Useful?
C, and its very close relative C++, are responsible for a huge portion of the world's code. I don't know if more code is written in C than any other language, but I'm guessing it's not far off.
Most of the really important programs you use are actually written in C/C++. Just for one example, Windows.
Where is C used today?
C/C++ are still used a lot. They're especially useful for developing low-level stuff (i.e. stuff like Operating Systems, which need a lot of speed, a lot of ability to control exactly what your code does, etc.).
But don't think it's all low level for C programmers. Even today, with many other languages available (which are arguably much better, and certainly much easier to program), C is still used to create practically everything. GUI applications, which you specifically asked about, are very often made with C, even though nowadays, lots of people are switching to other languages. Note I say switching: C used to be the standard language for writing, well, everything, really.
How do I develop GUIs with C
Alright, you specifically wanted to know how to create a GUI with C (I'm hoping C++ is ok too).
First of all, it depends on a number of factors:
What Operating System are you writing for? (Windows, Mac, and Linux are the most common).
Do you want the GUI to work on other systems as well?
The most common case is writing software to work on Windows. In that case, the "natural" solution is to write things that work with the Win32 API. That's basically the library that "comes" with Windows, letting you do any GUI work you want to do.
The big problem with this is, it's kinda hard. As in, a lot hard. This is the reason most people don't do that kind of work anymore.
So what are your other options?
The most natural is going with what's called a .NET language. Those are a bunch of languages, together with libraries, that Microsoft created. They're probably the easiest way to get a GUI on Windows. The problem is, you can't really use them from C (since it's not a .NET language).
Assuming you want to stay in C/C++ land, you can use some kind of library which makes working with the Windows API easier (since it hides all the ugly details):
One of the most common is what's called MFC (Microsoft Foundation Classes), which are a bunch of C++ classes which make it "easier" to create Windows stuff. Unfortunately, this library is very old, and is really not that easy to use.
The other way to go if you want to program in C++ is to use some kind of third-party library. This is a library that someone other than Microsoft created, which makes it easier to create a GUI.
Another option is to create only the GUI part of your software in a .NET language, and use C/C++ for the other parts (or use the .NET language to do almost everything, and use C/C++ only when you really need it, for example when you need things to go really fast). This is a very popular option.
The advantage of a third party library is that, if you pick the right one, you can use the same code to create a GUI for all the Operating Systems at the same time.
I think the most famous of these libraries is QT, and also WxWidgets, but there are a bunch of other ones. I would personally pick QT since it seems to get the most fame, but I haven't worked with either.
Every major operating system has all of its low-level libraries implemented in C. Mac OS X is a Unix-like system under the hood, which is a wonderful world if you're a C programmer.
Check out The Art of Unix Programming for some great ideas.
As for GUI stuff, you'll probably want to use X11. There is plenty of good documentation out there -- most Unix programming stuff and most deep system-level stuff just assumes you're working in C, since that's what everybody uses for it.
Well, that depends. If you want to build desktop applications, a multiplatform GUI library whose main language is C is GTK+:
http://www.gtk.org/
For games, check out SDL:
http://www.libsdl.org/
Which provides you with thinks like direct input from keyboard, 2D graphics, some audio and even threads and stuff like that. It can also open an OpenGL context if you want to get into the 3D world (however it's hard to do it in raw OpenGl). Did I mention that SDL is multiplatform?
However the real strength of C is in systems programming. For desktop applications/games maybe you are best suited with C++. Now that you have command of C, learning the basic C++ should be easy ;).
Cool stuff do with C?
Operating Systems, device drivers, and python modules for starters.
Games typically will use C++ if they're C-Based, in my experience / what I've seen.
There are many libraries for C under Unix, such as X lib, which accesses X11.
If you wanted to get into robotics you may find C to be very useful, as you will have to write low-level code with very small memory footprints, so even C++ may not be the best choice.
C and C++ are very good at writing small, fast code, but OOP is not always the best choice, so at times you will find that C is a better choice, for example if you are writing a compiler or OS.
Sure there are some impressive programs made in C !
GNOME for example, arguably the most used desktop environment used in modern unix systems is written in C (the major parts at least) and is mostly based on GTK+ gui toolkit, itself done in C.
For game, OpenGL is a C api and is the standard for 3D graphic programming in multi-platform development (not uniquely microsoft platform), and Quake 3, which the engine, Id Tech 3, is available in GPL, is also writen in C. There also is many 2D games written using SDL library.
SDL is a good library for graphics and sound, and I've seen some cool stuff done with it. If you do it in C, it'll take longer to make, but from a performance point of view, it'll be much better.
If your idea of cool is GUI apps and you want to write native GUI apps on the Mac, you'll want to look at
Carbon. This is the official C API into the Mac GUI and OS. They keep threatening to kill it, but it survives.
Personally I think GUI apps are a very narrow definition of cool. What I think is cool is implementing parallelized math algorithms using opencl.
GTK-server is REALLY easy to get started with, in C or any other language. Just click that link.
For a "cool" application that goes beyond simple GUI's, check out the OpenCV computer vision library. It provides fast real-time image processing and face recognition.
Now you can access a webcam and start writing real-time computer vision games. For applications like these that are processor intensive, C is the ideal choice.
Last I checked more Open Source projects are started in C than in any other language.
The fact that C is used by so many large and successful projects doesn't particularly make it "good". The reason C is so commonly used is because of a few factors, it's been around a long time, it's fast, it lets you access both low level and high level interfaces as needed, and it's better than the other old languages (FORTRAN etc). The "cool" thing about C is that you can make it do absolutely anything: inject itself into the kernel and add some new features or bug fixes that you couldn't convince Microsoft to do, etc.
Yes, C can be and is easily used for things beyond the command line, but it's extremely dangerous due to pointers... Not to mention development in other more modern languages is faster (and safer) by magnitudes. I never use C unless it's the last resort, ie: need to implement something low level or needs that extra performance.
By the way, when I say C, I really mean C++. I'd never choose C over C++ unless I was forced to.

Approaches to a GUI for a Large C Program

In our Bioinformatics lab we've recently been asked to create a GUI for a program written (and optimized) in C. Any GUI we designed would need to be able to feed input to and receive output from the C program, while also being easily portable to both Windows and Mac. What are ways to go about doing this?
If your looking for a GUI toolkit that works on windows/mac in C, have you considered GTK?
Download QT or wxWidgets.
Why do the hard work when someone already has and will let you use it for free? I prefer QT myself, btw.
Edit: It does mean using C++ but it will work perfectly with C code.
Use a separate program.
It doesn't break the existing code - especially important if the code has bio knowledge that a programmer might not know/understand/test.
Gui's change often, in a couple of years you are going to be rewriting the gui for NewSuperOsToolkit(tm) but the underlying worker code won't change. We have atmospheric modelling code that I'm sure was originally written in latin.
You keep the ability to run the engine code as a batch, in parallel on MPI in the cloud, and a bunch of other ways you haven't thought of.
You could write the front end GUIs in Java, and have them feed input and receive output from your C programs. I've known a couple of groups here at work that do something similar with C# and C code (but they don't have the multi-platform restraint).
This way you don't necessarily have to create your GUIs using a C toolkit.
Depending on the nature of the program, you could create an entirely separate GUI application (in any language you prefer), and fork/execute the existing program from it, redirecting it's stdin and stdout to your GUI program. Depending on how the existing program works, this could work well, or be very cumbersome.
You could also extend the existing program with GUI code in something like GTK (which has a C API), or you could use Qt (which is C++, but there's usually no problems calling C functions from C++ if you define them as 'extern "C"').
There are a number of scripting with languages that can make this easy. I'd go for tcl/tk it works on mac pretty much out of the box, and is cross platform across pretty much any machine you can think of.
Also GTK is great and a number of scripting languages have bindings for it.
You probably don't want to write your gui in c if you don't have to. use a rapid develoment scripting language.
If you want a native GUI, then go with wxWidgets or QT. (I use wxWidgets ...)
The alternative is to have an HTML based interface using Javascript and CSS. You avoid GUI libraries altogether, you get cross-platform support and the outcome is better on some dimensions, worse on others.
If you really want a standalone user interface, you can integrate it into a binary with something like WebKit.

OS independent clipboard copy/paste text in C

I'm working on a project that's supposed to work on both Windows and Linux (with an unofficial Mac port as well) that emulates a true colour system console.
My problem is that recently there appeared a request for textfield support (yes, console-based) and it would be cool to add the possibility of copying text to clipboard and pasting from it. Is there a way of achieving this that will:
be done in C (not C++),
work in both Windows and in Linux (preprocessor macros are an option if there's no platform-independent code),
require no extra libraries to link to?
Thanks in advance for your help.
If you're not using a cross platform UI library (like wx or something), then it sounds like you're just going to have to write native clipboard code for each platform you want to support.
Remember, on Macintoshes, you copy with Command-C, not Ctrl+C :)
The clipboard is inherently an operating system defined concept. The C language itself has no knowledge of what a clipboard is or how to operate on it. You must either interface directly with the OS, or use a portability library that does this on your behalf. There is no way around this.
Personally I would define my your own function
getClipboardText();
That is defined in two different header files (linux_clipboard.h, windows_clipboard.h, etc) and then do pre-proccessor stuff to load the appropriate one accordingly. I don't really code in C/C++ so I'm sorry if that didn't make any sense or is bad practice but that's how I'd go about doing this.
#if WIN32
#include windows_clipboard.h
#endif
That sort of thing
Remember:
For linux you have to deal with different window managers (Gnome, KDE) all with different ways of managing the clipboard. Keep this in mind when designing your app.
You may be able to communicate to the clipboard by using xclip. You can use this python script here to do this job via communicating with 'dcop' and 'klipper' here. That is for KDE, I do not know how it would be done under GNOME... You may also be able to do this independantly of either GNOME/KDE by using DBUS, although I cannot say 100% confidently on that either...
Just be aware, that for a truly cross-platform job, you have to take into account of the different GUI's such as under Linux, X is the main window manager interface and either GNOME/KDE sits on top of it..I am not singling out other GUI's such as FluxBox, WindowMaker to name but a few, and that there will be a lot of platform dependant code, and also in conjunction, you will be dealing with Windows clipboard as well..all in all, a big integrated code...
Have you not considered looking at the raw X programming API for clipboard support? Maybe that might be better as I would imagine, GNOME/KDE etc are using the X's API to do the clipboard work...if that is confirmed, then the work would be cut out and be independant of the major GUI interfaces...(I hope that would be the case as it would make life easier for your project!)
Perhaps using compile-time switches, for each platform...WIN, KDE, GNOME, MAC or use the one that is already pre-defined..
Hope this helps,
Best regards,
Tom.

Resources