How to save this image?

Hi,

Below is code from a sample included with Corona SDK. I would like to modify the sample so the image is saved as a jpg file. Can someone tell me how to do this?

Further below is the entire Main file. Right here in this code is where I want to save the image right before the print statement. Can someone tell me how to do this?

 if image then  
 -- center image on screen  
 image.x = display.contentWidth/2  
 image.y = display.contentHeight/2  
 local w = image.width  
 local h = image.height  
 print( "w,h = ".. w .."," .. h )  
 native.showAlert("Corona", "See image???")  
 end  

MAIN code:

--   
-- Abstract: Camera sample app  
--   
-- Version: 1.2  
--   
-- Updated: September 21, 2011  
--  
-- Update History:  
-- v1.1 Fixed logic problem where it said "session was cancelled".  
-- v1.2 Added Android support.  
--  
-- Sample code is MIT licensed, see http://www.coronalabs.com/links/code/license  
-- Copyright (C) 2011 Corona Labs Inc. All Rights Reserved.  
---------------------------------------------------------------------------------------  
-- Camera not supported on simulator.   
local isXcodeSimulator = "iPhone Simulator" == system.getInfo("model")  
if (isXcodeSimulator) then  
 local alert = native.showAlert( "Information", "Camera API not available on iOS Simulator.", { "OK"})   
end  
  
--  
local bkgd = display.newRect( 0, 0, display.contentWidth, display.contentHeight )  
bkgd:setFillColor( 128, 0, 0 )  
  
local text = display.newText( "Tap anywhere to launch Camera", 0, 0, nil, 16 )  
text:setTextColor( 255, 255, 255 )  
text.x = 0.5 \* display.contentWidth  
text.y = 0.5 \* display.contentHeight  
  
local sessionComplete = function(event)   
 local image = event.target  
  
 print( "Camera ", ( image and "returned an image" ) or "session was cancelled" )  
 print( "event name: " .. event.name )  
 print( "target: " .. tostring( image ) )  
  
 if image then  
 -- center image on screen  
 image.x = display.contentWidth/2  
 image.y = display.contentHeight/2  
 local w = image.width  
 local h = image.height  
 print( "w,h = ".. w .."," .. h )  
 native.showAlert("Corona", "See image???")  
 end  
end  
  
local listener = function( event )  
 if media.hasSource( media.Camera ) then  
 media.show( media.Camera, sessionComplete )  
 else  
 native.showAlert("Corona", "Camera not found.")  
 end  
 return true  
end  
bkgd:addEventListener( "tap", listener )  

[import]uid: 184193 topic_id: 32706 reply_id: 332706[/import]

Did you look into this?

http://docs.coronalabs.com/api/library/display/save.html

Brent
[import]uid: 9747 topic_id: 32706 reply_id: 130094[/import]

Did you look into this?

http://docs.coronalabs.com/api/library/display/save.html

Brent
[import]uid: 9747 topic_id: 32706 reply_id: 130094[/import]

Thanks but the image is not in the display is it? It is in the image variable that I can see. How would I save it from the variable image?

Warren
[import]uid: 184193 topic_id: 32706 reply_id: 130145[/import]

Thanks but the image is not in the display is it? It is in the image variable that I can see. How would I save it from the variable image?

Warren
[import]uid: 184193 topic_id: 32706 reply_id: 130145[/import]

I’ve read through the docs on all of the display and save and cannot get it to work. I did try to use the display.save as shown below but every single time I use the save either on display or from media the program stops right there. Or it does not completed the following line like the showAlert in this case. I never see that alert.

My biggest question right now is how do I call the next line of code or get the app to keep going after a save? Is that done in another listener or what? I want to take the saved picture and publish it to Facebook but I can’t get the app to run more code after the save.

SOMEONE PLEASE HELP ME…

Thanks!!!

local function captureWithDelay()  
 local capture = display.capture()  
 display.save(capture,"picture3.jpg",system.TemporaryDirectory)  
 native.showAlert("Corona", "Saved???")  
end  

[import]uid: 184193 topic_id: 32706 reply_id: 130176[/import]

I’ve read through the docs on all of the display and save and cannot get it to work. I did try to use the display.save as shown below but every single time I use the save either on display or from media the program stops right there. Or it does not completed the following line like the showAlert in this case. I never see that alert.

My biggest question right now is how do I call the next line of code or get the app to keep going after a save? Is that done in another listener or what? I want to take the saved picture and publish it to Facebook but I can’t get the app to run more code after the save.

SOMEONE PLEASE HELP ME…

Thanks!!!

local function captureWithDelay()  
 local capture = display.capture()  
 display.save(capture,"picture3.jpg",system.TemporaryDirectory)  
 native.showAlert("Corona", "Saved???")  
end  

[import]uid: 184193 topic_id: 32706 reply_id: 130176[/import]

Hi Warren,
It might be useful to check out the sample project in your local Corona application folder. I just loaded it up and it seems to work, but I can’t vouch that it’ll do everything you need to. However, worth looking into it…

CoronaSDK > SampleCode > Storage > ScreenCaptureToFile

Brent [import]uid: 9747 topic_id: 32706 reply_id: 130188[/import]

Thanks Brent. I am actually working with that demo now because i figured I could capture an image from the camera, display it on the screen, capture it and then upload it to Facebook. So I’m making some changes to load the image from the camera and go from there. I’ll keep you and everyone posted.

Warren
[import]uid: 184193 topic_id: 32706 reply_id: 130191[/import]

Hi Warren,
It might be useful to check out the sample project in your local Corona application folder. I just loaded it up and it seems to work, but I can’t vouch that it’ll do everything you need to. However, worth looking into it…

CoronaSDK > SampleCode > Storage > ScreenCaptureToFile

Brent [import]uid: 9747 topic_id: 32706 reply_id: 130188[/import]

Thanks Brent. I am actually working with that demo now because i figured I could capture an image from the camera, display it on the screen, capture it and then upload it to Facebook. So I’m making some changes to load the image from the camera and go from there. I’ll keep you and everyone posted.

Warren
[import]uid: 184193 topic_id: 32706 reply_id: 130191[/import]

I modified the code some and came up with this and it never got to the showAlert message. So I’m really stuck. This is why i would prefer to just pay someone to write a sample for what I need.

-- Project: ScreenCaptureToFile  
--  
-- Date: June 6, 2011  
--  
-- Version: 1.4  
--  
-- File name: main.lua  
--  
-- Author: Corona Labs  
--  
-- Abstract: Screen Capture JPG saved to /Documents directory  
--  
-- Demonstrates: audio.play, display.save, tap, detecting device type  
--  
-- File dependencies: none  
--  
-- Target devices: Simulator and Device  
--  
-- Limitations:  
--  
-- Update History:  
-- v1.1 Added app title and message saving JPG saved in /Documents directory  
-- Supports Android with MP3 sound file.  
-- v1.2 Changed to use new audio API  
-- v1.3 Changed to only use .wav  
-- v1.4 Scale the thumbnail according to the size of the captured image   
--  
-- Comments: Stores the capture screen image as a JPG in the /Documents directory  
--  
-- Sample code is MIT licensed, see http://www.coronalabs.com/links/code/license  
-- Copyright (C) 2010 Corona Labs Inc. All Rights Reserved.  
---------------------------------------------------------------------------------------  
-- Forward references  
  
local bkgd = display.newRect( 0, 0, display.contentWidth, display.contentHeight )  
bkgd:setFillColor( 128, 0, 0 )  
  
local text = display.newText( "Tap anywhere to capture screen", 0, 0, nil, 16 )  
text:setTextColor( 255, 255, 0 )  
text.x = 0.5 \* display.contentWidth  
text.y = 0.5 \* display.contentHeight  
  
local soundID = audio.loadSound ("CameraShutter\_wav.wav")  
  
-- Create the Screen Capture  
--  
function bkgd:tap( event )  
 audio.play( soundID )  
 local photo = event.target  
 print( "photo w,h = " .. photo.width .. "," .. photo.height )  
  
 -- Capture the screen and save it to file.  
 local baseDir = system.DocumentsDirectory  
 display.save( display.currentStage, "entireScreen.jpg", baseDir )  
  
 -- Create thumbnail  
 local thumbnail = display.newGroup()  
 local image = display.newImage( "entireScreen.jpg", baseDir )  
 if image then  
 -- Display screen capture image onscreen.  
 thumbnail:insert( image, true )  
 local thumbscale = 0.5 \* display.contentWidth / image.width  
 image:scale( thumbscale, thumbscale )  
 local r = 10  
 local border = display.newRoundedRect( 0, 0, image.contentWidth + 2\*r, image.contentHeight + 2\*r, r )  
 border:setFillColor( 255,255,255,200 )  
 thumbnail:insert( 1, border, true )  
 thumbnail:translate( 0.5\*display.contentWidth, 0.5\*display.contentHeight )  
 print( "File entireScreen.jpg was saved in the documents directory." )  
 native.showAlert( "Corona", "Dream. Build. Ship." )  
 else  
 -- Image file not found. This means that the screen capture failed.  
 -- This can occur if the device does not support screen captures, such as the Droid.  
 msg = display.newText( "Screen captures not supported.", 0, 400, "Verdana-Bold", 16 )  
 thumbnail:insert( msg, true )  
 local r = 10  
 local border = display.newRoundedRect( 0, 0, msg.contentWidth + 2\*r, (msg.contentHeight\*2) + 2\*r, r )  
 border:setFillColor( 0, 0, 0 )  
 thumbnail:insert( 1, border, true )  
 thumbnail:translate( 0.5\*display.contentWidth, 0.5\*display.contentHeight )  
 print( "Platform does not support screen captures." )  
 native.showAlert( "Corona", "Failed." )  
 end  
  
 text:removeSelf() -- remove the Tap message from the screen  
 msg = display.newText( "Screen JPG saved to /Documents", 0, 400, "Verdana-Bold", 14 )  
 msg.x = display.contentWidth/2 -- center title  
 msg:setTextColor( 255,255,255 )  
  
 -- Prevent future taps  
 bkgd:removeEventListener( "tap", bkgd )  
  
 return true  
end  
  
local listener = function( event )  
 if media.hasSource( media.Camera ) then  
 media.show( media.Camera, bkgd )  
 else  
 native.showAlert("Corona", "Camera not found.")  
 end  
 return true  
end  
  
-- Displays App title  
title = display.newText( "Capture Screen to File", 0, 30, "Verdana-Bold", 20 )  
title.x = display.contentWidth/2 -- center title  
title:setTextColor( 255,255,255 )  
  
-- Create the Tap listener  
--  
bkgd:addEventListener( "tap", listener )  

[import]uid: 184193 topic_id: 32706 reply_id: 130194[/import]

I modified the code some and came up with this and it never got to the showAlert message. So I’m really stuck. This is why i would prefer to just pay someone to write a sample for what I need.

-- Project: ScreenCaptureToFile  
--  
-- Date: June 6, 2011  
--  
-- Version: 1.4  
--  
-- File name: main.lua  
--  
-- Author: Corona Labs  
--  
-- Abstract: Screen Capture JPG saved to /Documents directory  
--  
-- Demonstrates: audio.play, display.save, tap, detecting device type  
--  
-- File dependencies: none  
--  
-- Target devices: Simulator and Device  
--  
-- Limitations:  
--  
-- Update History:  
-- v1.1 Added app title and message saving JPG saved in /Documents directory  
-- Supports Android with MP3 sound file.  
-- v1.2 Changed to use new audio API  
-- v1.3 Changed to only use .wav  
-- v1.4 Scale the thumbnail according to the size of the captured image   
--  
-- Comments: Stores the capture screen image as a JPG in the /Documents directory  
--  
-- Sample code is MIT licensed, see http://www.coronalabs.com/links/code/license  
-- Copyright (C) 2010 Corona Labs Inc. All Rights Reserved.  
---------------------------------------------------------------------------------------  
-- Forward references  
  
local bkgd = display.newRect( 0, 0, display.contentWidth, display.contentHeight )  
bkgd:setFillColor( 128, 0, 0 )  
  
local text = display.newText( "Tap anywhere to capture screen", 0, 0, nil, 16 )  
text:setTextColor( 255, 255, 0 )  
text.x = 0.5 \* display.contentWidth  
text.y = 0.5 \* display.contentHeight  
  
local soundID = audio.loadSound ("CameraShutter\_wav.wav")  
  
-- Create the Screen Capture  
--  
function bkgd:tap( event )  
 audio.play( soundID )  
 local photo = event.target  
 print( "photo w,h = " .. photo.width .. "," .. photo.height )  
  
 -- Capture the screen and save it to file.  
 local baseDir = system.DocumentsDirectory  
 display.save( display.currentStage, "entireScreen.jpg", baseDir )  
  
 -- Create thumbnail  
 local thumbnail = display.newGroup()  
 local image = display.newImage( "entireScreen.jpg", baseDir )  
 if image then  
 -- Display screen capture image onscreen.  
 thumbnail:insert( image, true )  
 local thumbscale = 0.5 \* display.contentWidth / image.width  
 image:scale( thumbscale, thumbscale )  
 local r = 10  
 local border = display.newRoundedRect( 0, 0, image.contentWidth + 2\*r, image.contentHeight + 2\*r, r )  
 border:setFillColor( 255,255,255,200 )  
 thumbnail:insert( 1, border, true )  
 thumbnail:translate( 0.5\*display.contentWidth, 0.5\*display.contentHeight )  
 print( "File entireScreen.jpg was saved in the documents directory." )  
 native.showAlert( "Corona", "Dream. Build. Ship." )  
 else  
 -- Image file not found. This means that the screen capture failed.  
 -- This can occur if the device does not support screen captures, such as the Droid.  
 msg = display.newText( "Screen captures not supported.", 0, 400, "Verdana-Bold", 16 )  
 thumbnail:insert( msg, true )  
 local r = 10  
 local border = display.newRoundedRect( 0, 0, msg.contentWidth + 2\*r, (msg.contentHeight\*2) + 2\*r, r )  
 border:setFillColor( 0, 0, 0 )  
 thumbnail:insert( 1, border, true )  
 thumbnail:translate( 0.5\*display.contentWidth, 0.5\*display.contentHeight )  
 print( "Platform does not support screen captures." )  
 native.showAlert( "Corona", "Failed." )  
 end  
  
 text:removeSelf() -- remove the Tap message from the screen  
 msg = display.newText( "Screen JPG saved to /Documents", 0, 400, "Verdana-Bold", 14 )  
 msg.x = display.contentWidth/2 -- center title  
 msg:setTextColor( 255,255,255 )  
  
 -- Prevent future taps  
 bkgd:removeEventListener( "tap", bkgd )  
  
 return true  
end  
  
local listener = function( event )  
 if media.hasSource( media.Camera ) then  
 media.show( media.Camera, bkgd )  
 else  
 native.showAlert("Corona", "Camera not found.")  
 end  
 return true  
end  
  
-- Displays App title  
title = display.newText( "Capture Screen to File", 0, 30, "Verdana-Bold", 20 )  
title.x = display.contentWidth/2 -- center title  
title:setTextColor( 255,255,255 )  
  
-- Create the Tap listener  
--  
bkgd:addEventListener( "tap", listener )  

[import]uid: 184193 topic_id: 32706 reply_id: 130194[/import]

Hi Warren,
So it just halts outright? And there are no error messages in the Terminal whatsoever? What platform are you using?

I understand it might be easier to just recruit (hire) somebody to tackle this for you. There are several developers who can assist you quickly and professionally. The best place for your request to get noticed is the “Corona Jobs” forum, linked below. You could also post a link on one of the e-lance job sites, and most likely you’ll have a dozen offers within an hour. :slight_smile:

http://developer.coronalabs.com/forums/corona-jobs

Brent
[import]uid: 9747 topic_id: 32706 reply_id: 130201[/import]

Hi Warren,
So it just halts outright? And there are no error messages in the Terminal whatsoever? What platform are you using?

I understand it might be easier to just recruit (hire) somebody to tackle this for you. There are several developers who can assist you quickly and professionally. The best place for your request to get noticed is the “Corona Jobs” forum, linked below. You could also post a link on one of the e-lance job sites, and most likely you’ll have a dozen offers within an hour. :slight_smile:

http://developer.coronalabs.com/forums/corona-jobs

Brent
[import]uid: 9747 topic_id: 32706 reply_id: 130201[/import]