Blame platform-demos/gl/magic-mirror.vala.page

Packit 1470ea
Packit 1470ea
<page xmlns="http://projectmallard.org/1.0/" xmlns:its="http://www.w3.org/2005/11/its" type="topic" id="magic-mirror.vala" xml:lang="gl">
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
  
Packit 1470ea
    <mal:credit xmlns:mal="http://projectmallard.org/1.0/" type="translator copyright">
Packit 1470ea
      <mal:name>Fran Dieguez</mal:name>
Packit 1470ea
      <mal:email>frandieguez@gnome.org</mal:email>
Packit 1470ea
      <mal:years>2012-2013.</mal:years>
Packit 1470ea
    </mal: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>

Crear un apliativo GTK+

</item>
Packit 1470ea
    <item>

Acceda á súa cámara web usando GStreamer e incruste o resultado nunha xanela

</item>
Packit 1470ea
    <item>

Obter as fotos da śua cámara web

</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>

Copias instaladas de GTK, GStreamer e compilador de Vala

</item>
Packit 1470ea
    <item>

Coñecemento básico dun linguaxe de programación orientado a obxectos

</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>Cree un proxecto de Anjuta</title>
Packit 1470ea
  

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.

Packit 1470ea
  <steps>
Packit 1470ea
    <item>
Packit 1470ea
    

Inicie Anjuta e prema <guiseq><gui>Ficheiro</gui><gui>Novo</gui><gui>Proxecto</gui></guiseq> para abrir o asistente de proxectos.

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>Construír o código por primeira vez</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>Acceder ao fluxo de vídeo de cámara web con GStreamer</title>
Packit 1470ea
 

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.

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>

Primeiro quítase a xanela creada anteriormente, xa que GStreamer encargarase de mostrar a imaxe na pantalla.

Packit 1470ea
 </item>
Packit 1470ea
  <item>
Packit 1470ea
  

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?

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>Implementación de referencia</title>
Packit 1470ea
 

Se ten problemas con este titorial, compare o seu código con este <link href="magic-mirror/magic-mirror.vala">código de referencia</link>. Tamén hai unha <link href="magic-mirror/magic-mirror-advanced.vala">implementación máis ampla</link> que incrusta a xanela nunha xanela GTK regular que implica algunhas técnicas avanzadas, e engade botóns para iniciar/deter a imaxe.

Packit 1470ea
</section>
Packit 1470ea
Packit 1470ea
<section id="further">
Packit 1470ea
<title>Lectura complementaria</title>
Packit 1470ea

Para atopar máis información sobre a linguaxe de programación Vala pode visitar <link href="http://live.gnome.org/Vala/Tutorial">o titorial de Vala</link>.

Packit 1470ea
</section>
Packit 1470ea
Packit 1470ea
<section id="conclusion">
Packit 1470ea
<title>Conclusión</title>
Packit 1470ea
  

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.

Packit 1470ea
Packit 1470ea
</section>
Packit 1470ea
Packit 1470ea
</page>