見出し画像

カメラアプリを自動操作して撮影する(Windows)

Windowsの標準カメラアプリを自動操作して撮影する方法を紹介します。
定点カメラとして利用するもヨシ、監視カメラとしてもヨシ

OSはWindows10以降


まずは基本
カメラの起動して終了するだけのコード

# カメラアプリを起動する
Start-Process "microsoft.windows.camera:"

# カメラアプリが起動して写真を撮れる状態まで3秒待つ
Start-Sleep -m 3000

# カメラアプリのプロセス名を取得する
$cameraProcess = Get-Process | Where-Object { $_.ProcessName -like "*WindowsCamera*" }

# カメラアプリを終了させる
Stop-Process -Id $cameraProcess.Id

写真が撮れる状態になるまで3000ミリ秒待っていますが最近の早いパソコンなら300ミリ秒くらいでも大丈夫だと思います。


カメラの起動から自動撮影まで


# System.Windows.Formsなどのインポート(Enterボタン押下の為)
Add-Type -AssemblyName System.Windows.Forms

# Windows APIの宣言(Enterボタン押下の為)
$signature=@'
[DllImport("user32.dll",CharSet=CharSet.Auto,CallingConvention=CallingConvention.StdCall)]
public static extern void mouse_event(long dwFlags, long dx, long dy, long cButtons, long dwExtraInfo);
'@

# カメラアプリを起動する
Start-Process "microsoft.windows.camera:"

# カメラアプリが起動して写真を撮るために3秒待つ
Start-Sleep -m 3000

# カメラアプリのプロセス名を取得する
$cameraProcess = Get-Process | Where-Object { $_.ProcessName -like "*WindowsCamera*" }

if ($cameraProcess) {

   # 写真撮影 Enterボタン押下
   [System.Windows.Forms.SendKeys]::SendWait("{ENTER}")
   Start-Sleep -m 2000

   # カメラアプリ終了
   [System.Windows.Forms.SendKeys]::SendWait("%{F4}")

}


カメラアプリが起動してから3000ミリ秒待ってENTERキーを押し、さらに2000ミリ秒後F4キー+ALTキーを押しています。
これはカメラアプリが立ち上がった時、写真モードだったらなら撮影即Stop-Processで撮影できますが、動画撮影モードになっていたとき丁寧に終了させないと撮影できないのでこうしました(Windowsのカメラアプリは前回の撮影モードで立ち上がる仕様になっています)。


カメラの起動から自動撮影、撮影ファイルの取得まで

# System.Windows.Formsなどのインポート(Enterボタン押下の為)
Add-Type -AssemblyName System.Windows.Forms

# Windows APIの宣言(Enterボタン押下の為)
$signature=@'
[DllImport("user32.dll",CharSet=CharSet.Auto,CallingConvention=CallingConvention.StdCall)]
public static extern void mouse_event(long dwFlags, long dx, long dy, long cButtons, long dwExtraInfo);
'@

# カメラアプリを起動する
Start-Process "microsoft.windows.camera:"
Start-Sleep -m 3000


# カメラアプリのプロセス名を取得する
$cameraProcess = Get-Process | Where-Object { $_.ProcessName -like "*WindowsCamera*" }

if ($cameraProcess) {

    # 写真撮影 Enterボタン押下
    [System.Windows.Forms.SendKeys]::SendWait("{ENTER}")
    Start-Sleep -m 2000

    # カメラアプリ終了
    [System.Windows.Forms.SendKeys]::SendWait("%{F4}")
}


# ユーザーのピクチャフォルダパスを取得
$picturesPath = [Environment]::GetFolderPath("MyPictures")
# カメラロールフォルダのパスを組み立て
$cameraRollPath = Join-Path -Path $picturesPath -ChildPath "Camera Roll"

# カメラロールフォルダの存在を確認
if (Test-Path -Path $cameraRollPath) {
    # 拡張子がjpgまたはmp4のファイルを取得
    $files = Get-ChildItem -Path $cameraRollPath -Name *.jpg -File
    $files+= Get-ChildItem -Path $cameraRollPath -Name *.mp4 -File

    # 最新のファイルを取得
    $latestFile = $files | Sort-Object -Property LastWriteTime -Descending | Select-Object -First 1

    # 最新のファイル名を表示
    if ($latestFile) {
        Write-Output "撮影ファイル名: $latestFile"
    } else {
        Write-Output "jpgまたはmp4ファイルが見つかりませんでした。"
    }
} else {
    Write-Output "カメラロールフォルダが見つかりませんでした。"
}

Windowsの標準カメラアプリで撮影すると、写真も動画も「カメラロール」というフォルダに保存されます。撮影後にフォルダを検索して撮影ファイルを探すようにしました。この部分はAIが書いています。
この撮影ファイルをファイルサーバにコピーするとかメール送信するとか、使い道がいろいろあると思います。


#PowerShell #自動化 #カメラアプリ #RPA #コマンドレット #プログラミング初心者 #プログラミング学習 #AI #AIコーディング

この記事が気に入ったらサポートをしてみませんか?