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