Page 1 of 1

Is this possible with LiveCode?

Posted: Wed Feb 27, 2013 9:32 pm
by DevBoyLars
Hi there,
is it possible with LiveCode to capture a photo with a mobile device, take this photo, save it to file or database (not camera roll of device!) and show this picture inside the App in a slider?

Thank you!

Re: Is this possible with LiveCode?

Posted: Wed Feb 27, 2013 9:47 pm
by Dixie
Yes...

Dixie

Re: Is this possible with LiveCode?

Posted: Wed Feb 27, 2013 9:51 pm
by DevBoyLars
Both works out of the box with LiveCode? Save the taken picture to file/device (not camera-roll) or into a database?

Are there any code examples for this and for a picture-swipe?

Re: Is this possible with LiveCode?

Posted: Wed Feb 27, 2013 10:10 pm
by Dixie
This script will take a photo and then save the photo (calling it 'temppic") as a PNG file to the 'documents' folder of your app.

Code: Select all

on mouseDown
   set the loc of the templateimage to -1000,-1000
   
   mobilePickPhoto "rear camera", 320,460
   if the result is empty then
      set the text of image "thePhoto" to the last image of this card
      set the visible of image "thePhoto" to true
      wait 0 millisecs with messages
      delete the last image of this card
      
      set the defaultfolder to specialfolderpath("documents")
      export image "thePhoto" of this card to URL("binfile:" & "./" & "tempic" ) as PNG
   end if
end mouseDown
This will get you started...

Dixie

Re: Is this possible with LiveCode?

Posted: Thu Feb 28, 2013 8:57 pm
by DevBoyLars
Wow, that's amazing!

Thank you!

Could you comment what which line does?

Re: Is this possible with LiveCode?

Posted: Thu Feb 28, 2013 9:43 pm
by sturgis
Dixie, hope you don't mind my taking a shot at this answer, but i'm trying to get better at understanding other peoples code as well as mine. Any corrections, additions welcome of course so that I can improve my understanding of all things livecode.

I suspect dixie will ask you to look up "result" in the dictionary and read the examples there so you understand fully what "the result" does in the code. Simplest explanation is that "the result" will contain the status of the previous command or function if there was an error. So if the result is empty the mobilepicphoto command worked.

Same with mobilepicphoto, though as you can see by looking at the code, it takes an argument that tells it what source to use for the image In this case the rear camera, and what dimensions to use for the image.

When a picture is taken using mobilpicphoto it creates a new "image" object to contain the picture. What may not be obvious is why templateimage is used.
the templateimage is just that. A template for the image object. When you set properties for the templateimage and then create a new image object it uses the settings you specified. So in the case of this really cool script, it sets the location (loc) of the template image to a hidden spot off screen. So when the picture is taken it appears in that location.

Then, since you already have an empty image object to hold the data, when you set the text of image "imagename" to the text of another image it copies the data from one image and stuffs it into the other.

Then you should look up "last" in the dictionary so that you get a good handle on its workings. In this case it is referring to the "last" image that was created, meaning the picture that was just taken that is hidden off screen at -1000 -1000

Then the newly filled image (thePhoto) is set to visible so that it can be seen.

Wait 0 seconds with messages (again, read up on it in the dictionary) is what it appears to be. The current handler is paused. The "with messages" part means that other things waiting in the message queue can be worked on while waiting. In this case what it does is let some background housekeeping chores occur, some of which are necessary due to the preceding code.

Once the wait is done (despite being 0 length of time, whatevr is in queue will finish before control is returned to the handler I believe) then it is safe to delete the hidden image that is off screen. Since no new images have been created, the "delete the last image of this card" line will remove it.

For the defaultfolder stuff, definitely again look in the dictionary. It explains what specialfolderpaths are available on each platform. Basically it is a way of pointing to a folder that has a path supplied by the system. So the specialfolderpath("documents") will point the the documents folder of the current user. No need to figure out if it's /Users/username/blahblahblah.
Setting the defaultfolder changes the current working directory of the app to wherever it is you tell it to go. So in this case the working directory for the app is the users documents folder.

The export also is pretty straight forward. You are exporting the image "thePhoto" that you just got to a file. URL means you are going to define what type of data transfer it is, in this case its a binary file. (like on the web, http: is a hypertext transfer protocol page, ftp: is file transfer protocol) LC has file: and binfile: So you are exporting the image to URL of type binfile: The rest just tells it the path and name of the file.

Dixie is doing somethign cool here though. To avoid complications on mobile I tend to use full paths to files rather than relative. I suspect using a relative path starting with . will solve the issues i've been having making it easy to do relative pathing with defaultfolder. in a path . means the current folder (so whatever the defaultfolder is set to) .. would mean up 1 folder. So, since the defaultfolder is already pointing to the documents folder, putting a file in ./filename.png will create a file named filename.png in the users documents folder.

PHEW

Re: Is this possible with LiveCode?

Posted: Thu Feb 28, 2013 9:48 pm
by DevBoyLars
Thats great, thank you :)