Saving camera picture

Hi Guys,

Thanks for an amazing SDK. Having fun so far.

Regarding question. My current setup for taking pictures include following.

  1. Capture image using media.show( )
  2. [I want to avoid this] Once image is captured, do a display.save() to take a screenshot.

For #2, is there a way I can tell media.show() to save image in a temp or documents directory? Doing a screenshot is giving me very low quality pictures and I want to depend on the camera quality than screenshotting. Is it possible?
thanks

[import]uid: 7499 topic_id: 21784 reply_id: 321784[/import]

We’ve recently added the option to media.show() to save to a file in daily build #724. It’s not documented yet since it is fairly new, but what you would do is this…

[lua]local onComplete = function(event)
if event.completed then
– Camera shot was successfully taken.
else
– User canceled out.
end
end
local filePath = { baseDir = system.TemporaryDirectory, filename = “CameraShot.jpg” }
media.show( media.Camera, onComplete, filePath )[/lua]

Adding that 3rd argument tells media.show() to save to file instead of displaying the camera shot as a display object. The advantage being that the camera shot will be saved at full camera resolution, which display.save() cannot do since it is limited to screen resolution. Also, the Lua listener’s “event.target” property will be set to nil in this case. If you omit the 3rd file argument, then media.show() will do the old behavior.

One more thing, this feature is currently only supported on iOS and Android. It is not supported on the Corona Simulator yet.

I hope this helps! [import]uid: 32256 topic_id: 21784 reply_id: 86497[/import]

Awesome. Thanks a lot. Just the thing I was searching for.

thanks [import]uid: 7499 topic_id: 21784 reply_id: 86501[/import]

On iOS, this works pretty good.

I am having hard time getting this to work on Android. Do I have to do anything special on Android?

thanks

I tried both of these:

local photo\_name = "photo\_new.jpg" --local filePath = { baseDir = system.DocumentsDirectory, filename = photo\_name } local filePath = system.pathForFile(photo\_name, system.DocumentsDirectory) media.show(media.Camera, stageImage, filePath) [import]uid: 7499 topic_id: 21784 reply_id: 89717[/import]

This definitely works on Android. Perhaps you forgot to set the “android.permission.CAMERA” permission in your build.settings file? Please see sample app “Media\Camera” that is included with the Corona SDK for more details. [import]uid: 32256 topic_id: 21784 reply_id: 89718[/import]

cant seem to get this to work on a device, the image never shows up. code is like this:

[lua]local onComplete = function(event)
if event.completed then
event.target:removeSelf() – remove the image grab from camera
local img = display.newImage(“CameraShot.jpg”, system.DocumentsDirectory) – display image that was saved
img.x = contentWidth/2
img.y = contentHeight/2
else
– User canceled out
end
end

local filePath = { baseDir = system.DocumentsDirectory, filename = “CameraShot.jpg” }
media.show( media.Camera, onComplete, filePath )[/lua] [import]uid: 17969 topic_id: 21784 reply_id: 99219[/import]

borgb,

not sure if it is something to do with the version. Mine works perfectly.

I have something like this:

  
local staging\_image = display.newImage(photo\_name, system.TemporaryDirectory )  
staging\_image.x = display.contentWidth/2  
staging\_image.y = display.contentHeight/2  
group:insert(staging\_image)  
  

Also, any reason why you are removing the event?
[import]uid: 7499 topic_id: 21784 reply_id: 99431[/import]

as far as I have understood it the event is a screengrab of the camera and I dont really need that. I have tried without removing it but nothing gets saved then either. If I do display.save it works as it should and I can save the image and all. but would like a little bigger file to work with.

I am running 776 daily build atm. [import]uid: 17969 topic_id: 21784 reply_id: 99436[/import]

borgb,

What is the make and model of the Android device you are using?

Also, can you try running sample app “media/camera” that is included with the Corona SDK on your Android device to see if it works?

If the sample app does not have access to the camera, then it will display a warning message indicating so. It checks for camera support by calling the Lua function…
[lua]media.hasSource(media.Camera)[/lua]
[import]uid: 32256 topic_id: 21784 reply_id: 99564[/import]

not Android, trying it out on Iphone 4s.

I have access to the camera, no problem. I can take a photo and use display.save and get a file from that I can use. I guess it would be good enough but I would really like the full resolution photo file as well. Or at least that the image that gets taken also gets saved in the users photo album as a normal photo would. [import]uid: 17969 topic_id: 21784 reply_id: 99571[/import]

Calling media.show() with a file path like below will save the image to file at full resolution. If you want to then display the saved photo at full resolution, then you will need to call display.newImage() function as shown below…

[lua]local onComplete = function(event)
if event.completed then
– Camera shot was successfully taken.
– Display at full resolution by passing in true.
display.newImage(“CameraShot.jpg”, system.TemporaryDirectory, true)
else
– User canceled out.
end
end
local filePath = { baseDir = system.TemporaryDirectory, filename = “CameraShot.jpg” }
media.show( media.Camera, onComplete, filePath )[/lua]

The full resolution option for the display.newImage() is documented via the link below…
http://developer.anscamobile.com/reference/index/displaynewimage

Please note that the displayed image will still be capped based on the max OpenGL texture size limit, which is typically 2048x2048 pixels on most iOS devices… but the image file will be the original size of the photo in case you want to upload it somewhere.

Also note that the media.show() function will not save to file in the Corona Simulator. We have not added support to the simulator yet, but this feature does work on iOS and Android.
[import]uid: 32256 topic_id: 21784 reply_id: 99586[/import]

@Joshua Quick

Awesome, that worked! Thanks! Im guessing it worked all the time really, I just though I only got the screenshot and not the file. Still got alot to learn! :slight_smile:

Is there also a way to save the image to the users photo album?
I saw there was a: object = display.captureScreen( saveToAlbum ) but that would only save a screenshot right? [import]uid: 17969 topic_id: 21784 reply_id: 99714[/import]

Correct. The only way to save images to the photo library in Corona is via our screenshot APIs…
[lua]display.captureScreen(true)[/lua]
[lua]display.capture(myDisplayObject, true)[/lua]

We do have a feature request written up to be able to export an image file to the photo library so that you can maintain the full resolution of the image. I believe that is the feature that you are after, right? It’s on our to-do list, but it’s not scheduled yet.
[import]uid: 32256 topic_id: 21784 reply_id: 99849[/import]

Great, thanks for the reply. I am wondering if I should only let the user import from photo album and just take the photo with the camera as usual. Guess I will try and see abit more and decide when its all done, maybe you will get there before me :slight_smile:

Anyway, was playing abit with taking photos and noticed that no matter if I took it in landscape or portrait it would always report it back with the same image width. is there a way to detect which way the user took the image? I would like to rotate the image so its the right way like when you view them in the photo album. This also seems to be the same when importing from album… but havent tested much there yet. [import]uid: 17969 topic_id: 21784 reply_id: 99867[/import]

The orientation of the photo is stored in the image file’s EXIF metadata. There is a bug in the iOS version of Corona where the EXIF orientation is ignored when loading images. The Android version of Corona does not have this problem.

Unfortunately, the only work-around at the moment is to not save the camera shot to file and show the image onscreen instead… which means that you will lose the full resolution of the image within Corona. [import]uid: 32256 topic_id: 21784 reply_id: 99938[/import]

ok, hope you get that fixed by the time Im ready to release then :wink: Else I have to either put it on hold or go with the low res version. If I only let them import from album they will at least have a copy of the high res for themselves.

Thanks again for the quick replys!
[import]uid: 17969 topic_id: 21784 reply_id: 100002[/import]

Any way you can save it to the file name of the file that the Camera sets it to. Or if you choose an image from the Photo Library, save it to that name? Or at least get the file name. I want to upload the photo to a server, but have it named as the user had it on their phone. [import]uid: 60471 topic_id: 21784 reply_id: 101184[/import]

alabelson,

The selected photo file name is currently unaccessible in Corona. Even if we did provide you the path and file name to the photo, your app would not have permission to overwrite files owed by other apps. In this case, the Camera is its own app and iOS would definitely NOT allow this.

I’ve found on Android, this is hit or miss. On some phones I had permission to overwrite the file and others I didn’t. The inconstancies across different Android devices just boggles my mind. This is partly why we implemented our own camera view in Corona. [import]uid: 32256 topic_id: 21784 reply_id: 101341[/import]

Thank you for your response.

I’m not looking to overwrite the file on the actual device. But if my app lets someone choose a photo from their photo library to upload to a server, I’d like to save the image on the server side with the same name it is on the device. Same goes for someone that just takes a picture. I’d like to be able to upload that picture with the same name (i.e. IMG_0194.JPG) instead of having to ask the user to rename it, or generating a name dynamically. [import]uid: 60471 topic_id: 21784 reply_id: 101347[/import]

Fair enough. I’ve written this up as a feature request to retrieve the original name of the photo. The only limiting factor here is iOS, which may not give you access to that file’s name due to sandboxing. In which case, the only work-around is to retrieve the file’s EXIF description which of course won’t match the file name, but at least it’s something. [import]uid: 32256 topic_id: 21784 reply_id: 101352[/import]