Camera bug

I’m having a problem taking and displaying a picture. The sample camera program works fine on my device, so I know it’s possible to use the camera and then display the image. My code works on the emulator, but not on my device.

Can anyone find where I’ve gone wrong in my code?

Why would my code work for the emulator but not the device?


module(…, package.seeall)

local widget = require(“widget”)

function new()

    local localGroup = display.newGroup()

    

    local bg = display.newRect(0, 0, display.contentWidth, display.contentHeight)

    bg:setFillColor(240, 240, 240)

    

    local commentLabel = display.newText(“Comments:”, 10, display.contentHeight - 120, Helvetica, 14)

    commentLabel:setTextColor(0, 0, 0)

    

    local commentBox = native.newTextField(10, display.contentHeight - 100, display.contentWidth - 20, 90)

    

    local function sendInEmail(event)

        local options = 

        {

            to = {“john@doe.com”},

            subject = “Test email”,

            body = commentBox.text,

            attachment = {filename = image, type = “image”},

        }

        native.showPopup(“mail”, options)

    end

    

    local function showPicture(event)

        local image = event.target

        

        if image then

            image:scale(display.contentWidth/display.contentHeight, display.contentWidth/display.contentHeight)

            image.x = display.contentWidth/2

            image.y = display.contentHeight/2-50

            

            local button_send = widget.newButton

            {

                left = display.contentWidth/2 - 60,

                top = display.contentHeight - 160,

                width = 120,

                height = 40,

                id = “button_2”,

                label = “Send”,

                fontSize = 14,

                onEvent = sendInEmail,

            }

                                

        end

        

    end

    

    

    local function takePicture(event)

        if media.hasSource(media.Camera) then

            media.show(media.Camera, showPicture)

        else 

            native.showAlert(“Corona”, “Camera not found”)

        end

        

    end

    

    local button_takepic = widget.newButton

    {

        left = display.contentWidth/2 - 75,

        top = 30,

        width = 150,

        height = 40,

        id = “button_1”,

        label = “Take Picture”,

        fontSize = 14,

        onEvent = takePicture,

    }

        

    

    localGroup:insert(bg)

    localGroup:insert(commentLabel)

    localGroup:insert(commentBox)

    localGroup:insert(button_takepic)

    return localGroup

    

end

Hi @aaron34.  While you posted code which is good (it would have been better had you put the code inside [lua] and [/lua] tags (no spaces inside the brackets), your description of your problem is pretty vague.  I don’t know what problem you are having other than “it doesn’t work on device”.  The “on device” does help me offer some generic suggestions, but without knowing what “it doesn’t work” means we can’t get very specific.

In general there are two most frequent causes of “It works in the simulator but not on device”.   The first is you are trying to access some file name with a different case than what is really there.  Trying to use director to load a scene like:

      director.loadScene(“BillyBob”)

will work on device if the lua file for that scene is “billybob.lua” (all lower case) but fail miserably on device because the device is case sensitive.  Please check every filename you access, image, sound, require and director scene names to make sure you don’t have a mis-match.

The second most common cause is an error in your build.settings file.  There are no reporting of syntax errors and if it’s incorrect it potentially wont work on device.

You also didn’t say what device you are using. If you are doing Android builds, the Camera requires a couple of extra permissions in your build.settings to go along with your existing INTERNET permission:

settings =
{
   android =
   {
      usesPermissions =
      {
          “android.permission.CAMERA”,
          “android.permission.WRITE_EXTERNAL_STORAGE”,
      },
   },
}

Make sure you have those two if you want to use the camera on Android.

The specific problem that I’m having is this:

On the emulator I can take a picture (with my built in camera) and then display that image once I am done. However, if I try to do this using my device (Samsung Galaxy tablet) I can take a picture, but the image does not then display where it should once I return to the application. 

To further confuse things, when I use the sample code “Camera” application on the device, I am able to take and display a picture. 

I’m starting to think that this might have to do with the director class, but I don’t have the programming knowledge to know why this could be a problem. 

Also, thanks for the tip on the lua brackets.

Hi @aaron34,

Can you try the sample Camera app without using Director? It would be useful to see if the issue is related to Corona or the “media.show” API, or whether it’s a simple issue with how you’re implementing it with Director.

Thanks,

Brent

Hi Brent, 

I’ve tried the Camera app without using the director and it definitely works correctly. That’s why I’m so confused. Is there something about the director class that I need to do differently in order to display the image?

aaron34

Hi @aaron34.  While you posted code which is good (it would have been better had you put the code inside [lua] and [/lua] tags (no spaces inside the brackets), your description of your problem is pretty vague.  I don’t know what problem you are having other than “it doesn’t work on device”.  The “on device” does help me offer some generic suggestions, but without knowing what “it doesn’t work” means we can’t get very specific.

In general there are two most frequent causes of “It works in the simulator but not on device”.   The first is you are trying to access some file name with a different case than what is really there.  Trying to use director to load a scene like:

      director.loadScene(“BillyBob”)

will work on device if the lua file for that scene is “billybob.lua” (all lower case) but fail miserably on device because the device is case sensitive.  Please check every filename you access, image, sound, require and director scene names to make sure you don’t have a mis-match.

The second most common cause is an error in your build.settings file.  There are no reporting of syntax errors and if it’s incorrect it potentially wont work on device.

You also didn’t say what device you are using. If you are doing Android builds, the Camera requires a couple of extra permissions in your build.settings to go along with your existing INTERNET permission:

settings =
{
   android =
   {
      usesPermissions =
      {
          “android.permission.CAMERA”,
          “android.permission.WRITE_EXTERNAL_STORAGE”,
      },
   },
}

Make sure you have those two if you want to use the camera on Android.

The specific problem that I’m having is this:

On the emulator I can take a picture (with my built in camera) and then display that image once I am done. However, if I try to do this using my device (Samsung Galaxy tablet) I can take a picture, but the image does not then display where it should once I return to the application. 

To further confuse things, when I use the sample code “Camera” application on the device, I am able to take and display a picture. 

I’m starting to think that this might have to do with the director class, but I don’t have the programming knowledge to know why this could be a problem. 

Also, thanks for the tip on the lua brackets.

Hi @aaron34,

Can you try the sample Camera app without using Director? It would be useful to see if the issue is related to Corona or the “media.show” API, or whether it’s a simple issue with how you’re implementing it with Director.

Thanks,

Brent

Hi Brent, 

I’ve tried the Camera app without using the director and it definitely works correctly. That’s why I’m so confused. Is there something about the director class that I need to do differently in order to display the image?

aaron34

I’m having exactly the same problem now. have you found the solution? if yes, please kindly share.

I believe that it was a problem with compatibility between the “widget” class and “director” class. I’ve switched to the storyboard class (instead of the director class), which seems to have solved the problem. The director class was created by a corona user, but not corona labs itself, so that may have been part of the issue. 

I have been using Storyboard API so far. Nevertheless, I ended up in that same problem.

However, recently, I have just noticed that the camera sample app which is found in Corona SDK also doesn’t work on my android Galaxy Note II. The problem is that I can only open Camera, but then the picture didn’t display on screen at all. I have used logcat to see error logs on device. What I have noticed is that function call media.show(media.Camera, sessionComplete) doesn’t really go to sessionComplete after Camera opened. I noticed that by trying to print some messages in sessionComplete function, yet, I never got those messages printed out.

So, i think it’s a problem of Camera API itself. What do you think then? Thank for sharing idea with me here.

Perhaps there is a phone setting that allows/disallows applications to access native phone features? 

Native phone features? I will try to look for it then. If you can think of other possible causes, please kindly let me know. thank. :slight_smile:

Hi @malypoeur,

Did you grant access to the device’s camera in “build.settings”? That is described in the documentation for “media.show”:

http://docs.coronalabs.com/api/library/media/show.html

and also here, more generally:

http://docs.coronalabs.com/guide/distribution/buildSettings/index.html#androidsettings

Hope this helps,

Brent

Hello Brent,

Yes, I really did. I have no clues now why it still doesn’t work. :frowning:

Thanks Brent,

Malypoeur

Hello Brent,

I have tested Camera sample app on different device HTC android, and it works. That same build does not work on Samsung Galaxy Note II and Samsung Galaxy S3. And that same build just happen to crash on android 2.3 (another phone of my friend).

Any suggestion idea would be appreciated. thank

Malypoeur

I’ve been having almost the same issue with media.Camera.

local onComplete = function(event)

    local photo = event.target

    print(‘±--------------------------------------------------’)

    print(’| back from camera with photo: '…tostring(photo))

    print(‘±--------------------------------------------------’)

    if photo then

end

media.show(media.Camera,

            {listener=onComplete},

            {baseDir=system.DocumentsDirectory,filename=‘captured.jpg’,type=‘image’})

It works beautifully, as per documentation and example, on the Corona simulator on my rMBP. I am even able to take pictures on my rMBP (it automatically uses the facetime camera) and each picture gets transferred nicely into the app via the listener (onComplete). The file is saved automatically on the documents folder. However, after building my app and deploying it to iPad2, iPhone5 and Asus Nexus (android,) it does *not* work in the end (the most important step, which is saving the picture taken and/or passing it into the app.) The camera shows up and I can take the picture on all devices, however, the picture does not get transferred to the app. I put a print statement in the listener to see it but it simply comes back from the camera as *nil* (seen in xcode device console.) Note that the same print statement on the simulator shows a nice table memory address pointer as it should. It just does not work on any of my devices. I am using corona’s storyboard and have set the camera permissions for android, which is not required for iPhone, by the way. I can also rule out writing permission issues and such as I was able to save and load an asset image (bundled with the app) to and from within system.DocumentsDirectory on all devices with no problems. I’ll continue looking for a solution and post if I find it, but I would appreciate if someone could post their solution.