From c1a984023ac13bdb2731098ff4789deb263b004a Mon Sep 17 00:00:00 2001 From: Nicolas Aguirre Date: Sun, 16 Apr 2023 10:14:19 +0200 Subject: [PATCH 1/2] Add rotate option to calaos_picture script and use it if rotate option is passes in json params --- src/bin/calaos_server/JsonApiHandlerHttp.cpp | 26 +++--- src/bin/tools/calaos_picture | 85 ++++++++++++-------- 2 files changed, 68 insertions(+), 43 deletions(-) diff --git a/src/bin/calaos_server/JsonApiHandlerHttp.cpp b/src/bin/calaos_server/JsonApiHandlerHttp.cpp index e707e784..a81ebef0 100644 --- a/src/bin/calaos_server/JsonApiHandlerHttp.cpp +++ b/src/bin/calaos_server/JsonApiHandlerHttp.cpp @@ -403,10 +403,12 @@ void JsonApiHandlerHttp::processGetCover() return; } - string w; + string width; if (jsonParam.Exists("width")) - w = jsonParam["width"]; + width = jsonParam["width"]; + string rotate; + if (jsonParam.Exists("rotate")) player->get_album_cover([=](AudioPlayerData data) { //do not start another exe if one is running already @@ -420,8 +422,8 @@ void JsonApiHandlerHttp::processGetCover() } string cmd = "calaos_picture " + data.svalue + " " + tempfname; - if (!w.empty()) - cmd += " " + w; + if (!width.empty()) + cmd += " -w " + width; exe_thumb = uvw::Loop::getDefault()->resource(); exe_thumb->once([this](const uvw::ExitEvent &ev, auto &h) @@ -455,13 +457,20 @@ void JsonApiHandlerHttp::processGetCameraPic() return; } - string w; + string width; if (jsonParam.Exists("width")) - w = jsonParam["width"]; + width = jsonParam["width"]; + + string rotate; + if (jsonParam.Exists("rotate")) + rotate = jsonParam["rotate"]; string cmd = "calaos_picture " + camera->getPictureUrl() + " " + tempfname; - if (!w.empty()) - cmd += " " + w; + if (!width.empty()) + cmd += " -w " + width; + + if (!rotate.empty()) + cmd += " -r " + rotate; exe_thumb = uvw::Loop::getDefault()->resource(); exe_thumb->once([this](const uvw::ExitEvent &ev, auto &h) @@ -934,4 +943,3 @@ void JsonApiHandlerHttp::processRegisterPush() Json ret = {{ "success", registerPushToken(jsonParam)?"true":"false" }}; sendJson(ret); } - diff --git a/src/bin/tools/calaos_picture b/src/bin/tools/calaos_picture index 77f31efb..cc3187b5 100755 --- a/src/bin/tools/calaos_picture +++ b/src/bin/tools/calaos_picture @@ -1,62 +1,79 @@ -#/bin/sh +#!/bin/bash #Usage: -#calaos_picture +#calaos_picture [width] [rotate] -set -e +set -x -url=$1 -filename=$2 -width=$3 +# Parse command line arguments +OPTIONS=$(getopt -o r:w: --long rotate:,width: -- "$@") +eval set -- "$OPTIONS" + +# Set default values +rotate="" +width="" + +# Process options +while true; do + case "$1" in + -r|--rotate) + rotate="$2" + shift 2;; + -w|--width) + width="$2" + shift 2;; + --) + shift; break;; + *) + echo "Invalid option: $1" >&2; exit 1;; + esac +done + +# Get remaining arguments +url="$1" +filename="$2" + +# Download file from URL +curl -o "$filename" "$url" showUsage() { - echo "Usage: calaos_picture [width]" + echo "Usage: calaos_picture -w|--width [width] -r|--rotate [rotate]" exit 1 } - -if [ "x$url" = "x" ]; then +if [ "$url" == "" ]; then showUsage fi -if [ "x$filename" = "x" ]; then +if [ "$filename" == "" ]; then showUsage fi -if ! which curl &> /dev/null; then +if ! which curl > /dev/null 2>&1 ; then echo "curl is not installed." exit 1 fi -curl $url --output ${filename}_curl +curl "$url" --output "${filename}"_curl -if ! which convert &> /dev/null; then +if ! which convert > /dev/null 2>&1 ; then echo "imagemagick is not installed. Resizing is not done." - mv ${filename}_curl ${filename} + mv "${filename}"_curl "${filename}" exit 0 fi -if [ "x$width" = "x" ]; then - mv ${filename}_curl ${filename} +if [ "$width" == "" ] && [ "$rotate" == "" ]; then + mv "${filename}"_curl "${filename}" exit 0 fi -convert ${filename}_curl \ - -filter Triangle \ - -define filter:support=2 \ - -thumbnail ${width} \ - -unsharp 0.25x0.25+8+0.065 \ - -dither None \ - -posterize 136 \ - -quality 82 \ - -define jpeg:fancy-upsampling=off \ - -define png:compression-filter=5 \ - -define png:compression-level=9 \ - -define png:compression-strategy=1 \ - -define png:exclude-chunk=all \ - -interlace none \ - -colorspace sRGB \ - -strip ${filename} - -rm -fr ${filename}_curl +# Resize image and rotate before resizing +if [ ! "$rotate" == "" ]; then + convert "${filename}"_curl -rotate "${rotate}" "${filename}_curl" +fi + +if [ ! "$width" == "" ]; then + convert "${filename}"_curl -thumbnail "${width}" "${filename}_curl" +fi +mv "${filename}"_curl "${filename}" From 7ad2ebbb2c584361674af5454d14447e442fd7f5 Mon Sep 17 00:00:00 2001 From: Nicolas Aguirre Date: Sun, 16 Apr 2023 12:22:48 +0200 Subject: [PATCH 2/2] Add option rotate and width to IPCam class --- src/bin/calaos_server/IPCam/IPCam.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/bin/calaos_server/IPCam/IPCam.cpp b/src/bin/calaos_server/IPCam/IPCam.cpp index 6972cd38..92300b05 100644 --- a/src/bin/calaos_server/IPCam/IPCam.cpp +++ b/src/bin/calaos_server/IPCam/IPCam.cpp @@ -32,6 +32,9 @@ IPCam::IPCam(Params &p): set_param("gui_type", "camera"); set_param("visible", "false"); + ioDoc->paramAdd("width", _("Width of the image, if this parameter is set, video will be resized to fit the given width. Let parameter empty to keep the original size."), IODoc::TYPE_INT, false, ""); + ioDoc->paramAdd("rotate", _("Rotate the image. Set a value between. The value is in degrees. Example : -90 for Counter Clock Wise rotation, 90 for Clock Wise rotationCW."), IODoc::TYPE_INT, false, ""); + //Add again to cache, because gui_type has changed //Special case for Camera ListeRoom::Instance().addIOHash(this);