Camera access and manipulating the output

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.

[lua]local onComplete = function(event)
local photo = event.target
print( "photo w,h = " … photo.width … “,” … photo.height )
end
media.show( media.Camera, onComplete )[/lua]

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! [import]uid: 6084 topic_id: 6041 reply_id: 306041[/import]

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:

Fri Feb 4 16:51:47 unknown CamTest[11173] <warning>: [Warning] Deregistering for sleep notifications when we have not registered<br>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<br>Fri Feb 4 16:51:47 unknown UIKitApplication:com.beyondthetech.CamTest[0x313d][11173] <notice>: Camera returned an image<br>Fri Feb 4 16:51:47 unknown UIKitApplication:com.beyondthetech.CamTest[0x313d][11173] <notice>: event name: completion<br>Fri Feb 4 16:51:47 unknown UIKitApplication:com.beyondthetech.CamTest[0x313d][11173] <notice>: target: table: 0x291a40<br>Fri Feb 4 16:51:47 unknown UIKitApplication:com.beyondthetech.CamTest[0x313d][11173] <notice>: w,h = 382,512<br>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… [import]uid: 6084 topic_id: 6041 reply_id: 20670[/import]

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

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. [import]uid: 3953 topic_id: 6041 reply_id: 20709[/import]

Thank you, MarkHenryC. It’s darn blurry, but at least it’s centered. Hopefully they’ll fix the resolution thing, it looks pretty bad. [import]uid: 6084 topic_id: 6041 reply_id: 20725[/import]

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

c [import]uid: 24 topic_id: 6041 reply_id: 20731[/import]

Thanks for the response, Carlos. If you can throw it into an upcoming daily build, I would love to test it out. [import]uid: 6084 topic_id: 6041 reply_id: 20734[/import]

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? [import]uid: 6084 topic_id: 6041 reply_id: 22029[/import]

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. [import]uid: 8045 topic_id: 6041 reply_id: 22296[/import]

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

C. [import]uid: 24 topic_id: 6041 reply_id: 22406[/import]

@Carlos, thanks for looking into this. Much appreciated. [import]uid: 8045 topic_id: 6041 reply_id: 22475[/import]

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.
[import]uid: 6084 topic_id: 6041 reply_id: 22705[/import]

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 [import]uid: 24 topic_id: 6041 reply_id: 23031[/import]

Everyone stand back!

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

ok. Carry on.

[import]uid: 8045 topic_id: 6041 reply_id: 23084[/import]

[lua]-- 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);[/lua]
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. :slight_smile:

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. [import]uid: 6084 topic_id: 6041 reply_id: 23197[/import]

@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:

[lua]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)
[import]uid: 10211 topic_id: 6041 reply_id: 23319[/import]

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? [import]uid: 6084 topic_id: 6041 reply_id: 23658[/import]

@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.

[import]uid: 8045 topic_id: 6041 reply_id: 23664[/import]

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. [import]uid: 13560 topic_id: 6041 reply_id: 23800[/import]

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 [import]uid: 24 topic_id: 6041 reply_id: 24133[/import]

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. [import]uid: 12108 topic_id: 6041 reply_id: 27860[/import]