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?
In this tutorial, we're going to make a program which lets you use your webcam as a mirror. You will learn how to:
Crear un apliativo GTK+
Acceda á súa cámara web usando GStreamer e incruste o resultado nunha xanela
Obter as fotos da śua cámara web
You'll need the following to be able to follow this tutorial:
An installed copy of the Anjuta IDE
Copias instaladas de GTK, GStreamer e compilador de Vala
Coñecemento básico dun linguaxe de programación orientado a obxectos
Antes de comezar a programar, deberá configurar un proxecto novo en Anjuta. Isto creará todos os ficheiros que precise para construír e executar o código máis adiante. Tamén é útil para manter todo ordenado.
Inicie Anjuta e prema
Choose
Disable
Make sure that
Click
The code loads an (empty) window and shows it. More details are given below; skip this list if you understand the basics:
The two using
lines import namespaces so we don't have to name them explicitly.
The constructor of the Main
class creates a new window and sets its title. Afterwards the window
is shown and a signal is connected which quits the application if the window is closed. More on signals later on.
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).
This code is ready to be used, so you can compile it by clicking
Change the
O contorno multimedia de traballo de GStreamer é capaz de manexar vídeo desde cámara web. Engada GStreamer ao seu aplicativo e poderá acceder ao fluxo de vídeo.
Primeiro quítase a xanela creada anteriormente, xa que GStreamer encargarase de mostrar a imaxe na pantalla.
Agora vaise crear un elemento de GStreamer que accede á cámara web. Vaise a usar o elemento «Camerabin», que é un elemento de cámara todo en un capaz de facer fotos, vídeos, aplicar efectos e moito máis. Perfecto para o noso caso de uso. Con this.camerabin.set_state (Gst.State.PLAYING)
indícase á túbería de GStreamer que se acaba de crear que empece a reproducir. Sinxelo, non?
Of course it is also possible to integrate the video more tightly into other windows but that is an advanced topic that includes some details of the X Window System we will omit here.
Compile and run it again. You will end up with two windows. In the next step we will integrate the video into the GTK+ window.
Se ten problemas con este titorial, compare o seu código con este código de referencia. Tamén hai unha implementación máis ampla que incrusta a xanela nunha xanela GTK regular que implica algunhas técnicas avanzadas, e engade botóns para iniciar/deter a imaxe.
Para atopar máis información sobre a linguaxe de programación Vala pode visitar o titorial de Vala.
Isto é todo, acaba de crear un aplicativo completo para sacar fotos coa cámara web en menos de 15 minutos. Agora xa pode afeitar a barba, maquillarse un pouco e ordenar o seu espazo de traballo para impresionar aos seus amigos e colegas con un aplicativo feito en 15 minutos.