Hello everyone,
I’m not sure if what i’m doing here is right, we had a place on the old corona website to do this
which is code contribution
since i love Corona or Solar2D, and i’m living off of it and i noticed a lot of guys asking about this … so i wanted to share my code with you … this code took me long to reach out this stable stage … so there you go
it will allow you to take a photo, and save it in smaller size, it will look almost the same … very difficult to tell the difference but very much smaller in size … you can also use it to reduce images from gallery …same way
please download the latest build 3617 (yesterday’s build) because it solved a severe problem in media.capture which occurred in so many devices and was crashing media.capturePhoto function
just paste the code in main.luaPreformatted text
you might want to tap the circle 3 times the first time to get permissions
local newName=""
local newName2=""
local imgExtension=".jpg"--"png if you wish ... i'm using jpg"
local function resizeImage()
local camImg1 = display.newImage(newName..imgExtension,system.TemporaryDirectory)--This will display the photo which was taken by the camera ... we do this to get the actual size of the image
camImg1.x=display.contentCenterX
camImg1.y=display.contentCenterY
local imgW = camImg1.width--actual width of the image taken by the camera
local imgH = camImg1.height--actual height of the image taken by the camera
local factor=(display.contentWidth/imgW) --- we create a factor based on width because we want the image to fit in the phones screen width (portrait) regardless of the actual generated by the camera which depends on camera resolution
display.remove(camImg1)--we got the sizes ... remove the image
camImg1=nil
local camImg1 = display.newImageRect(newName..imgExtension,system.TemporaryDirectory, imgW * factor, imgH * factor)--display the image again to fit phone screen this time
camImg1.x=display.contentCenterX
camImg1.y=display.contentCenterY
local G1=display.newGroup()
G1:insert( camImg1 )--place image in a display group to make use of the save function below
newName2=newName.."2"..imgExtension
display.save( G1, { filename=newName2, baseDir=system.TemporaryDirectory, captureOffscreenArea=true, backgroundColor={1,1,1,0}, jpegQuality=0.8 } )--0.8 will reduce image size as well
display.remove(camImg1)
camImg1=nil
display.remove(G1)
G1=nil
local camImg2 = display.newImage(newName2,system.TemporaryDirectory)--display the saved image result --- or send it to someone -- or do whatever you want to do .... personally i'm reducing image sizes and sending them to a server in my apps
camImg2.x=display.contentCenterX
camImg2.y=display.contentCenterY
end
local function onComplete( event )
local photo = event.target
resizeImage()
end
local function appPermissionsListener( event )
for k,v in pairs( event.grantedAppPermissions ) do
if ( v == "Storage" ) then
camImg_Tap()
elseif ( v == "Camera" ) then
camImg_Tap()
end
end
end
local function camImg_Tap()
if ( system.getInfo( "environment" ) == "simulator" ) then
return true
end
if media.hasSource( media.Camera ) then
local destDir = system.TemporaryDirectory -- Location where the file is stored
local date = os.date( "*t" ) -- Returns table of date & time values
newName=date.year.."_"..date.month.."_"..date.day.."_"..date.hour.."_"..date.min.."_"..date.sec --- create a random name based on time
media.capturePhoto( { listener=onComplete,destination = {baseDir = system.TemporaryDirectory, filename = newName..imgExtension, type = "image"} } )
else
local options =
{
appPermission = "Camera",
urgency = "Normal",
listener = appPermissionsListener,
rationaleTitle = "Camera access required",
rationaleDescription = "Camera access is required to take photos. Re-request now?",
settingsRedirectTitle = "Alert",
settingsRedirectDescription = "Without the ability to use Camera, this app cannot properly function. Please grant camera access within Settings."
}
native.showPopup( "requestAppPermission", options )
local options1 =
{
appPermission = "Storage",
urgency = "Normal",
listener = appPermissionsListener,
rationaleTitle = "Storage access required",
rationaleDescription = "Storage access is required to take photos. Re-request now?",
settingsRedirectTitle = "Alert",
settingsRedirectDescription = "Without the ability to use Storage, this app cannot properly function. Please grant storage access within Settings."
}
native.showPopup( "requestAppPermission", options1 )
end
end
circle1 = display.newCircle(display.contentCenterX,display.contentCenterY,100)
circle1:setFillColor(1)
circle1:addEventListener("tap", camImg_Tap)