【问题标题】:Image capture using imagesnap command Cocoa app使用 imagesnap 命令 Cocoa 应用程序捕获图像
【发布时间】:2017-01-27 08:54:33
【问题描述】:

我正在使用“imagesnap”命令(通过 shell 脚本)在后台使用 launchd 从我的 OS X 应用程序中的网络摄像头捕获图像。

文件名:CaptureCameraPic

#!/bin/bash

timestamp=$(date +"%Y%m%d%H%M%S")

homeDirectory=$HOME/Documents

# Create Project Directory

if [ -d "$homeDirectory/MyApp" ]; then
echo "MyApp Directory exists"
cd $homeDirectory/MyApp
homeDirectory=$homeDirectory/MyApp

else
echo "MyApp Directory does not exists"

echo "Creating MyApp Directory"

cd $homeDirectory/
mkdir MyApp

echo "Created MyApp Directory successfully"

cd $homeDirectory/MyApp
homeDirectory=$homeDirectory/MyApp

fi

# Camera
if [ -d "$homeDirectory/Camera" ]; then
# Control will enter here if $DIRECTORY exists.
echo "Camera Directory exists"
cd $homeDirectory/Camera
camera_filename="IMG_${timestamp}.jpg"
echo "Take picture $camera_filename"

imagesnap "$camera_filename"

else
echo "Directory does not exists"
echo "Create directory"
cd $homeDirectory
mkdir Camera
cd $homeDirectory/Camera
camera_filename="IMG_${timestamp}.jpg"
echo "Take picture $camera_filename"

imagesnap "$camera_filename"

fi

我的 plist (test.plist) 看起来像

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>GroupName</key>
    <string>user</string>
    <key>UserName</key>
    <string>root</string>
    <key>RunAtLoad</key>
    <true/>
    <key>KeepAlive</key>
    <true/>
    <key>Label</key>
    <string>test</string>
    <key>ProgramArguments</key>
    <array>
        <string>Path_to_ CaptureCameraPic </string>
    </array>
    <key>OnDemand</key>
    <false/>
    <key>Nice</key>
    <integer>1</integer>
    <key>StartInterval</key>
    <integer>10</integer>
    <key>StandardErrorPath</key>
    <string>Path_to_Error_log.log</string>
    <key>StandardOutPath</key>
    <string>Path_to_Output_log.log</string>
</dict>
</plist>

我将脚本和 plist 文件都保存在应用程序包路径中。

在我的 os x 应用程序中,单击登录按钮时,我将 plist 复制到 /Library/LaunchDaemons/。

脚本开始运行(使用 launchctl load path_to_plist_in_LaunchDaemon_folder)(我可以在输出文件中看到日志打印,例如文件夹创建等)。

但是对于'imagesnap',它会抛出错误

imagesnap: command not found

但是当我在终端上运行这个脚本时

chmod +x CaptureCameraPic
./CaptureCameraPic

一切正常。

请需要帮助。

【问题讨论】:

  • 请问imagesnap 命令是什么?我从来没有听说过——它是从哪里来的?
  • @MarkSetchell:请检查,我刚刚用 imagesnap ref 更新了我的问题。网址(iharder.net/current/macosx/imagesnap)。
  • 在终端中尝试“which imagesnap”,如果我可以找到你会得到一个路径。然后将完整路径放入您的脚本中。
  • @Rogier:非常感谢,它成功了。 :)
  • @Akshad-Systematix 随时! ;-)

标签: macos shell daemon launchd launch-daemon


【解决方案1】:

正如@Rogier 建议的那样,您的脚本中需要imagesnap 的完整路径。您可以通过以下方式找到:

which imagesnap

或

find /usr /opt -name imagesnap -type f -print

然后您将在脚本中使用以下内容:

/path/to/imagesnap ...

另外,你的脚本中有很多乱七八糟的东西,你可以像这样更简单地做一些事情:

#!/bin/bash

# Set a variable just once so there is less maintenance
photodir="$HOME/Documents/MyApp/Camera"

# Make the directory if it doesn't exist, don't complain if it does
mkdir -p "$photodir"

cd "$photodir"
if [ $? -ne 0 ]; then
   echo "Failed to set working directory to $photodir"
   exit 1
fi
...
/path/to/imagesnap ...

此外,您的时间戳非常糟糕 - 想象一下必须稍后解析它!在里面放一些分隔符——也许是这样:

date +"%Y-%m-%d_%H:%M:%S"
2017-01-27_10:24:35 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多