Display screenshot after it is taken - lua

I am trying to display a screenCap after it is taken, it is saving the screenCap but how can i get the latest screenCap's url ?
local screenCap = display.captureScreen( true )
local alert = native.showAlert( "Success", "Screen Capture Saved to Library", { "OK" } )
NewsScreenShot = display.newImage( " path to the PNG file " )

http://jp.anscamobile.com/dev/reference/index/displaycapturescreen/index.html
The picture will be saved in the device gallery with a name Picture X.png. After this you need to custom selection to the latest indexed picture.
Btw you can try it whit display.save("name",path) and this will be alway the last saved picture.

display.captureBounds is good for saving the whole screen to the directory. But it usually saves the file with increase in last index. So it may be difficult to read them correctly. So I prefer display.save. But it is not a straight way.
For doing this, you have to:
First create a displayGroup.
Then add the screen objects to that group.
Return the display group.
Use display.save to save the entire group displayed.
Display the desired image from system.DocumentsDirectory.
I am giving a sample here:
-- creating the display group --
local localGroup = display.newGroup()
-- creating display objects and adding it to the group --
local bg = display.newRect(0,0,_w,_h)
bg.x = 160
bg.y = 240
bg:setFillColor(150)
localGroup:insert(bg)
local rect = display.newRect(0,0,50,50)
rect.x = 30+math.random(260)
rect.y = 30+math.random(420)
localGroup:insert(rect)
-- Take Screenshot --
local function saveGroupImages()
-- take screen shot to baseDirectory --
local baseDir = system.DocumentsDirectory
display.save( localGroup, "myScreenshot.jpg", baseDir )
end
rect:addEventListener("tap",saveGroupImages)
After this, you can read the file and display it as follows:
local readImage = display.newImage( "myScreenshot.jpg" ,system.DocumentsDirectory , 50, 100 )
readImage.x = 160
readImage.y = 240
readImage:scale(0.5,0.5)
keep Coding........... :)

Related

dynamically add spritesheets to sequenceData array in corona

Problem: I have a long duration frame-based animation for which i have created many spritesheets (e.g. 50 spritesheets with 10 images in each - 2 MB per spritesheet). It takes noticeable time (and memory) to pre-load all 50 spritesheets before the animation can be played using code based on the example code snippet below (this is suggested in the corona docs):
local function spriteListener( event )
thisSprite = event.target -- "event.target" references the sprite
if ( event.phase == "ended" ) then
sequenceNumer = sequenceNumer + 1;
if (sequenceNumer <=10) then
thisSprite:setSequence(filenameArray[sequenceNumer])
thisSprite:play()
end
end
end
local spriteWidth = 551
local spriteHeight = 401
local numFrames1 = 15
local startFrame = 1
frameInfoSet1 = {width = spriteWidth , height = spriteHeight, numFrames = numFrames1}
filenameArray = {"001-015","016-030","031-045","046-060","061-075","076-090","091-105","106-120","121-135","136-150"}
fullSequence =
{
{name=filenameArray[1], sheet=graphics.newImageSheet("spritesheets/" .. filenameArray[1] .. ".png", frameInfoSet1), start = startFrame, count=numFrames1, time=2250, loopCount=1},
{name=filenameArray[2], sheet=graphics.newImageSheet("spritesheets/" .. filenameArray[2] .. ".png", frameInfoSet1), start = startFrame, count=numFrames1, time=2250, loopCount=1},
{name=filenameArray[3], sheet=graphics.newImageSheet("spritesheets/" .. filenameArray[3] .. ".png", frameInfoSet1), start = startFrame, count=numFrames1, time=2250, loopCount=1},
-- more such spritesheets are loaded further...
}
firstSpriteSheet = fullSequence[1]["sheet"]
sequenceNumer = 1
tt = display.newSprite (firstSpriteSheet, fullSequence)
tt.x = display.contentWidth/2 ; tt.y = display.contentHeight/2
tt:addEventListener( "sprite", spriteListener )
tt:play()
However, what i would like to achieve is a "streaming" version of this technique. i.e. I will load only 3 spritesheets out of the total of 50, therefore requiring say 6 MB to start playing the animation and keep loading additional spritesheets as the already loaded ones keep playing. So, to illustrate further, i load sheets 1,2,3 and start playing the animation and load sheet 4 by the time sheet 1 is finished, load sheet 5 by the time sheet 2 is finished and so on.
Any advice is appreciated ! Thanks in advance.
I see this is old question, but I will add thoughts from my expirience:
This is not an easy task, but it's possible
Preload sheets before you go
I suggest to add simple loading scene, where you will show progress bar while loading.
local filenameArray = ... -- this is from your code
local preloadedSheets = {}
local function enterFrameLoading()
local nextSheetToLoad = #preloadedSheets + 1
if (nextSheetToLoad > #filenameArray) then
Runtime:removeEventListener("enterFrame", enterFrameLoading)
-- go to game scene if you are using scenes
return
end
preloadedSheets[nextSheetToLoad] =
graphics.newImageSheet("spritesheets/" .. filenameArray[nextSheetToLoad] .. ".png", ??)
updateProgress(nextSheetToLoad) -- this function will update UI, i.e. progress bar, text, etc
end
Runtime:addEventListener("enterFrame", enterFrameLoading)
After preloading all sheets will be in memory, so you shouldn't have performance issues
Use multiple sprite objects, created on demand
You can create new objects inside spriteListener - again, store all sprites inside a table and use .isVisible = false to hide old sprites
Combine solutions 1 and 2
You can create sprite objects with just 3 sequences. Then preload other sheets in enterFrame listener like I showed above.
When preloading is finished you want to destroy old sprite and create new with the same sequence and frame
Let me know if you need more information on this

CoronaSDK Touch Events

Currently creating a game with Corona SDK is it possible to have an image and when it is clicked it displays 3 images and once one them 3 images are clicked the score increases by 1. Also Im only a beginner at coding , this is a new language to me. Thanks.
local CButton = display.newImage("+5.jpg" , 100 , 600)
CButton.alpha = 0.5
CButton.name = "CButton"
local CButtonLabel = display.newText( { text = "", x = 0, y = 0, fontSize = 28 } )
CButtonLabel:setTextColor( 0 ) ; CButtonLabel.x = 100 ; CButtonLabel.y = 45
local function touchCListener( event )
local object = event.target
print( event.target.name.." TOUCH on the '"..event.phase.."' Phase!" )
local ChordCOne = display.newImage("+5.jpg", 900,300)
local ChordCTwo = display.newImage("+5.jpg", 1000,300)
local ChordCThree = display.newImage("+5.jpg", 1100,300)
end
--add "touch" listener -- LABEL IS FOR TESTING!
CButton:addEventListener( "touch", touchCListener)
ChordCOne:addEventListener( "touch", updateScore)
CButtonLabel.text = "touch"
Yes, new DisplayObjects can be created in a listener function and listeners can be added to those objects as well.
In your code, you have not added the DisplayObjects created in your listener to any GroupObject (such as scene.view), which will give unexpected results.
Since the variables pointing to the newly created DisplayObjects (ChordCOne, etc.) are local to the function where they are instantiated, you cannot call addEventListener() on them outside the function. You should add the listener when they are created.
Also, the updateScore() listener function isn't defined anywhere. Make sure updateScore is not nil when and wherever you give it as an argument to addEventListener().

Image displays in top left corner in Corona Simulator

I just started playing with lua / Corona, and have been using the book "Create Mobile Games with Corona." (Silvia Domenech) The first project in the book is to make a simple planet defense game. For this, I am using a multi-screen application (or "scene" as it's called in OS X). The very first step is displaying an image using groups in Corona.
The book has me enter the following code in the main scene group:
local image = display.newImage( "images/iphone_767.png")
group:insert( image )
After adding it to (what I think is) the correct group, the code looks like this:
-- Called when the scene's view does not exist:
function scene:createScene( event )
local group = self.view
-----------------------------------------------------------------------------
local image = display.newImage( "images/iphone_767.png")
group:insert( image )
-- CREATE display objects and add them to 'group' here.
-- Example use-case: Restore 'group' from previously saved state.
-----------------------------------------------------------------------------
end
For the image, I first tried an image that was 320 x 480. When it rendered on the simulator, it is situated way off to the top left of the simulator. Here is a screenshot of how it renders: http://imgur.com/Kpm0XTd
The config file is set for 320 x 480 px. I'm really at a loss as to what could be causing this since I haven't modified anything outside of what I described. Any ideas?
Set the x and y values of the token to screen center (assuming you want to center it):
image.x = display.contentWidth/2
image.y = display.contentHeight/2
Thanks
Anand
I personally like setting a variable to display.contentWidth and display.contentWidth.
_W = display.contentWidth
_H = display.contentHeight
image.x = _W/2
image.y = _H/2
You could also format it like so:
image.x = _W*.5
image.y = _W*.5
Corona SDK has recently started using a new graphics engine that affects how things are positioned. The display.newImage() used to draw the top left corner at 0, 0, but now it draws the center at 0, 0. Your best bet is to always explicitly set the .x and .y of the image where you want it.
Seems there is simplified way exists now:
image.x = display.contentCenterX
image.y = display.contentCenterY
Or single line:
local image = display.newImage('images/iphone_767.png', display.contentCenterX, display.contentCenterY)
But now it is seems more conventional pattern is to create centeredGroup first:
-- Create centered group
local centeredGroup = display.newGroup()
centeredGroup.x = display.contentCenterX
centeredGroup.y = display.contentCenterY
-- Add background
local image = display.newImage(centeredGroup, 'images/iphone_767.png', display.contentCenterX, display.contentCenterY)

How to change the position of display objects which are inside a table

I want to create a table with display Objects on specific positions. I wrote the following
for i=0, 5 do
life[i] = display.newImage( "life.png" )
end
but when I am trying this:
for i=0, 5 do
life[i] = display.newImage( "life.png" )
life[i].x=i*Space_
end
compiler complains attemp to index field ? nil value
Any idea why this happen or how can I solve it? I want to use a for loop to add objects in a table (or maybe group? ) on specific positions.
Show us where Space_ is in your code.
Try this:
local SpaceX = 10
for i=0, 5 do
life[i] = display.newImage( "life.png" )
life[i].x = i * SpaceX
end
Corona doesn't find your image and returns nil as the result of display.newImage call. When you try then to access field x of a nil value, you get the error.
Make sure your image is available to your script.
This assert should probably trigger, because life.png could not be found
for i=0, 5 do
life[i] = assert(display.newImage("life.png"), "image could not be found")
life[i].x=i*Space_
end

Combine images into single image

How can I combine two PNGs into one image?
If one image is displays "1", and another image displays "9" - I'd like make an image file showing "19".
There is a object "group" which group the images into an array, but it doesn't seem that I can merge the members of a group.
number1 = display.newImage( "number1.png" );
number9 = display.newImage( "number9.png" );
number19 = display.newGroup();
number19:insert(number1)
number19:insert(number9)
--Put 9 next to 1
number9.left =number1.width
I use a snapshot when I need to combine images. Very similar to a group, the snapshot can be rendered out as a single PNG or JPEG.
local Function MakeSnapshot()
local snapshot = display.newSnapshot(digitWidth*2, digitWidth)
local digit1 = display.newImage("number1.png")
local digit2 = display.newImage("number9.png")
digit1:translate(-digitWidth/2, 0)
digit2:translate(digitWidth/2, 0)
snapshot.group:insert(digit1)
snapshot.group:insert(digit2)
snapshot:invalidate()
--Save file as a single image:
display.save(snapshot, "19.png", system.DocumentsDirectory)
end

Resources