Screenshots, saving images and accessing the camera

Just started using Codea a couple of days and am really impressed with it, got a couple of ideas I wanna mess around with, but was unsure if its possible or not…

I know that when you run your program you get the buttons at the bottom of the screen that allow you to video and capture screenshots of your program running, is there a way to access these from within your code? I know theres the function to stop and start recording but i cant seem to find anything for taking screenshots.

Also, I’m thinking it might be handy to grab some pics from the camera/photo gallery, is this possible?

We will be adding features like that in a future update. For now you can use setContext() to render into an image — it’s not as nice as a function for grabbing the screen but it should be able to help you now.

I put some past advice by Simeon on screen capture on to the wiki.

Thanks @mpilgrem. I just want to add: that method will not work if you need to use setContext for other reasons within your draw function. But it’s good if you are drawing a scene that does not rely on setContext.

I’ve updated the wiki page to make that clear. I think the following code would deal with the circumstance you describe. If you agree, I’ll add it to the page.

_setContext = setContext  -- preserve the built-in setContext() function
_screenContext = nil      -- if not nil, will act as buffer

-- Overwrite the built-in setContext() function
setContext = function(img)
	if img then return _setContext(img) end -- use img, if exists
	if _screenContext then return _setContext(_screenContext) end -- use buffer, if exists
	return _setContext() -- otherwise, call without arguments
function captureScreen()
	_screenContext = image(WIDTH, HEIGHT)   -- initialise buffer the size of the screen 
	draw()                                  -- draw a new frame (to the buffer)
	local screenCap = _screenContext        -- preserve the buffer
	_screenContext = nil                    -- set buffer to nil, restoring screen
	return screenCap                        -- return the image

That’s a very clever piece of code. That allows nested use of setContext? I wasn’t sure that was possible without us adding additional features to the runtime, but from reading your code it makes sense.

Thank you - I was inspired by the loveCodea.lua wrapper.