Access camera/photos


#21

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 = io.open("camerasnapshot.png", "wb")
f:write(png_bytes)
f:flush()
f:close()



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

-- get some information about the png, such as the size
local info = png.info(png_bytes)

-- 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)

#22

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.


#23

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


#24

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


#25

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.


#26

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


#27

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:


#28

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


#29

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.


#30

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'];

#31

Solved it, had to add

["filename"] = "imagename"

to the header part of the request.


#32

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!


#33

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)

#34

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


#37
    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)
	        return
	    end
	        
	    local img = image.load(res.response) 
	    if not img then 
	        print(self, "Unable to load image")
	        return
        else if gui.new_texture("image", img.width, img.height, img.type, img.buffer) then 
 	            gui.set_texture(rows[v][hash("item/img")], "image")
        else
                print("unable to greate texture")
        end
	end, headers, post_data, options)

#38

@britzl

the server respond with HTTP request code 304 for all different images?

192.168.1.1 - - [28/Nov/2017:20:51:27 +0100] “GET /dev/images/1_1511897812000.png HTTP/1.1” 304 128 “-” "-"
192.168.1.1 - - [28/Nov/2017:20:51:27 +0100] “GET /dev/images/1_1511765014000.png HTTP/1.1” 304 128 “-” "-"
192.168.1.1 - - [28/Nov/2017:20:51:28 +0100] “GET /dev/images/1_1511805954000.png HTTP/1.1” 304 128 “-” “-”


#39

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


#41

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


#42

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)


#43

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.