Access camera/photos

This is what I’d do if I use extension-camera, defold-png (completely untested):

-- get information about the camera (we need to know width and height)
local info = camera.get_info()

-- retrieves the camera pixel buffer This buffer has one stream named "rgb", and is of type buffer.VALUE_TYPE_UINT8 and has the value count of 1
local frame = camera.get_frame()

-- get the bytes of the pixel buffer as a string. These are raw RGB bytes.
local pixels = buffer.get_bytes(frame, hash("rgb"))

-- encode raw RGB bytes to a PNG
local png_bytes = png.encode_rgb(pixels, info.width, info.height)

-- save the PNG to disk
local f ="camerasnapshot.png", "wb")

-- now lets reverse the process by first reading the image
local f ="camerasnapshot.png", "rb")
local png_bytes = f:read("*a")

-- get some information about the png, such as the size
local info =

-- decode the png into a pixel buffer
local png_as_pixels = png.decode_rgb(png_bytes)

-- now set the png pixels as the texture on a sprite (#sprite)
local texture_header = {
	width = info.width,
	height = info.height,
	type = resource.TEXTURE_TYPE_2D,
	format = resource.TEXTURE_FORMAT_RGB,
	num_mip_maps = 1,
resource.set_texture(go.get("#sprite", "texture0"), texture_header, png_as_pixels)
1 Like

Yes this works, but i would like to add images to a GUI node, is that possible? i have a scroll list (gooey) and i want to place different images on every row.

I figured it out, no need for help about that image in GUI node any more. thx!


Hmm… still haven’t figured out how i can save images to a server, any examples of this somewhere? @britzl?

You can make http requests using the http.request() function. Do an HTTP POST/PUT with the proper headers and send the actual pixel data in the request body.

guess i need a url safe base64 encoder? is there anyone good available?

Do you want to do the upload as a normal file upload or will you have a custom receiver on the server to receive and store the image?

For b64 encoding you have plenty of options:

I have a PHP server that i before saved the base64 string of an image in the database, but it would be better to just send the image to the PHP server and save the file to a specific folder instead, sounds better. But i cant find a working LUA script to PHP server script that works. @britzl

There are plenty of examples of how to use LuaSocket to perform file uploads using the http module provided in LuaSocket. The problem is that the .lua files in LuaSocket aren’t provided in the Defold builtins (this is something I hope we can fix soon). In the meantime my recommendation is to use my new defold-luasocket extension which includes all of the .lua files from LuaSocket as well as the mime.core files as a native extension. Once you have the project added as a library dependency in your own project refer to the file upload example.

This is awesome! did you get the upload to work? on my PHP server i get an error saying that there is no “file”

" PHP Notice:  Undefined index: userfile "

the PHP row that complain looks like this

$filename = $_FILES['userfile']['name'];

Solved it, had to add

["filename"] = "imagename"

to the header part of the request.

Great! Now that I’ve been refreshed my memory on file uploads I believe the better approach would be to use multipart/form-data for large binary files. But if you have it working now then I guess it’s ok!


Yes, i read that aswell, i dont know, the image gets saved on the server now, and i can view it with an image viewer, but when i read the image back to the client i get an error

failed (http result: -4  socket result: 0)

How do you read it back to the client? Using http.request() and ?

    http.request(texture_url, "GET", function(self, id, res)
		if res.status ~= 200 and res.status ~= 304 then 
	        print("Unable to get image\n".. res.response)
	    local img = image.load(res.response) 
	    if not img then 
	        print(self, "Unable to load image")
        else if gui.new_texture("image", img.width, img.height, img.type, img.buffer) then 
 	            gui.set_texture(rows[v][hash("item/img")], "image")
                print("unable to greate texture")
	end, headers, post_data, options)


the server respond with HTTP request code 304 for all different images? - - [28/Nov/2017:20:51:27 +0100] “GET /dev/images/1_1511897812000.png HTTP/1.1” 304 128 “-” "-" - - [28/Nov/2017:20:51:27 +0100] “GET /dev/images/1_1511765014000.png HTTP/1.1” 304 128 “-” "-" - - [28/Nov/2017:20:51:28 +0100] “GET /dev/images/1_1511805954000.png HTTP/1.1” 304 128 “-” “-”

304 = Not modified. You should cache images locally and if you get a 304 read from local cache instead of from server.

1 Like

Do you know why the image from the camera extension is upside down? can i change that some how? its from the laptop camera, maybe its different on a device? @britzel

The image was flipped in Y when I captured it and set on the sprite. So I added that. Also, the image was also flipped in X on OSX.

It’s currently not possible in that extension. Not sure when I’ll have the time to add it.
You can change the API of the extension yourself, so that it can take flags (e.g. “flipy = true” or “flipx” = true) and make a pull request (or perhaps someone can help with that)

1 Like

2 new problems now. After buying Apple developer stuff i signed the app and downloaded the Defold app from the Dashboard to my Iphone but then when i was supposed to chose my phone as a target in Defold editor i couldn’t find it. My computer and IPhone was on the same WIFI? I couldn’t do the second option either, drag the .ipa build to ITunes, because they seem to have removed that option in the latest Itunes build, there is no APPs there, so i finally found a trial program so i could transfer the .ipa to my phone, it worked but is not a permanent solution, any ideas how i can do this?

So problem 2 now. When i am trying to start the camera on the phone the app just crash, no messages. How can i debug this? I Have a IPhone X, could that be the problem? that its not supporting this phone or the latest IOS build.