Posted: 5th December 2018

Author: Ric

Tagged: Products & Services

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, and is fully documented at our Corona plugins site. You can also watch a quick video of our camera plugin in action at Youtube.

Blog posts written by former QWeb employees are not necessarily an accurate indication of the current opinions of QWeb Ltd and the information provided in tutorials might be biased or subjective, or might become out of date.

Discuss this post

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 and you won't be added to any mailing list and your details won't be shared with any third party.