jpg 打印

This commit is contained in:
admin
2026-02-24 18:51:53 +08:00
parent 9e141b8de0
commit e1e0ac8929
10 changed files with 535 additions and 127 deletions

View File

@@ -43,6 +43,117 @@ static esp_err_t submit_raster_job_and_reply(httpd_req_t *req,
return err;
}
static bool parse_bool_text(const char *text, bool *out_value) {
if (text == NULL || out_value == NULL) {
return false;
}
if (strcmp(text, "1") == 0 || strcasecmp(text, "true") == 0 || strcasecmp(text, "yes") == 0) {
*out_value = true;
return true;
}
if (strcmp(text, "0") == 0 || strcasecmp(text, "false") == 0 || strcasecmp(text, "no") == 0) {
*out_value = false;
return true;
}
return false;
}
static const char *map_density_text(const char *text) {
if (text == NULL || text[0] == '\0') {
return "中等";
}
if (strcasecmp(text, "light") == 0 || strcmp(text, "较淡") == 0) {
return "较淡";
}
if (strcasecmp(text, "medium") == 0 || strcmp(text, "中等") == 0) {
return "中等";
}
if (strcasecmp(text, "dark") == 0 || strcmp(text, "较浓") == 0) {
return "较浓";
}
if (strcasecmp(text, "max") == 0 || strcmp(text, "最深") == 0) {
return "最深";
}
return "中等";
}
static void parse_image_upload_options(httpd_req_t *req,
bool *out_scale_to_width,
uint8_t *out_threshold,
bool *out_invert,
uint16_t *out_max_height,
const char **out_density) {
if (out_scale_to_width != NULL) {
*out_scale_to_width = true;
}
if (out_threshold != NULL) {
*out_threshold = 160;
}
if (out_invert != NULL) {
*out_invert = false;
}
if (out_max_height != NULL) {
*out_max_height = 2200;
}
if (out_density != NULL) {
*out_density = "中等";
}
if (req == NULL) {
return;
}
int qlen = httpd_req_get_url_query_len(req);
if (qlen <= 0 || qlen > 512) {
return;
}
char *query = (char *)calloc(1, (size_t)qlen + 1);
if (query == NULL) {
return;
}
if (httpd_req_get_url_query_str(req, query, (size_t)qlen + 1) != ESP_OK) {
free(query);
return;
}
char value[48] = {0};
if (httpd_query_key_value(query, "scale_to_width", value, sizeof(value)) == ESP_OK && out_scale_to_width != NULL) {
bool parsed = *out_scale_to_width;
if (parse_bool_text(value, &parsed)) {
*out_scale_to_width = parsed;
}
}
if (httpd_query_key_value(query, "invert", value, sizeof(value)) == ESP_OK && out_invert != NULL) {
bool parsed = *out_invert;
if (parse_bool_text(value, &parsed)) {
*out_invert = parsed;
}
}
if (httpd_query_key_value(query, "threshold", value, sizeof(value)) == ESP_OK && out_threshold != NULL) {
long v = strtol(value, NULL, 10);
if (v >= 0 && v <= 255) {
*out_threshold = (uint8_t)v;
}
}
if (httpd_query_key_value(query, "max_height", value, sizeof(value)) == ESP_OK && out_max_height != NULL) {
long v = strtol(value, NULL, 10);
if (v >= 64 && v <= 3000) {
*out_max_height = (uint16_t)v;
}
}
if (httpd_query_key_value(query, "density", value, sizeof(value)) == ESP_OK && out_density != NULL) {
*out_density = map_density_text(value);
}
free(query);
}
static esp_err_t decode_image_json_to_raster(cJSON *json,
uint16_t *out_width,
uint16_t *out_height,
@@ -235,40 +346,53 @@ esp_err_t rest_server_print_image_post(httpd_req_t *req) {
return rest_server_send_error(req, "401 Unauthorized", "unauthorized");
}
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");
}
}
bool scale_to_width = true;
uint8_t threshold = 160;
bool invert = false;
uint16_t max_height = 2200;
const char *density = "中等";
parse_image_upload_options(req,
&scale_to_width,
&threshold,
&invert,
&max_height,
&density);
char *body = NULL;
esp_err_t body_err = rest_server_read_body(req, &body);
if (body_err != ESP_OK) {
return rest_server_send_error(req, "400 Bad Request", "invalid request body");
}
cJSON *json = cJSON_Parse(body);
free(body);
if (json == NULL) {
return rest_server_send_error(req, "400 Bad Request", "invalid json");
}
cJSON *jdensity = cJSON_GetObjectItemCaseSensitive(json, "density");
const char *density = (cJSON_IsString(jdensity) && jdensity->valuestring != NULL)
? jdensity->valuestring
: "中等";
uint16_t width = 0;
uint16_t height = 0;
uint8_t *raster = NULL;
size_t raster_len = 0;
char decode_err[128] = {0};
esp_err_t decode_rc = decode_image_json_to_raster(json,
&width,
&height,
&raster,
&raster_len,
decode_err,
sizeof(decode_err));
cJSON_Delete(json);
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));
free(body);
if (decode_rc != ESP_OK) {
return rest_server_send_error(req,
"400 Bad Request",
decode_err[0] != '\0' ? decode_err : "decode image failed");
decode_err[0] != '\0' ? decode_err : "decode jpeg failed");
}
esp_err_t submit_rc = submit_raster_job_and_reply(req,