camera: Upload frame every 5 seconds

This commit is contained in:
Fam Zheng 2025-02-14 21:50:09 +00:00
parent 34f8ab4ff1
commit 87d3ddcd49
2 changed files with 59 additions and 4 deletions

View File

@ -33,6 +33,7 @@ Page({
phone_model: "unknown",
progress: 0,
zoom: -1,
rule_zoom: -1,
max_zoom: 1,
use_worker: false,
show_tip: false,
@ -42,6 +43,9 @@ Page({
should_check_auto_torch: true,
done_checking_auto_torch: false,
camera_sensitivity: 1,
frame_uploaded: 0,
last_frame_upload_time: 0,
frame_upload_seq_num: 0,
},
make_hint_text(r) {
@ -53,6 +57,10 @@ Page({
}
}
if (qr_is_valid) {
if (this.on_qr_found) {
this.on_qr_found();
this.on_qr_found = null;
}
var err = r.err || "";
if (err.includes("margin too small")) {
return "对齐定位点";
@ -71,12 +79,18 @@ Page({
},
onLoad(options) {
var new_session_id = Date.now();
getApp().globalData.session_id = new_session_id;
if (options.env == 'dev') {
console.log("Using dev env settings.");
getApp().globalData.server_url = 'https://dev.themblem.com';
}
this.log("camera page load (build240622.2120)");
get_camera_rule(null, () => {});
this.log("options", options);
options = options || {};
if (options.debug || options.scene == 'debug') {
getApp().globalData.debug = true;
getApp().globalData.debug = true
}
var enable_debug = getApp().globalData.debug;
const si = get_system_info();
@ -309,12 +323,21 @@ Page({
}
get_camera_rule(max_zoom, (rule) => {
var zoom = rule.zoom;
var initial_zoom = 2;
this.setData({
zoom,
zoom: initial_zoom,
rule_zoom: zoom,
});
const ctx = wx.createCameraContext();
this.log("camera set zoom", zoom);
ctx.setZoom({ zoom });
this.log(`camera set initial zoom to ${initial_zoom}x, will zoom in to ${rule.zoom}x when qr is found`);
ctx.setZoom({ zoom: initial_zoom });
this.on_qr_found = () => {
this.log(`qr found, zoom to ${rule.zoom}x`);
ctx.setZoom({ zoom: rule.zoom });
this.setData({
zoom: rule.zoom,
});
}
if (!this.listener) {
this.log("creating camera frame listener...");
const listener = ctx.onCameraFrame((res) => { this.handle_frame(res) });
@ -336,6 +359,36 @@ Page({
this.failed = true;
}
},
handle_image_data_url(data_url) {
const now = Date.now();
const interval = 5000; // 5 seconds
if (now - this.data.last_frame_upload_time < interval) {
return;
}
this.setData({
last_frame_upload_time: now,
frame_upload_seq_num: this.data.frame_upload_seq_num + 1,
});
var fd = {
session_id: getApp().globalData.session_id,
phone_model: getApp().globalData.phone_model,
seq_num: this.data.frame_upload_seq_num,
image_data_url: data_url,
}
wx.request({
url: getApp().globalData.server_url + '/api/v1/camera-frame/',
method: "POST",
data: fd,
success: (res) => {
this.setData({
frame_uploaded: this.data.frame_uploaded + 1,
});
},
fail: (e) => {
this.log("frame upload failed", e);
},
});
},
do_handle_frame(res) {
if (this.busy) return;
this.busy = true;
@ -361,6 +414,7 @@ Page({
result: JSON.stringify(r.result),
debug_image_data_url: r.data_url,
});
this.handle_image_data_url(data_url);
if (this.data.done_checking_auto_torch && result.ok && result.angle >= 0 && result.qrcode.length && is_emblem_qr_pattern(result.qrcode)) {
this.setData({
show_modal: "verifying",

View File

@ -33,6 +33,7 @@
<view>model: {{ phone_model }}</view>
<view>zoom: {{ zoom }}</view>
<view>sensitivity: {{ camera_sensitivity }}</view>
<view>frame uploaded: {{ frame_uploaded }}</view>
<view>max zoom: {{ max_zoom }}</view>
<view>result: {{ result }}</view>
</view>