|
Packit |
1470ea |
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
xmlns:its="http://www.w3.org/2005/11/its"
|
|
Packit |
1470ea |
type="topic"
|
|
Packit |
1470ea |
id="magic-mirror.vala">
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<info>
|
|
Packit |
1470ea |
<title type="text">Magic mirror (Vala)</title>
|
|
Packit |
1470ea |
<link type="guide" xref="vala#examples"/>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<desc>Use your webcam as a mirror using the GStreamer framework and GTK+</desc>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<revision pkgversion="0.1" version="0.1" date="2011-03-19" status="review"/>
|
|
Packit |
1470ea |
<credit type="author">
|
|
Packit |
1470ea |
<name>Daniel G. Siegel</name>
|
|
Packit |
1470ea |
<email its:translate="no">dgsiegel@gnome.org</email>
|
|
Packit |
1470ea |
</credit>
|
|
Packit |
1470ea |
<credit type="author">
|
|
Packit |
1470ea |
<name>Johannes Schmid</name>
|
|
Packit |
1470ea |
<email its:translate="no">jhs@gnome.org</email>
|
|
Packit |
1470ea |
</credit>
|
|
Packit |
1470ea |
<credit type="editor">
|
|
Packit |
1470ea |
<name>Marta Maria Casetti</name>
|
|
Packit |
1470ea |
<email its:translate="no">mmcasetti@gmail.com</email>
|
|
Packit |
1470ea |
<years>2013</years>
|
|
Packit |
1470ea |
</credit>
|
|
Packit |
1470ea |
</info>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<title>Magic mirror</title>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<synopsis>
|
|
Packit |
1470ea |
Your mirror just fell off the wall and broke into a thousand pieces — but you need a mirror to shave your beard off or add some makeup! You only have 15 minutes left before catching the bus to work. So what can you do?
|
|
Packit |
1470ea |
In this tutorial, we're going to make a program which lets you use your webcam as a mirror. You will learn how to:
|
|
Packit |
1470ea |
<list>
|
|
Packit |
1470ea |
<item>Create a GTK+ application </item>
|
|
Packit |
1470ea |
<item>Access your webcam using GStreamer and embed the result into a window </item>
|
|
Packit |
1470ea |
<item>Grab photos off your webcam </item>
|
|
Packit |
1470ea |
</list>
|
|
Packit |
1470ea |
You'll need the following to be able to follow this tutorial:
|
|
Packit |
1470ea |
<list>
|
|
Packit |
1470ea |
<item>An installed copy of the <link xref="getting-ready">Anjuta IDE</link> </item>
|
|
Packit |
1470ea |
<item>Installed copies of GTK, GStreamer, and a Vala compiler </item>
|
|
Packit |
1470ea |
<item>Basic knowledge of an object-oriented programming language </item>
|
|
Packit |
1470ea |
</list>
|
|
Packit |
1470ea |
</synopsis>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<media type="image" mime="image/png" src="media/magic-mirror.png"/>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<section id="anjuta">
|
|
Packit |
1470ea |
<title>Create a project in Anjuta</title>
|
|
Packit |
1470ea |
Before you start coding, you'll need to set up a new project in Anjuta. This will create all of the files you need to build and run the code later on. It's also useful for keeping everything together.
|
|
Packit |
1470ea |
<steps>
|
|
Packit |
1470ea |
<item>
|
|
Packit |
1470ea |
Start Anjuta and click <guiseq><gui>File</gui><gui>New</gui><gui>Project</gui></guiseq> to open the project wizard.
|
|
Packit |
1470ea |
</item>
|
|
Packit |
1470ea |
<item>
|
|
Packit |
1470ea |
Choose <gui>GTK+ (simple)</gui> from the <gui>Vala</gui> tab, click <gui>Forward</gui>, and fill out your details on the next few pages. Use <file>magic-mirror</file> as project name and directory.
|
|
Packit |
1470ea |
</item>
|
|
Packit |
1470ea |
<item>
|
|
Packit |
1470ea |
Disable <gui>Use GtkBuilder for user interface</gui> as we will
|
|
Packit |
1470ea |
create the UI manually in this tutorial. Check the <link xref="guitar-tuner.vala">Guitar-Tuner</link>
|
|
Packit |
1470ea |
tutorial using the interface builder.
|
|
Packit |
1470ea |
</item>
|
|
Packit |
1470ea |
<item>
|
|
Packit |
1470ea |
Make sure that <gui>Configure external packages</gui> is selected. On the next page, select
|
|
Packit |
1470ea |
gstreamer-0.10 from the list to include the <app>GStreamer</app> library into your project.
|
|
Packit |
1470ea |
</item>
|
|
Packit |
1470ea |
<item>
|
|
Packit |
1470ea |
Click <gui>Apply</gui> and the project will be created for you. Open <file>src/magic_mirror.vala</file> from the <gui>Project</gui> or <gui>File</gui> tabs. You should see some code which starts with the lines:
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
using GLib;
|
|
Packit |
1470ea |
using Gtk;]]>
|
|
Packit |
1470ea |
</item>
|
|
Packit |
1470ea |
</steps>
|
|
Packit |
1470ea |
</section>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<section id="build">
|
|
Packit |
1470ea |
<title>Build the code for the first time</title>
|
|
Packit |
1470ea |
The code loads an (empty) window and shows it. More details are given below; skip this list if you understand the basics:
|
|
Packit |
1470ea |
<list>
|
|
Packit |
1470ea |
<item>
|
|
Packit |
1470ea |
The two using lines import namespaces so we don't have to name them explicitly.
|
|
Packit |
1470ea |
</item>
|
|
Packit |
1470ea |
<item>
|
|
Packit |
1470ea |
The constructor of the Main class creates a new window and sets its title. Afterwards the window
|
|
Packit |
1470ea |
is shown and a signal is connected which quits the application if the window is closed. More on signals later on.
|
|
Packit |
1470ea |
</item>
|
|
Packit |
1470ea |
<item>
|
|
Packit |
1470ea |
The static main function is run by default when you start a Vala application. It calls a few functions which create the Main class, set up and then run the application. The Gtk.Main function starts the GTK main loop, which runs the user interface and starts listening for events (like clicks and key presses).
|
|
Packit |
1470ea |
</item>
|
|
Packit |
1470ea |
</list>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
This code is ready to be used, so you can compile it by clicking <guiseq><gui>Build</gui><gui>Build Project</gui></guiseq> (or press <keyseq><key>Shift</key><key>F7</key></keyseq>).
|
|
Packit |
1470ea |
Change the <gui>Configuration</gui> to <gui>Default</gui> and then press <gui>Execute</gui> to configure the build directory. You only need to do this once, for the first build.
|
|
Packit |
1470ea |
</section>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<section id="webcam">
|
|
Packit |
1470ea |
<title>Access the webcam video stream with GStreamer</title>
|
|
Packit |
1470ea |
The GStreamer multimedia framework is able to handle video from webcams. Let's add GStreamer to our application and so we can access the video stream.
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
using GLib;
|
|
Packit |
1470ea |
using Gtk;
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
public class Main : Object
|
|
Packit |
1470ea |
{
|
|
Packit |
1470ea |
private Gst.Element camerabin;
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
public Main () {
|
|
Packit |
1470ea |
this.camerabin = Gst.ElementFactory.make ("camerabin", "camera");
|
|
Packit |
1470ea |
this.camerabin.set_state (Gst.State.PLAYING);
|
|
Packit |
1470ea |
}
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
static int main (string[] args) {
|
|
Packit |
1470ea |
Gtk.init (ref args);
|
|
Packit |
1470ea |
Gst.init (ref args);
|
|
Packit |
1470ea |
var app = new Main ();
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
Gtk.main ();
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
return 0;
|
|
Packit |
1470ea |
}
|
|
Packit |
1470ea |
}
|
|
Packit |
1470ea |
]]>
|
|
Packit |
1470ea |
<steps>
|
|
Packit |
1470ea |
<item>First we remove the window we created before because GStreamer will
|
|
Packit |
1470ea |
take care of showing the picture on screen.
|
|
Packit |
1470ea |
</item>
|
|
Packit |
1470ea |
<item>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
Now we are creating a GStreamer element which accesses our webcam. We are
|
|
Packit |
1470ea |
using the Camerabin element, which is an all-in-one camera element and is
|
|
Packit |
1470ea |
capable of taking photos, videos, applying effects and much more. Perfect for
|
|
Packit |
1470ea |
our use case! With this.camerabin.set_state (Gst.State.PLAYING)
|
|
Packit |
1470ea |
we tell the GStreamer pipeline we just created to start playing. Easy, no?
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
Of course it is also possible to integrate the video more tightly into other
|
|
Packit |
1470ea |
windows but that is an advanced topic that includes some details of the X Window
|
|
Packit |
1470ea |
System we will omit here.
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
Compile and run it again. You will end up with two windows. In the next step
|
|
Packit |
1470ea |
we will integrate the video into the GTK+ window.
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
</item>
|
|
Packit |
1470ea |
</steps>
|
|
Packit |
1470ea |
</section>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<section id="impl">
|
|
Packit |
1470ea |
<title>Reference Implementation</title>
|
|
Packit |
1470ea |
If you run into problems with the tutorial, compare your code with this <link href="magic-mirror/magic-mirror.vala">reference code</link>.
|
|
Packit |
1470ea |
There is also a more <link href="magic-mirror/magic-mirror-advanced.vala">extensive implementation</link> that embeds the window into a regular Gtk.Window
|
|
Packit |
1470ea |
which involves some advanced techniques, and adds buttons to start/stop the picture.
|
|
Packit |
1470ea |
</section>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<section id="further">
|
|
Packit |
1470ea |
<title>Further reading</title>
|
|
Packit |
1470ea |
To find out more about the Vala programming language you might want to check out the
|
|
Packit |
1470ea |
<link href="http://live.gnome.org/Vala/Tutorial">Vala Tutorial</link>.
|
|
Packit |
1470ea |
</section>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<section id="conclusion">
|
|
Packit |
1470ea |
<title>Conclusion</title>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
That's it, you have managed to create a full-featured webcam photo
|
|
Packit |
1470ea |
application in 15 minutes. Now you can shave your beard off or add some makeup
|
|
Packit |
1470ea |
to your beautiful face, right before having a beautiful day at your
|
|
Packit |
1470ea |
workplace, where you can impress your friends and colleagues with an awesome
|
|
Packit |
1470ea |
application you just made in 15 minutes.
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
</section>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
</page>
|