Opens a platform-specific interface to the device's camera or photo library. This function is asynchronous, i.e. it returns immediately so the calling code will continue to execute until the end of its scope; after that, the application will be suspended until the session is complete. By default, the image object is added to the top of the current stage. A listener is required to handle the returned display object (image).
media.show( mediaSource, listener )
local onComplete = function(event) local photo = event.target print( "photo w,h = " .. photo.width .. "," .. photo.height ) end if media.hasSource( media.Camera ) then media.show( media.Camera, onComplete ) else native.showAlert( "Corona", "This device does not have a camera.", { "OK" } ) end
mediaSource
Can be one of the following:
media.PhotoLibrarymedia.Cameramedia.SavedPhotosAlbumlistener can be either a function listener or a table listener. If a table, it must have a completion method. The event dispatched to the listener will be a completion event with the following additional property: event.target is a display image object based on the imageSource parameter. The property will be nil if the user canceled the camera or photo selection.
Nothing. "event.target" in the Listener function will contain the Display Image object.
On the Corona Simulator and Xcode Simulator, there is no camera so the API will open a finder window and allow the user to select an image file to substitute as the camera.
Note: This API (Camera and PhotoLibrary) is not support on Android devices in the current release build (591). Camera and PhotoLibrary support was added to Android starting with build 622.
+1 from me too. I'm working on a project that will need to use this feature on the iPad 2. If this hasn't been fixed yet, please add it back to your list as things have changed now that the iPad 2 has a camera. Thanks!
Please post your requests and questions in the forums. As stated below, all non-document related postings will be removed.
Thanks.
Already did and got no reply on the forums! I thought posting here will make sure someone reads it!
File it as a bug, case #6762
How can I get the location information from the photos taken, or from the photo library?
That information is not available in Corona.
Is it possible to save/reference a photo taken in an app? If so how?
Thanks.
+1
Nevermind. Figured it out.
Could you please share what you have figured out?
Thanks a lot.
Hey I would be happy to share how I did it but it would be easier for me if you showed me what you had. Also I figured out how to do it with multiple photos in a tableview, if your curious check out vAD in the created section, there should be a few promo codes left.
Thanks.
I plan to add camera function to my app,
but nothing has been done so far.
Thanks
Ok I'm going to assume your using director and different scenes. The first thing you need to do is make a button for your camera. The function for the button should look something like this:
1 2 3 4 5 6 | local function newphoto ( event ) if (event.phase == "release") then media.show( media.Camera, sessionComplete ) return true end end |
--Then you will need to save/target the photo. That should look something like this:
1 2 3 4 5 6 7 | local function sessionComplete ( event ) t = event.target local photot = display.newGroup() photot:insert(t) local baseDir = system.DocumentsDirectory display.save(photot, "photo.jpg", baseDir) end |
And then to load in another scene it should look something like this:
1 2 | local baseDir = system.DocumentsDirectory local img = display.newImage("photo.jpg",baseDir) |
This should work, I didn't 100% test it as I took previously written code and simplified it. If there are any errors let me know.
In the completion function I can detect that the event.target is nil when the user "cancels" the album/pictures dialog by touching outside the dialog box. However, I can't instantiate another media.show() dialog by calling:
media.show( media.PhotoLibrary, onComplete )
How do I cancel the previous instance of media.show() so that I can create another?
Try adding something like: if (t~=nil) then before t = event.target.
Jeremy,
I can detect that the image (event.target) is nil. The problem lies in the fact the previous call to media.show() doesn't seem to end properly so when I try to call media.show() again the selection dialog doesn't appear on screen.
I tried reloading the scene (using director) but that didn't help. The scene change didn't even happen. Upon detection of the nil event.target I can print out a debug text but nothing more than that.
Nathan.
I think I'm not understanding the problem correctly. Is the problem that you can't have a new call if you cancel the old one from the photo library?
Would it be possible to see some of the code your using?
Yes, that's correct.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | local onComplete = function(event) if event.target == nil then -- nothing works here -- can't change scene -- can't call media.show again media.show( media.PhotoLibrary, onComplete ) -- even if I use another callback function else -- process image end end media.show( media.PhotoLibrary, onComplete ) |
I think the blank media.show call is messing you up/causing your code to break. In my experience you need two functions to handle/deal with photos.
try something like:
1 2 3 4 5 6 7 8 9 10 | --show/do something with the photo local function sessionComplete ( event ) --do something with the photo end --call the Library local function showLibrary ( event ) media.show( media.PhotoLibrary, sessionComplete ) return true end |
Let me know if that works.
Jeremy,
Thanks for your help. You made me take a closer look at my code. The reason for all this trouble is me calling media.show() before returning the display group to Director. I assumed that the call to media.show() is non-blocking and because Director loaded the scene properly I thought everything worked, but this isn't the case. I added a button to initiate the call to media.show() after the fact and everything worked fine.
Thanks again.
Nathan.
Glad I could help. It took me so long to get the camera and photos to function/display the way I wanted in my most recent app.
Jeremy,
I know what you mean. I am just about finished with the profile photo cropping component of my app. It's very difficult to crop images to desired dimensions for every device. I ended up doing the actual cropping on a remote server since Corona does not yet support this feature.
Nathan.
is any news about Android support?? camera/gallery is a really important feature of a mobile application. Why isn't this feature on the Corona roadmap?? :-s
Does anybody know how to enable the front camera directly
for media.show()? The default one is the back camera.
Thanks!
Does anybody know when will add camera support on android? i want make photo on my android application, and send it via email to customer.
Camera is supported on Android in the current Daily Builds. You must be a subscriber to use it.
Is there anyway to load PNG files from photo library?
Since iOS screenshots are PNG format.
BTW, in build 691, media.show( media.PhotoLibrary, onComplete )
seems not working on iPhone devices (iPod touch 4th & iPhone 3GS).
Bug reported.
Thanks.
correct: In build 691, you can load PNG files. However sometimes it won't allow you to resave the image to TemporaryDirectory by using:
display.save( photo, fileName..".jpg", system.TemporaryDirectory )
still trying to figure out why.
@jeremyapplebaum12
Where can I find the created section? Is here on Corona? What does vAD stand for?
What do you mean by created section? VAD stands for virtual assistant director.
Its your post number 12 above
"Hey I would be happy to share how I did it but it would be easier for me if you showed me what you had. Also I figured out how to do it with multiple photos in a tableview, if your curious check out vAD in the created section, there should be a few promo codes left."
"check out vAD in the created section"
Thanks,
Looks like a great app. I am trying to finish my first here.
I am trying to build an app with real time face detection. The images/frames don't need to be captured and saved anywhere - all that's required is that faces are detected in the field of view that the camera is currently 'seeing'. Any clues on how Corona could be used to do this!?!
If you capture a photo from the library like this:
media.show(media.PhotoLibrary, onCompleteLibrary)
And access the photo via:
local photo = event.target
I'm able to access the width/height and display the photo on screen, but how do I get the name of the photo? Same goes for when you capture the photo via the Camera.
Thanks!!
Adam
Adam,
Corona does not provide the name of the photo or file name. Also, I believe iOS restricts access to the photo libraries files which may make this difficult. I believe the file name can be received on iOS 4.1 or higher, but not the older versions. In any case, I'll write this up as a feature request to be investigated later.
Hi guys, I think I have found a bug..
In its simplest terms if you just create an app with this code init..
1 2 3 4 5 | local onComplete = function(event) local photo = event.target end media.show( media.Camera, onComplete ) |
Run it on an android device
take the photo.
Photo appears in app
Lose app focus(i.e use another app.. for example take a phone call)
Go back to the above app.
The screen now just displays as "white"
I found this issue in my app googly face ... currently available on the android market.
I tried to get it on the amazon market but they failed it because of the above bug...
aidanmack,
This bug was fixed in daily build 707 back in December.
Hi Joshua,
Apologies, media call works fine.
However I have narrowed down the focus bug to the scrollview widget..
Specially when the maskfile property is used.
If you use the maskfile and the app loses focus, on returning to the app everything around it goes black when you come back to it...
anyway for me to fix this?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | local widget = require "widget" local testImage =display.newImage("bgimage.jpg") local scrollView = widget.newScrollView{ width = 256, height = 320, hideBackground = true, scrollWidth = 768, scrollHeight = 1024, maskFile="mask.png", listener = scrollViewListener } local myObject = display.newImage("itemtoSroll.png" ) scrollView:insert( myObject ) |
Please move you questions into the forums. The documentation pages are not intended for discussion or interchange. We are slowly closing all documentation pages to comments as we transition to a new doc pages.
Thanks.
won't work on ipad.
Calling media.show(media.PhotoLibrary, onComplete)
shows the albums/pictures dialog.
on the iPad there is no "cancel" button
Clicking outside the window, closes the albums dialog, but the application state won't change (it will stay suspended) until you re-launch the app.
any way around this?