Просмотр исходного кода

update: 优化usb摄像头的demo代码

Wendal Chen 1 год назад
Родитель
Сommit
566ddb1b16
1 измененных файлов с 33 добавлено и 20 удалено
  1. 33 20
      demo/camera/usb_cam/main.lua

+ 33 - 20
demo/camera/usb_cam/main.lua

@@ -1,27 +1,21 @@
 PROJECT = "camerademo"
 VERSION = "1.0.0"
 sys = require("sys")
-log.style(1)
+
+httpplus = require "httpplus"
 
 local SCAN_MODE = 0 -- 写1演示扫码
 local scan_pause = true
 local getRawStart = false
 local RAW_MODE = 0 -- 写1演示获取原始图像
 
-local uartid = 1 -- 根据实际设备选取不同的uartid
-local uartBaudRate = 115200 -- 串口波特率
-local uartDatabits = 8 -- 串口数据位
-local uartStopBits = 1 -- 串口停止位
-
--- 初始化串口
-uart.setup(uartid, uartBaudRate, uartDatabits, uartStopBits)
 
 local camera_id = camera.USB
 
 local usb_camera_table = {
     id = camera_id,
-    sensor_width = 800,
-    sensor_height = 480
+    sensor_width = 1280,
+    sensor_height = 720
 }
 
 camera.on(camera_id, "scanned", function(id, str)
@@ -31,17 +25,18 @@ camera.on(camera_id, "scanned", function(id, str)
         log.error("摄像头没有数据")
     else
         log.info("摄像头数据", str)
-        sys.publish("capture done", true)
+        sys.publish("capture_done", true)
     end
 end)
 
+-- 按键触发拍照
 local function press_key()
     log.info("boot press")
     sys.publish("PRESS", true)
 end
 gpio.setup(14, press_key, gpio.PULLDOWN, gpio.RISING)
 gpio.debounce(14, 50)
-local rawbuff, err = zbuff.create(60 * 1024, 0, zbuff.HEAP_AUTO)
+local rawbuff, err = zbuff.create(200 * 1024, 0, zbuff.HEAP_PSRAM)
 
 sys.taskInit(function()
     if rawbuff == nil then
@@ -51,22 +46,40 @@ sys.taskInit(function()
         log.info("zbuff创建失败", err)
     end
 
+    wlan.init()
+    wlan.connect("uiot", "12345678")
+
+    sys.wait(3000)
+    httpsrv.start(80, function() end) -- 联网且抓取成功之后, 可通过设备ip访问图片 http://192.168.1.19/abc.jpg
+
     log.info("摄像头初始化", camera.init(usb_camera_table))
     log.info(rtos.meminfo("sys"))
     log.info(rtos.meminfo("psram"))
     while 1 do
-        local result, data = sys.waitUntil("PRESS", 30000)
-        if result == true and data == true then
+        local result, data = sys.waitUntil("PRESS", 5000)
+        if true then
             -- camera.init(usb_camera_table)
             camera.start(camera_id)
-            camera.capture(camera_id, rawbuff, 1)
-            result, data = sys.waitUntil("capture done", 30000)
-            log.info(rawbuff:used())
+            -- camera.capture(camera_id, rawbuff, 1)
+            camera.capture(camera_id, "/abc.jpg", 1)
+            result, data = sys.waitUntil("capture_done", 30000)
+            -- log.info(rawbuff:used())
             camera.stop(camera_id)
             -- camera.close(camera_id)	--完全关闭摄像头才用这个
-            rawbuff:resize(60 * 1024)
-            log.info(rtos.meminfo("sys"))
-            log.info(rtos.meminfo("psram"))
+            -- rawbuff:resize(60 * 1024)
+
+            -- 上传到upload.air32.cn, 数据访问页面是 https://www.air32.cn/upload/data/
+            -- local code, resp = httpplus.request({
+            --     url = "http://upload.air32.cn/api/upload/jpg",
+            --     method = "POST",
+            --     body = rawbuff
+            -- })
+            -- log.info("http", code)
+
+            -- 打印内存信息, 调试用
+            -- log.info("sys", rtos.meminfo())
+            -- log.info("sys", rtos.meminfo("sys"))
+            -- log.info("psram", rtos.meminfo("psram"))
         end
     end