Free Code to reduce image size taken by camera or gallery

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

    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

    local camImg1 = display.newImageRect(newName..imgExtension,system.TemporaryDirectory, imgW * factor, imgH * factor)--display the image again to fit phone screen this time

    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 G1, { filename=newName2, baseDir=system.TemporaryDirectory, captureOffscreenArea=true, backgroundColor={1,1,1,0}, jpegQuality=0.8  } )--0.8 will reduce image size as well


    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
local function onComplete( event )
    local photo =
local function appPermissionsListener( event )
    for k,v in pairs( event.grantedAppPermissions ) do
        if ( v == "Storage" ) then
        elseif ( v == "Camera" ) then
local function camImg_Tap()
  if ( system.getInfo( "environment" ) == "simulator" ) then
    return true
  if media.hasSource( media.Camera ) then        
      local destDir = system.TemporaryDirectory  -- Location where the file is stored
      local date = "*t" )    -- Returns table of date & time values

      newName=date.year.."_""_""_""_""_" --- create a random name based on time

      media.capturePhoto( { listener=onComplete,destination = {baseDir = system.TemporaryDirectory, filename = newName..imgExtension, type = "image"} } )
      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 )

circle1 = display.newCircle(display.contentCenterX,display.contentCenterY,100)
circle1:addEventListener("tap", camImg_Tap)

Save Images From Camera In Smaller (2.3 KB)


Nice. I also use the to make the images smaller, but the size/resolution depends on the mobile-device screen resolution (xxxhdpi will be bigger than xhdpi).

Someone previously reported (and I tested) that the camera function wasn’t working some time ago, here : Sample: Camera app - photo doesn't show

Hey this looks pretty awesome and like exactly what I need. I’m a little new to all this. Would you be able to explain how to implement this in existing code? For example. I have a page where the user takes a photo and it displays on the screen, I then want to use your code to reduce the image size like your code does (not scale). I guess I’m a little unsure exactly how to implement your code. I appreciate it!

i sent you a full working example … just create a new project

paste the code in main.lua and run it … i also attached the entire project

if you click the circle at the middle it will open the camera and take the picture, reduce its actual size and finally view it … but the real use comes when you send this photo to a server via network.upload

to implement this you need a web service on a server to accept your file … and there has to be a purpose for that in a real app…

Hey awesome! I really appreciate it. Did you message me the full working version? I guess I don’t see anything. Thanks again, I look forward to seeing exactly how it works.

1 Like