• No se han encontrado resultados

5 ¿Cuáles elementos de un modelo educativo sustenta actualmente la UNEVE?

Capítulo III. El Modelo Educativo

III. 5 ¿Cuáles elementos de un modelo educativo sustenta actualmente la UNEVE?

For this initial camera app shown in Figure 18, Camera Preview App, on page 107, we’ll display the view seen by the back-facing Android camera. We’ll use the KetaiCamera class to connect to and start the camera. The KetaiCamera class streamlines this process significantly for us. For example, creating a simple camera preview app using KetaiCamera takes about ten lines of code, compared with about three hundred documented on the Android developer site.8 KetaiCamera helps us set up and control the camera, and it also decodes the YUV9 color format provided by the Android camera into RGB, used in Process-ing.

KetaiCamera works similarly to other Ketai classes that we’ve explored in Using Motion and Position Sensors. First, we create a KetaiCamera object and start() the camera. Then, we update the screen as soon as we receive a new image from the camera via onCameraPreviewEvent(). And finally, we use Processing’s own image() method to display the camera preview.

6. http://ketai.googlecode.com/svn/trunk/ketai/reference/ketai/cv/facedetector/KetaiSimpleFace.html 7. http://developer.android.com/reference/android/media/FaceDetector.Face.html

8. http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/graphics/CameraPre-view.html

9. http://en.wikipedia.org/wiki/YUV#Conversion_to.2Ffrom_RGB

Chapter 5. Using Android Cameras

106

Figure 18—Camera Preview App. The illustration shows a camera preview image at a resolution of 640x480 pixels, displayed on the 800x480 pixels Google Nexus S touch screen.

The code for a basic camera sketch looks like this:

Camera/CameraGettingStarted/CameraGettingStarted.pde import ketai.camera.*;

KetaiCamera cam;

void setup() {

orientation(LANDSCAPE);

cam = new KetaiCamera(this, 640, 480, 30);

imageMode(CENTER);

}

void draw() {

if (cam.isStarted())

image(cam, width/2, height/2);

}

void onCameraPreviewEvent()

{

cam.read();

}

void mousePressed() {

if (cam.isStarted()) {

cam.stop();

} else

cam.start();

}

Let’s take a closer look at the steps you take and methods you use to set up a camera sketch:

❶ Create an instance of the KetaiCameraclass to generate a new camera object, with a preview size of 640x480px width and height, and an update rate of 30 frames per second

❷ Call imageMode() to tell Android to center its camera images on its screen.

All images are now drawn from their center point instead of the default left upper corner

❸ Display the camera preview using the image()10 method. It requires an image source, as well as the x and y coordinate of the image to display.

Optionally, the image can be re-scaled using an additional parameter for the image width and height

❹ Use the onCameraPreviewEvent() callback method notifying us that a new preview image is available. This is the best time to read the new image

❺ Read the camera preview using the read() camera method

❻ Toggle the camera preview on and off when we tap the screen

Release the camera when we pause the app to it available again to other applications

Release the camera if we exit the app

Let’s try the sketch on the Android phone or tablet.

Run the App

Before we run the sketch, we need to give the app permission to use the camera. Here’s how: on the Processing menu bar, select Android→Sketch Permis-sions. In the Android Permissions Selector that appears, check the CAMERA. As we’ve done already in the Using Geolocation and Compass earlier in Section 4.4, Setting Sketch Permissions, on page 83, the Android must allow the app to use the camera through a certificate, or prompt the user to approve the request to use the camera. The app has the permission to use the camera,

10. http://processing.org/reference/image_.html

Chapter 5. Using Android Cameras

108

the device will remember and not prompt the user any more. For this app, we only need to check the CAMERA permission.

Now run the sketch on the device. The rear-facing camera preview starts up as illustrated in Figure 18, Camera Preview App, on page 107, in a resolution of 640px width and 480px height, known as NTSC.11 Android cameras are set to auto mode, so they adjust focus and exposure automatically. On the Google Nexus S developer phone with a native screen resolution of 800x480 pixels, the preview image covers the screen height but not all of the screen width. You can certainly scale and stretch the preview image, which also changes the image aspect ratio and distorts the image. For instance, if you set the width and height parameters in the image() method to screenWidth and screenHeight as in the code below, the camera preview will always stretch full screen, independent of the screen size and resolution.

image(cam, width/2, height/2, width, height);

Go ahead and try the full screen mode on your device. For a preview image in a camera app, it doesn’t seem like a good idea to stretch the image though.

When we write apps that scale seamlessly across devices, we typically lock and maintain aspect ratios for images and UIs.

As we can see in the code on page 107, the steps we take to get the camera started are like the steps we took working with other sensors (Using Motion and Position Sensors). First we instantiate a KetaiCamera object, using a defined width, height, and frameRate. Then we start the camera. And finally, we read new images from the camera using onCameraPreviewEvent(), and display them. The frame rate in this sketch is set to 30 frames per second, which is the typical playback speed for digital video giving the appearance of seamless movement.

Depending on your device and image conversion performance, the image preview might not be able to keep up with the designated thirty previews per second. In that case, the sketch will try to approach the set frame rate as best it can.

With less than ten lines of code added to the typical processing sketch methods, we’ve completed our first camera app. The onPause() and exit() methods are responsible for releasing the camera properly when we pause or exit the app. The methods make sure that other apps can use the cameras, and that we don’t keep them locked down for our app alone. You can only have one active connection to cameras at a time.

11. http://en.wikipedia.org/wiki/Display_resolution

Now let’s add some code so we can toggle between the front- and rear camera, as well as some controls to give the user greater control over the app.