The following code is supposed to start the camera (when you hit “shoot”)
After the picture is taken it should save it and display it on screen.
That last part doesn’t happen unless you touch the screen.
Any ideas why?
I am testing on Android phones
(the code needs a simple 320x480 config, and build.settings should give permissions to CAMERA and WRITE_EXTERNAL_STORAGE)
local screenW, screenH, midW, midH = display.viewableContentWidth, display.viewableContentHeight, display.viewableContentWidth\*0.5, display.viewableContentHeight\*0.5 local widget = require( "widget" ) local button1, infoText, img1 local function fitImage( displayObject, fitWidth, fitHeight, enlarge ) -- https://coronalabs.com/blog/2014/06/10/tutorial-fitting-images-to-a-size/ -- first determine which edge is out of bounds -- local scaleFactor = fitHeight / displayObject.height local newWidth = displayObject.width \* scaleFactor if newWidth \> fitWidth then scaleFactor = fitWidth / displayObject.width end if not enlarge and scaleFactor \> 1 then return end displayObject.width = displayObject.width \* scaleFactor displayObject.height = displayObject.height \* scaleFactor end local function onPhotoComplete( event ) if ( event.completed ) then infoText.text = infoText.text .. " in " img1 = display.newImage( "myimage.jpg", system.DocumentsDirectory) fitImage( img1, 150, 150, false ) img1.y = midH-100 img1.x = midW display.save( img1, { filename="myimagesmall.jpg", baseDir=system.DocumentsDirectory, isFullResolution=true } ) timer.performWithDelay( 2000, function() media.save( "myimagesmall.jpg", system.DocumentsDirectory ); end ) end end local function shootphoto(event) if ( media.hasSource( media.Camera ) ) then media.capturePhoto( { listener = onPhotoComplete, destination = { baseDir = system.DocumentsDirectory, filename = "myimage.jpg", type = "image" } } ) else native.showAlert( "Corona", "This device does not have a camera.", { "OK" } ) end infoText.text = infoText.text .. " out " return true end local mainbackground = display.newRect(midW,midH,screenW,screenH) mainbackground:setFillColor( .2,.2,.2 ) button1 = widget.newButton {x = midW, y = midH+100, label = "Shoot"} button1:addEventListener( "tap", shootphoto ) infoText = display.newText( "testing ", midW, midH, native.systemFont, 20 ) infoText:setFillColor( 1 )