POWERAPPS – PHOTO CAMERA SELECTION

For any mobile app these days it’s normally a requirement to be able to take photos for reference, and the new App I’m working on is no different.

Adding a photo facility to an App is very straightforward – put a Camera control onto a screen and add code in the OnSelect to take the Photo and add it a collection using Patch. Here’s my code to do this:

Patch(colPhotos, Defaults(colPhotos), {Value:Camera1_1.Photo, PhotoDateTime:Now()});

Of course most devices have multiple cameras these days – at minimum one front facing, and one rear facing.   PowerApps allows you to change the camera in use by setting the Camera property of the Camera control (!) with a number.  

These are the devices I have, and their associated Camera numbers

Samsung Galaxy S7 
0 = Rear Camera, 1 = Front Camera, 2 = Not Available

IPhone 7
0 = Rear Camera, 1 = Front Camera, 2 = Front Camera

Samsung Galaxy S9
0 = Front Camera, 1 = Front Camera (Zoomed in), 2 = Rear Camera

NOTE: All the devices I’ve tested only have two cameras, 1 front and 1 rear – though there are many newer devices which have several rear cameras.  I’m guessing that they would have Camera numbers from 2 onwards though I can’t verify this.

So it appears that the rear camera, which the user would most likely use, is not always 0.  To allow the user to change camera I use a variable and store this to local cache so the App will start on the same camera the user was last on.

Code wise – the Camera control’s Camera property is set to varCameraNumber.  There is a ‘Switch Camera’ button which loops around Cameras 0 to 2 using the code:

If(varCameraNumber=2,
UpdateContext({varCameraNumber:0});,
UpdateContext({varCameraNumber:varCameraNumber+1});
);

This is the code in the Screen OnVisible section.  It uses LoadData with the 3rd parameter IgnoreNonexistentFile set to True.  This is for when the App is first run and the file doesn’t exist.

LoadData(colCameraNumber, “colCameraNumber” ,true);
If(CountRows(colCameraNumber) = 0,
UpdateContext({varCameraNumber:0});,
UpdateContext({varCameraNumber:First(colCameraNumber).CameraNumber});
);

The last bit of code is at the end of the OnSelect of the ‘Switch Camera’ button.

ClearCollect(colCameraNumber,{CameraNumber:varCameraNumber});
SaveData(colCameraNumber,”colCameraNumber”);

That’s it, a fully functioning Camera control allowing switching of the viewing camera, which is saved per session too.  🙂

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s