I need to find a way that how to get the size of an image taken from the camera programmatically, as in my app I am taking some images and then need to upload them. It works fine but the images that I take from too near are as of larger size, fail to upload.
Is there anyway that I reduce the size of those images programmatically and upload them?
Hi there,
You may want to look into Lua File System, which is an extension to Lua’s standard file library that provides some additional functionality. It seems like one of the things it can do is tell you the size of the file: http://keplerproject.github.io/luafilesystem/manual.html#reference.
As for reducing the size, if the camera captures a JPEG, then I would think the only way you could reduce the size is by lowering the quality or reducing the resolution. There may be a Lua library to resample a JPEG at a lower quality somewhere out there. Alternatively, to save at a lower resolution, you might be able to use display.save() (http://docs.coronalabs.com/api/library/display/save.html). You could load the image, scale it to smaller dimensions, and then resave it.
By the way, I haven’t tried either of these things myself, just wanted to pass along the ideas.
- Andrew
you can resize the image using display.save(). However, there are a couple of critical bugs coronaLabs hasn’t fixed that you *may* run into, depending on your use case.
To resize using display.save, the image must be displayed onscreen. From there, the display.save will capture in device pixels (see docs), not your virtual screen pixels, so you can get really large images unless you scale as auk said (scale the image/group by displayContentScaleX, display.contentScaleY to get the size you want).
However, if you call display.save using a mask to save a jpg file, you will get a large (3 pixel / mask edge size) solid white border on iOS platforms (corona bug #22962). Also, ALL jpg files created by display.save are basically gimped on Android. A retina level (640x960) image on iOS is approx 700k. On Android, approx 70k - with MUCH lower quality. There was an attempted fix for this a month or so ago, but either it regressed, or never really worked.
The more bug free route to resize your images is to go with png, they work pretty well with display.save (they’re just larger, closer to 1MB for a retina level image).
Thank you so much, now I can get the size. There is one problem I am facing here. I have noticed that images below 1.5 mb are easily uploaded, so is it a way that I make sure to resize image in such a way that automatically its size become whatever I want?
One thing that came to mind after reading your answers is, I should get back the saved image, see its size and resize and save it again unless size becomes a constant that I require(1.5mb in my case), but its too crude implementation as saving, retrieving and resizing unless got a demanding figure…
Is there a more decent way to do this that I am missing?
Hi there,
You may want to look into Lua File System, which is an extension to Lua’s standard file library that provides some additional functionality. It seems like one of the things it can do is tell you the size of the file: http://keplerproject.github.io/luafilesystem/manual.html#reference.
As for reducing the size, if the camera captures a JPEG, then I would think the only way you could reduce the size is by lowering the quality or reducing the resolution. There may be a Lua library to resample a JPEG at a lower quality somewhere out there. Alternatively, to save at a lower resolution, you might be able to use display.save() (http://docs.coronalabs.com/api/library/display/save.html). You could load the image, scale it to smaller dimensions, and then resave it.
By the way, I haven’t tried either of these things myself, just wanted to pass along the ideas.
- Andrew
you can resize the image using display.save(). However, there are a couple of critical bugs coronaLabs hasn’t fixed that you *may* run into, depending on your use case.
To resize using display.save, the image must be displayed onscreen. From there, the display.save will capture in device pixels (see docs), not your virtual screen pixels, so you can get really large images unless you scale as auk said (scale the image/group by displayContentScaleX, display.contentScaleY to get the size you want).
However, if you call display.save using a mask to save a jpg file, you will get a large (3 pixel / mask edge size) solid white border on iOS platforms (corona bug #22962). Also, ALL jpg files created by display.save are basically gimped on Android. A retina level (640x960) image on iOS is approx 700k. On Android, approx 70k - with MUCH lower quality. There was an attempted fix for this a month or so ago, but either it regressed, or never really worked.
The more bug free route to resize your images is to go with png, they work pretty well with display.save (they’re just larger, closer to 1MB for a retina level image).
Thank you so much, now I can get the size. There is one problem I am facing here. I have noticed that images below 1.5 mb are easily uploaded, so is it a way that I make sure to resize image in such a way that automatically its size become whatever I want?
One thing that came to mind after reading your answers is, I should get back the saved image, see its size and resize and save it again unless size becomes a constant that I require(1.5mb in my case), but its too crude implementation as saving, retrieving and resizing unless got a demanding figure…
Is there a more decent way to do this that I am missing?