Capture whole screen at any time with Corona

Introduction:
Think of a scenario where you are in certain position of a game level where you need to save the screenshot of that instance and probably you can share them later to your social network. So don’t worry more. Using Corona with few simple steps you can implement this feature to you application

Description:
Corona provides several API which makes your life easy while developing your application. ‘Display’ is the one of the API which generally called to populate UI element on the screen but it also have some function which can be called to capture the screen.

So let’s have a screen where we will have few UI elements and then we will take a screenshot of the screen.

CODE BLOCK:

-- Define background color of the screen
display.setDefault( "background", 180, 180, 180 )

Lets add few UI elements to the screen and let me describe the way to capture the screen.

-- Define a label
local label = display.newText( "Click here to capture screen!", 50, 100, nill, 30 )

-- Mentioned the text color
label:setTextColor( 0, 0, 255 )

 -- Define a rectangle
local myBox = display.newRect( 50, 200, 200, 200 )

-- Mentioned color of the rectangle
myBox:setFillColor( 120, 120, 120 )

On above code we have added a label and a rectangle as UI element of the screen.

-- Define a function to capture the screen
local function labelClickListener(event)

-- Check the touch start condition
if event.phase == 'began' then

-- Capture screen
local captureMyScreen = display.captureScreen()
end
end
-- Assign event listener to the label label:addEventListener( 'touch', labelClickListener)

Here I have make a touch event listener to the label. Now on click of the label ‘labelClickListener’ function will be called.

On ‘labelClickListener’ function I have check the condition of began state of the touch and then perform the capturing. To capture the screen I define a variable called ‘captureMyScreen’ where I have called display.captureScreen() function which exactly takes the screenshoot.

Now if you want to show the captured image to the same window, simply modify the above function with below code.

local function labelClickListener(event)

-- Check the touch start condition
if event.phase == 'began' then

-- Capture screen
local captureMyScreen = display.captureScreen()
captureMyScreen:scale( .6, .6)

-- Mention the position of the captured screen view on screen       captureMyScreen.x, captureMyScreen.y = 200, 400
end
end

So here define a local variable ‘captureMyScreen’ to take the screenshoot and  then scaled down it because with scaling down we can display the whole image to the screen and then sets the coordinates (x,y) value to set the image position on the screen.

Again if we want to save this image to device memory then while capturing the screen modify the code with the following line.

local captureMyScreen = display.captureScreen( true ).

With passing a boolean parameter to the function ‘display.captureScreen’ means that do we need to save that image to the device memory or not. It works perfectly on IOS but to do the same on Android we need to add the following code to settings file which additionally gives the permission to save the image on Android platform.

settings =
{
android =
{
usesPermissions =
{
"android.permission.WRITE_EXTERNAL_STORAGE",
},
},
}

Summary:

So now we have learned to get the screenshot of the application window at any instance of time and save them to the device and if necessary we can also display them to the screen using Corona and off course with its APIs.

Next I will describe you the way to capture any specific UI element from the screen and to save them on device.

Written ByRaju Mahato, Software Developer, Mindfire Solutions

Advertisements

3 thoughts on “Capture whole screen at any time with Corona

  1. Pingback: Capture whole screen at any time in your crossplatform mobile app with Corona. | Cross Platform Mobile App Architecture

  2. Pingback: Capture specific UI element from the screen at anytime with Corona | MindfireMobile

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s