Camera access and manipulating the output

27 replies [Last post]
BeyondtheTech
User offline. Last seen 16 hours 25 min ago. Offline
Joined: 14 Apr 2010

Someone started a topic in the newbie section, and I chimed in because I was coincidentally working on using the camera API in my app.

http://developer.anscamobile.com/forum/2011/02/03/camera-support

The bottom line is that, based on the original sample code below from http://developer.anscamobile.com/reference/video, I am trying to take the image and save it as a JPG/PNG in order to upload it to my server.

1
2
3
4
5
local onComplete = function(event)
   local photo = event.target
   print( "photo w,h = " .. photo.width .. "," .. photo.height )
end
media.show( media.Camera, onComplete )

If photo contains the image, how to I get to redisplay it at will? I tried background = display.newImage ( photo )but I got a "proxy" error. Also, I tried display.save to save it to a JPEG in the temporary directory, then using background=display.newImage.. to retrieve it, and the image quality is so blocky and low-resolution.

Is the Camera API really usable, am I doing it wrong? Please help!

Replies

BeyondtheTech
User offline. Last seen 16 hours 25 min ago. Offline
Joined: 14 Apr 2010

Starting from scratch, I used the sample Camera app here and ran it on my iPhone 4.

This is the result.

http://www.flickr.com/photos/beyondthetech/5416430625/

Here's the Console Log from my iPhone 4:

1
2
3
4
5
6
7
Fri Feb  4 16:51:47 unknown CamTest[11173] <Warning>: [Warning] Deregistering for sleep notifications when we have not registered
Fri Feb  4 16:51:47 unknown UIKitApplication:com.beyondthetech.CamTest[0x313d][11173] <Notice>: AudioStreamBasicDescription:  2 ch,  44100 Hz, 'lpcm' (0x00000C2C) 8.24-bit little-endian signed integer, deinterleaved
Fri Feb  4 16:51:47 unknown UIKitApplication:com.beyondthetech.CamTest[0x313d][11173] <Notice>: Camera  returned an image
Fri Feb  4 16:51:47 unknown UIKitApplication:com.beyondthetech.CamTest[0x313d][11173] <Notice>: event name: completion
Fri Feb  4 16:51:47 unknown UIKitApplication:com.beyondthetech.CamTest[0x313d][11173] <Notice>: target: table: 0x291a40
Fri Feb  4 16:51:47 unknown UIKitApplication:com.beyondthetech.CamTest[0x313d][11173] <Notice>: w,h = 382,512
Fri Feb  4 16:51:47 unknown UIKitApplication:com.beyondthetech.CamTest[0x313d][11173] <Notice>: AudioStreamBasicDescription:  2 ch,  44100 Hz, 'lpcm' (0x00000C2C) 8.24-bit little-endian signed integer, deinterleaved

As you can see, the image capture is registering at a paltry 382x512 from the iPhone 4's 5.0 megapixel camera. Something's clearly wrong here...

MarkHenryC
User offline. Last seen 10 weeks 1 day ago. Offline
Joined: 11 Nov 2009

Can't help you with the resolution thing, but you need to centre the image:

1
2
3
photo.x = display.contentWidth/2
photo.y = display.contentHeight/2
display.save(photo, ....)

Do a removeSelf after that if you don't want it visible straight away.
Then you can load it as a regular newImage.

BeyondtheTech
User offline. Last seen 16 hours 25 min ago. Offline
Joined: 14 Apr 2010

Thank you, MarkHenryC. It's darn blurry, but at least it's centered. Hopefully they'll fix the resolution thing, it looks pretty bad.

carlos m. icaza's picture
carlos m. icaza
User offline. Last seen 6 weeks 5 days ago. Offline
Alumni
Joined: 22 Jun 2009

Am aware of the issue - it bugs me too ! I'll c what we can do

c

BeyondtheTech
User offline. Last seen 16 hours 25 min ago. Offline
Joined: 14 Apr 2010

Thanks for the response, Carlos. If you can throw it into an upcoming daily build, I would love to test it out.

BeyondtheTech
User offline. Last seen 16 hours 25 min ago. Offline
Joined: 14 Apr 2010

I'm still tinkering with this issue to see if I can make a workaround, but I think the problem also lies with the display.save command.

I tried to give up on using the Camera (media.Camera) and went for the Photo Library (media.PhotoLibrary) instead. I mean, every iOS device has a Photo Library and not a camera, right? (Out of curiosity, what happens if you try to open the camera on a device that doesn't have one? Do we have a detector for that, perhaps one to be put in a system.getInfo("camera") result?

The image I chose showed up properly on screen on the device, but when it went to capture it, it did a poorly compressed copy of the upper-left corner of the screen.

I tried display.save(photo,... as well as display.save(display.getCurrentStage()... before removing the image properly displayed on my device, and they both produced the same crappy results in the final JPEG.

One thing of note is that the capture of display.getCurrentStage is 320x480 on my iPhone 4... I don't have an older device to test, but maybe it's working for older devices and just needs to be tweaked for the Retina display?

luna
User offline. Last seen 42 weeks 3 days ago. Offline
Joined: 4 Aug 2010

This is the issue I've been hacking away at the past few days. I have client that wants a camera and at this point I will need to switch to another API if this doesn't get resolved soon. At such a low resolution, the camera is pretty much a toy right now.

If I find anything I'll be sure to post it.

carlos m. icaza's picture
carlos m. icaza
User offline. Last seen 6 weeks 5 days ago. Offline
Alumni
Joined: 22 Jun 2009

It is a known issue with Corona and we are looking at this as I type away...

C.

luna
User offline. Last seen 42 weeks 3 days ago. Offline
Joined: 4 Aug 2010

@Carlos, thanks for looking into this. Much appreciated.

BeyondtheTech
User offline. Last seen 16 hours 25 min ago. Offline
Joined: 14 Apr 2010

I hope this can get resolved soon, this is one of the last two pieces of the puzzle for my game. I'm throwing everything but the kitchen sink into my first title.

carlos m. icaza's picture
carlos m. icaza
User offline. Last seen 6 weeks 5 days ago. Offline
Alumni
Joined: 22 Jun 2009

the latest daily build has a fix for higher resolution jpeg image.

am going to fix for saving the entire display later today/tomorrow

c

luna
User offline. Last seen 42 weeks 3 days ago. Offline
Joined: 4 Aug 2010

Everyone stand back!

[start]
[happy dance]
[happy dance]
[happy dance]
[done]

ok. Carry on.

BeyondtheTech
User offline. Last seen 16 hours 25 min ago. Offline
Joined: 14 Apr 2010

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
-- camera capture
local onCameraComplete = function (event)
        local photo = event.target
        if photo then
                photo.x = display.contentWidth / 4; photo.y = display.contentHeigh / 4;
                photo:scale(0.5,0.5)
                display.save( display.currentStage, "imageCapture.jpg",system.DocumentsDirectory)               
                debug ("Captured photo, dimensions: " .. photo.width .. "x" .. photo.height);
                photo:removeSelf()
                local alert = native.showAlert ("Thank you!","Your image has been captured.", { "OK" } );
        else
                debug ("No photo was taken");
        end
end
 
media.show(media.Camera, onCameraComplete);

The code above works in my game using build 299, and the image looks great on my iPhone 4.

But I have a few things of note:

  1. It doesn't look right in the Simulator, as it's shifted up and left.
  2. My iPhone 4's console output shows that it captured photo at 328x512 again, but of course, I saved display.currentStage, and the actual image it saved on the device ended up as 320x480.
  3. The code doesn't work on the iPad. I didn't see the console output, but it just went back into my game. At least it didn't crash the app entirely, but there's yet no way to detect for a camera. I'd prefer it to revert to the Photo Library if Corona can't find a camera.
  4. I don't have a pre-Retina display device, so I don't know how these tweaks will result on an iPhone/3G/3GS or their camera-enabled counterparts. Can someone test on their device?
  5. I'm accessing the front camera as well on my iPhone 4, and the output is centered and complete, too. :)

So, to clarify Carlos' announcement above, I'm not getting a higher-resolution image, but a higher-quality rendering and a complete image. I believe the resolution is due to my settings in config.lua.

I'm doing a screen capture instead of saving the contents of variable photo because I was getting even funkier results.

w2md's picture
w2md
User offline. Last seen 56 min 56 sec ago. Offline
Joined: 18 Oct 2010

@BeyondTheTech:

I just tested it out on the iPhone 3G and it looks significantly better with the new build (I'm using build 300). It's not anywhere near the grainy, pixelated images I was getting before.

Also, your method of using display.currentStage when saving the image improved the image clarity and sharpness as well (as opposed to using display.save(event.target, "imageCapture.jpg", system.DocumentsDirectory). And using the scaling adjustments completely fixed every scaling issue I had.

Thanks So Much!! And Thanks to Ansca and Carlos too!

Here's the relevant parts of the code I'm using:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
local t = event.target
local scaleX, scaleY = display.contentScaleX, display.contentScaleY
print("scaleX, scaleY: ", scaleX, scaleY)
if scaleX ==.5 and scaleY == .5 then 
        print("this is displaying on a retina display... iPhone 4G")
        t.x = display.contentWidth / 4
        t.y = display.contentHeight / 4
        t:scale(0.5,0.5)
elseif scaleX == 1 and scaleY == 1 then 
        print("this is displaying on an iPhone 3G.")
        --centering image
        t.x = display.viewableContentWidth/2
        t.y = display.viewableContentHeight/2
else
        print("this is displaying on some other device.")
        --centering image
        t.x = display.viewableContentWidth/2
        t.y = display.viewableContentHeight/2                   
end 
 
display.save( display.currentStage, newPhotoName,system.DocumentsDirectory)               

BeyondtheTech
User offline. Last seen 16 hours 25 min ago. Offline
Joined: 14 Apr 2010

That's a neat trick, w2md. I always wanted to know how my app can more closely determine what device it's running on.

The iPad happens to return a value of 0.46875 (well, according to the Simulator so far), so you can choose to not grab from media.Camera (that the iPad doesn't have), but media.PhotoLibrary instead - every iOS device has one of those.

HOWEVER...

There appears to be another bug, or at least when I'm using media.PhotoLibrary on the iPad. If I cancel or tap out of the photo selection, my application appears to hang for a long time. It doesn't crash, but it's unusable for the app and would likely get rejected by Apple's review team.

I'd use the iPad value to hide the Camera import feature entirely, but we all know that the iPad 2 will be coming out and it will sport multiple cameras, so they'll likely be crying foul that they can't use the feature.

Back to square 2?

luna
User offline. Last seen 42 weeks 3 days ago. Offline
Joined: 4 Aug 2010

@BeyondtheTech

re:"There appears to be another bug, or at least when I'm using media.PhotoLibrary on the iPad. If I cancel or tap out of the photo selection, my application appears to hang for a long time"

I noticed that as well. The higher resolution is a marked improvement. The issue now is trying to capture the 512x384 size as it appears that is the largest it will allow at this time. I can easily capture the screen with the hi-res but I want the full 512.

lano78's picture
lano78
User offline. Last seen 4 hours 39 min ago. Offline
Joined: 4 Jan 2011

Has anyone tried to add custom camera button, zoom, flash, front/back camera and HDR so it doesn't look like the standard camera app?

Is there a way to implement HDR with corona or is that feature not ready?

I'm curious about the camera too, can someone sum up all the bits and pieces of camera code so we get an updated and functional sample with the save to photo library implemented?

Do we have to use the "start screen" to launch the camera, can't it just go directly to the app and the just have a button that takes the user to a LightBox preview?

Video recording is not implemented in corona is it? From what I can understand is only audio recording available.

carlos m. icaza's picture
carlos m. icaza
User offline. Last seen 6 weeks 5 days ago. Offline
Alumni
Joined: 22 Jun 2009

There appears to be another bug, or at least when I'm using media.PhotoLibrary on the iPad. If I cancel or tap out of the photo selection, my application appears to hang for a long time. It doesn't crash, but it's unusable for the app and would likely get rejected by Apple's review team.

----------

Can you enter this in the bug form?

If can't find it. send email to support and me

c

jhocking
User offline. Last seen 8 weeks 5 days ago. Offline
Joined: 4 Dec 2010

I am glad camera images are working better in the daily builds, because I was planning to look at this functionality in my next project.

drewvy
User offline. Last seen 6 weeks 2 days ago. Offline
Joined: 21 Feb 2011

I hope the camera issues gets fixed soon. I need access to the camera and I really want to use this SDK.

chris.slee5's picture
chris.slee5
User offline. Last seen 33 weeks 6 days ago. Offline
Joined: 11 Feb 2011

I must have missed it, but the camera support for Android dropped from the roadmap. I see from the api page it isn't on Android.

When can the Android platform use the camera?

Thanks

carlos m. icaza's picture
carlos m. icaza
User offline. Last seen 6 weeks 5 days ago. Offline
Alumni
Joined: 22 Jun 2009

must have been dropped by accident.

go ahead and submit a request ;-) on the road map page.

c.

ciprian.filipas
User offline. Last seen 3 weeks 4 days ago. Offline
Joined: 23 May 2011

Hello,

I'm a bit newbie on using the SDK. I am trying to save a picture captured with the camera. When using display.save() I can only save in the 3 predefined folders.

Is there a way of viewing that image in the photoLibrary?
Or what should I use for saving it there?

Thanks.

paingpyi
User offline. Last seen 14 weeks 1 day ago. Offline
Joined: 16 Sep 2010

Any update for camera video? I just want use custom frame and buttons for picture taking.
Is that possible with current builds?

paingpyi
User offline. Last seen 14 weeks 1 day ago. Offline
Joined: 16 Sep 2010

I now have to leave Corona which I'm not happy with, just because I can't use custom layout for camera. Do u have plan for next builds?

akviby's picture
akviby
User offline. Last seen 1 week 6 days ago. Offline
Joined: 20 Jan 2011

Hey
We just got a huge contract and inside that app we need to be able to start the camera in a small window with a little cross on it so that the customer can focus on the accident and then when they press a button we want to grab the image and add some images on top of the live camera image.

This is crucial for the app and we must get this to work inside Corona, I don't want to build this huge project in Titanium Appcelerator which has the support.

So please Carlos & co... maybe some camera addings to the api the upcoming month?

ZANUKA's picture
ZANUKA
User offline. Last seen 3 weeks 5 hours ago. Offline
Joined: 20 Nov 2011

and what about video capture support?

Viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.