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)