Skip to content

Commit

Permalink
Merge pull request #160 from calaos/nico/rotate_cam
Browse files Browse the repository at this point in the history
Add rotate option to calaos_picture script and use it if rotate optio…
  • Loading branch information
raoulh authored Apr 17, 2023
2 parents 74b1106 + 7ad2ebb commit 543114d
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 43 deletions.
3 changes: 3 additions & 0 deletions src/bin/calaos_server/IPCam/IPCam.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
26 changes: 17 additions & 9 deletions src/bin/calaos_server/JsonApiHandlerHttp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<uvw::ProcessHandle>();
exe_thumb->once<uvw::ExitEvent>([this](const uvw::ExitEvent &ev, auto &h)
Expand Down Expand Up @@ -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<uvw::ProcessHandle>();
exe_thumb->once<uvw::ExitEvent>([this](const uvw::ExitEvent &ev, auto &h)
Expand Down Expand Up @@ -934,4 +943,3 @@ void JsonApiHandlerHttp::processRegisterPush()
Json ret = {{ "success", registerPushToken(jsonParam)?"true":"false" }};
sendJson(ret);
}

85 changes: 51 additions & 34 deletions src/bin/tools/calaos_picture
Original file line number Diff line number Diff line change
@@ -1,62 +1,79 @@
#/bin/sh
#!/bin/bash

#Usage:
#calaos_picture <url> <filename> <width>
#calaos_picture <url> <filename> [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 <url> <filename> [width]"
echo "Usage: calaos_picture <url> <filename> -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}"

0 comments on commit 543114d

Please sign in to comment.