Published Wednesday 5th December 2018

Android Custom Camera Preview for Corona SDK

We're building a mobile app for a client at the moment, and for a number of reasons chose Corona SDK as the development platform for this. Corona is intended for game programming really and is therefore a little limited in comparison to native device code when it comes to accessing operating system level functionality, which created a slight stumbling block for us. One feature of this app is the ability to take approximate measurements of trees. We need to record the timber height, (the distance between the base of the tree and the point where the trunk can no longer be distinguished from branches), and the total tree height, (the distance between the base of the tree and the very top point where tree meets sky). To do this we're using trigonometry and readings from the device accelerometer, so to help the user in taking reasonably accurate readings we wanted to output the camera image to screen, overlay it with a cross-hair, instructions, and a live angle of rotation, and make 3 readings before closing the camera view again - one reading while pointing towards the base of the tree, one while pointing towards the top of the trunk, and one while pointing at the top of the tree.

Out of the box, Corona can launch the native camera app of the device and wait for the user to take a photo or select from its media gallery, but this wasn't good enough for our needs. We wouldn't be able to overlay the image with anything, we wouldn't be able to receive feedback from the camera app except for if the user takes a photo, which wouldn't be useful, and we wouldn't be able to execute any code while the camera app was open.

We've therefore built our own Corona plugin that meets all of these requirements, providing Android developers who use Corona SDK with functionality to open a rendered camera preview above their Corona app, overlay it with a centered image (either a frame, a cross-hair, or anything else), large text towards the top of the preview (to output instructions, for example), and smaller text towards the bottom of the preview, (to output the angle of measurement and approximate height data in our case). While the camera feed is open, your Corona code continues to run in the background and will continue to receive tap events etc, despite not being visible to the user. For our needs, this allows us to create a full screen button so that the user can tap anywhere on the camera feed and a traditional tap event sits in the background to process that action, resulting in changes to the overlaying texts and after 3 taps, closing the camera feed again.

Our Android Custom Camera Preview plugin is available to all Corona SDK developers through the Corona marketplace Solar2D Plugins Marketplace, and is fully documented at our Corona plugins site. You can also watch a quick video of our camera plugin in action at Youtube.

Custom Camera Preview plugin screenshot
Custom Camera Preview plugin screenshot
Photo of Ric

Ric

Ric is a senior web and game programmer with nearly 30 years industry experience and countless programming languages in his skillset. He's worked for and with a number of design and development agencies, and is the proprietor of QWeb Ltd. Ric is also a Linux server technician and an advocate of free, open-source technologies. He can be found on Mastodon where he often posts about the projects he's working on both for and outside of QWeb Ltd, or you can follow and support his indie game project on Kofi. Ric also maintains our Github page of useful scripts.

Blog posts are written by individuals and do not necessarily depict the opinions or beliefs of QWeb Ltd or its current employees. Any information provided here might be biased or subjective, and might become out of date.

Discuss this post

Nobody has commented yet.

Leave a comment

Your email address is used to notify you of new comments to this thread, and also to pull your Gravatar image. Your name, email address, and message are stored as encrypted text. You won't be added to any mailing list, and your details won't be shared with any third party.

This site is protected by reCAPTCHA and the Google Privacy Policy & Terms of Service apply.