This tutorial demonstrates how to develop a Photo Booth app. Youll build an
app that lets you take pictures, assign pictures to canvases and share pictures
via email. When a picture is taken, it will display in one of 4 canvases. When
all canvases have been assigned pictures, the next picture assignment will
rotate back to first canvas.
Next, from User Interface drawer, select and drop three buttons into this layout and set their properties as
follows:
Select and drop a TextBox component below HorizontalArrangment3. Select and drop Button below this
TextBox and set their properties as below::
Component Rename to Purpose Properties
Initializing global variable: From the Built-in Variables drawer, select initialize global name to block.
Change its name to canvasNum. Next, from Math drawer select 0 number block and plug into socket of the
new variable block:
Starting the camera: When either btnTakePicture or btnCamera buttons are clicked, we will use their
associated Click event handler to capture the event and start the camera using Camera1.TakePicture block:
Saving pictures as canvas backgrounds: After a picture is taken and accepted, the Camera.AfterPicture
event will be triggered. In this event, the global variable canvasNum is incremented by 1. This will allow us to
rotate the canvas and save, as background, to Canvas1 through Canvas4. When canvasNum exceeds 4,
we set it back to 1 so that we can save the picture back to Canvas1 background. To keep the code
modularized, we use a procedure that can accept an image as argument (the full image path of the
just-taken picture).
Using the Built-in Procedures drawer, select Procedure block and add into blocks-editor viewer. The
procedure back has a mutator. The figure below shows how to use the mutator to construct the required
procedure setPictureToCanvas.
The blocks in the procedure will need to inspect the value of our global index, canvasNum. If this value is 1,
then we set the designated image (path) to be the background of Canvas1, if canvasNum is 2, we set it as
the background of Canvas2, and so on. For purpose of this tutorial, we will use and if-then-else block. The
completed code will look like figure below.
The setPictureToCanvas procedure will need to be invoked after a picture is taken (Camera1.AfterPicture).
In this event handler, we increment our global variable canvasNum by 1.
Next, we inspect if the resulting value is greater than 4. If so, we set it back to 1.
The Camera.AfterPicture event handler passes a parameter called image which is the full path of the
picture that was just taken. Knowing this, we invoke our setPictureToCanvas procedure passing this
image parameter to it. The full block-code for Camera1.AfterPicture is shown in figure below:
Now, every time a picture is taken, it will display in Canvas1, then Canvas2, then Canvas3, then Canvas4
and back to will reset back to Canvas1.
Sharing pictures: For an additional feature of the photo booth app, we will allow users to share a picture
by selecting it (via tapping on a canvas) and then tapping on the Share Picture button. To capture the
canvas touch events, we use Canvas.Touched event handler. This event handler is triggered every time the
canvas is touched. Canvas also includes a block property called Canvas.BackgroundImage which returns
the full image-path of the image that is assigned to its background. We retrieve the image-path and display it
in our TextBox (txtPictureToShare):
The completed block-code for handling the touch events on all 4 canvas is shown in figure below. Note that
the code for all 4 canvases are same with exception of canvas name:
The final functionality to implement is to share the picture via Sharing component. When btnSharePicture is
clicked, we need to check if a valid picture was selected. A valid picture would mean that its full-path
starts-with file:///. For this, we use the contains block in the Built-in Text drawer.
If the path includes file:///, then we use Sharing.ShareFileWithMessage block and pass it the full image
path and message-body.
If the path is invalid (doesnt include file:///, we display an alert message using Notifier.ShowAlert block.