This commit is contained in:
admin
2026-02-24 22:24:21 +08:00
parent 68840e6ea4
commit f0bbc86ba6
8 changed files with 409 additions and 237 deletions

View File

@@ -64,7 +64,7 @@ static const char *s_web_index =
" </div>\n"
"\n"
" <label>Image Upload</label>\n"
" <input id='imageFile' type='file' accept='.jpg,.jpeg,image/jpeg'>\n"
" <input id='imageFile' type='file' accept='.png,image/png'>\n"
" <div class='row'>\n"
" <input id='imgThreshold' type='number' min='0' max='255' value='160' placeholder='Threshold 0-255' style='max-width:160px'>\n"
" <input id='imgMaxHeight' type='number' min='64' max='3000' value='2200' placeholder='Max height' style='max-width:160px'>\n"
@@ -86,7 +86,7 @@ static const char *s_web_index =
" </label>\n"
" <button id='btnPrintImage'>Print Image</button>\n"
" </div>\n"
" <div class='small'>Direct JPG upload: browser sends binary JPEG; ESP32-S3 decodes/scales/thresholds then prints.</div>\n"
" <div class='small'>Direct PNG upload: browser sends binary PNG; ESP32-S3 decodes/scales/thresholds then prints.</div>\n"
"\n"
" <div class='small'>Tip: connect printer first, then submit print jobs.</div>\n"
" <pre id='out'>Ready.</pre>\n"
@@ -130,19 +130,19 @@ static const char *s_web_index =
"\n"
" function buildImageUploadRequest(file) {\n"
" if (!file) {\n"
" throw new Error('please select a jpg image first');\n"
" throw new Error('please select a png image first');\n"
" }\n"
" const name = (file.name || '').toLowerCase();\n"
" const type = (file.type || '').toLowerCase();\n"
" const isJpeg = type.includes('jpeg') || name.endsWith('.jpg') || name.endsWith('.jpeg');\n"
" if (!isJpeg) {\n"
" throw new Error('only .jpg/.jpeg is supported in direct upload mode');\n"
" const isPng = type.includes('png') || name.endsWith('.png');\n"
" if (!isPng) {\n"
" throw new Error('only .png is supported in direct upload mode');\n"
" }\n"
" if (file.size <= 0) {\n"
" throw new Error('empty image file');\n"
" }\n"
" if (file.size > 3 * 1024 * 1024) {\n"
" throw new Error('jpg too large (>3MB), backend limit is 4MB');\n"
" throw new Error('png too large (>3MB), backend limit is 4MB');\n"
" }\n"
"\n"
" const densityMap = {\n"
@@ -162,7 +162,7 @@ static const char *s_web_index =
" return {\n"
" path: '/v1/print/image?' + q.toString(),\n"
" body: file,\n"
" contentType: file.type || 'image/jpeg'\n"
" contentType: file.type || 'image/png'\n"
" };\n"
" }\n"
"\n"
@@ -229,7 +229,7 @@ static const char *s_web_index =
" const file = imageFileEl.files && imageFileEl.files[0] ? imageFileEl.files[0] : null;\n"
" const req = buildImageUploadRequest(file);\n"
" const result = await callBinaryApi(req.path, req.body, req.contentType);\n"
" result.client_note = 'uploaded jpg bytes: ' + file.size;\n"
" result.client_note = 'uploaded png bytes: ' + file.size;\n"
" return result;\n"
" });\n"
" </script>\n"

View File

@@ -349,7 +349,10 @@ esp_err_t rest_server_print_image_post(httpd_req_t *req) {
char content_type[96] = {0};
if (httpd_req_get_hdr_value_str(req, "Content-Type", content_type, sizeof(content_type)) == ESP_OK) {
if (strstr(content_type, "application/json") != NULL) {
return rest_server_send_error(req, "400 Bad Request", "use binary jpeg body, not json");
return rest_server_send_error(req, "400 Bad Request", "use binary png body, not json");
}
if (strstr(content_type, "image/png") == NULL) {
return rest_server_send_error(req, "415 Unsupported Media Type", "Content-Type must be image/png");
}
}
@@ -376,23 +379,23 @@ esp_err_t rest_server_print_image_post(httpd_req_t *req) {
uint8_t *raster = NULL;
size_t raster_len = 0;
char decode_err[128] = {0};
esp_err_t decode_rc = raster_tools_convert_jpeg_to_raster_384((const uint8_t *)body,
(size_t)req->content_len,
scale_to_width,
threshold,
invert,
max_height,
&width,
&height,
&raster,
&raster_len,
decode_err,
sizeof(decode_err));
esp_err_t decode_rc = raster_tools_convert_png_to_raster_384((const uint8_t *)body,
(size_t)req->content_len,
scale_to_width,
threshold,
invert,
max_height,
&width,
&height,
&raster,
&raster_len,
decode_err,
sizeof(decode_err));
free(body);
if (decode_rc != ESP_OK) {
return rest_server_send_error(req,
"400 Bad Request",
decode_err[0] != '\0' ? decode_err : "decode jpeg failed");
decode_err[0] != '\0' ? decode_err : "decode png failed");
}
esp_err_t submit_rc = submit_raster_job_and_reply(req,