QRCode-Extension/main/main.script
2017-02-28 17:31:58 +01:00

110 lines
3.3 KiB
Plaintext

function init(self)
msg.post(".", "acquire_input_focus")
local logosize = 128
local screen_width = sys.get_config("display.width", 600)
local screen_height = sys.get_config("display.height", 800)
local scale_width = screen_width / logosize
local scale_height = screen_height / logosize
go.set("#sprite", "scale", vmath.vector3(scale_width, scale_height, 1) )
if camera ~= nil then
local sysinfo = sys.get_sys_info()
local quality = camera.CAPTURE_QUALITY_HIGH
local type = camera.CAMERA_TYPE_FRONT
self.flip = 0
if sysinfo.system_name == 'iPhone OS' then
type = camera.CAMERA_TYPE_BACK
quality = camera.CAPTURE_QUALITY_MEDIUM
self.flip = 1
end
if camera.start_capture(type, quality) then
self.cameraframe = camera.get_frame()
self.camerainfo = camera.get_info()
print("Initialized camera")
pprint(self.camerainfo)
self.cameratextureheader = {width=self.camerainfo.width,
height=self.camerainfo.height,
type=resource.TEXTURE_TYPE_2D,
format=resource.TEXTURE_FORMAT_RGB,
num_mip_maps=1 }
self.debugtextureheader = {width=self.camerainfo.width,
height=self.camerainfo.height,
type=resource.TEXTURE_TYPE_2D,
format=resource.TEXTURE_FORMAT_RGB,
num_mip_maps=1 }
end
else
print("could not start camera capture")
end
if qrcode == nil then
print("could not find qrcode module")
end
end
function final(self)
if self.cameraframe ~= nil then
camera.stop_capture()
end
end
local first = 1
function update(self, dt)
if first == 0 then
first = 1
local img = resource.load("/main/images/defold.png")
local text = qrcode.scan(self.cameraframe, self.camerainfo.width, self.camerainfo.height, img)
if text ~= nil then
label.set_text("logo#qrcode", text)
end
local path = go.get("logo#sprite", "texture0")
local image = qrcode.get_debug_image()
if image ~= nil then
resource.set_texture(path, self.debugtextureheader, image)
end
end
if self.cameraframe then
local pathmodelcamera = go.get("#sprite", "texture0")
resource.set_texture(pathmodelcamera, self.cameratextureheader, self.cameraframe)
local text = qrcode.scan(self.cameraframe, self.camerainfo.width, self.camerainfo.height, self.flip)
if text ~= nil then
label.set_text("logo#qrcode", text)
end
local path = go.get("logo#sprite", "texture0")
local image = qrcode.get_debug_image()
if image ~= nil then
resource.set_texture(path, self.debugtextureheader, image)
end
end
end
function on_input(self, action_id, action)
if action_id == hash("click") or action_id == hash("touch") then
pprint(action)
label.set_text("logo#qrcode", "...")
end
end