private static void SetMtpDevice(Device device, MediaDevice currentMediaDevice) { MtpResponse mtpResponse; MtpCommand mtpCommand = new MtpCommand(); string[] deviceIds = mtpCommand.GetDeviceIds(); if (deviceIds.Length == 0) { return; } string targetDeviceId = String.Empty; foreach (string deviceId in deviceIds) { if (currentMediaDevice.FriendlyName.Equals(mtpCommand.GetDeviceFriendlyName(deviceId))) { targetDeviceId = deviceId; break; } } if (targetDeviceId.Length == 0) { return; } mtpCommand.Open(targetDeviceId); uint[] storageIds = null; mtpResponse = mtpCommand.Execute(MtpOperationCode.GetStorageIDs, null, null); if (mtpResponse != null) { storageIds = Utils.GetUIntArray(mtpResponse.Data); if (storageIds.Length != 0) { try { mtpResponse = mtpCommand.Execute(MtpOperationCode.GetStorageInfo, new uint[1] { storageIds[0] }, null); StorageInfo storageInfo = new StorageInfo(mtpResponse.Data); device.TotalMemory = (long)storageInfo.MaxCapacity; device.FreeMemory = (long)storageInfo.FreeSpaceInBytes; device.UsedMemory = device.TotalMemory - device.FreeMemory; } catch { device.TotalMemory = device.FreeMemory = device.UsedMemory = 0; } } } }
/// <summary> /// Thetaに接続する /// </summary> /// <returns></returns> public bool Connect() { // 接続済みならtrueを返して終了 if (IsConnected) { return(true); } string thetaId = null; string[] ids = mtp.GetDeviceIds(); foreach (string id in ids) { int vid = id.IndexOf("vid_05ca"); int pid = id.IndexOf("pid_0366"); if (vid != -1 && pid != -1) { thetaId = id; break; } } // Thetaがなければfalseを返す if (thetaId == null) { return(false); } // Thetaに接続する mtp.Open(thetaId); IsConnected = true; // 静止画撮影モードにする MtpResponse res; res = mtp.Execute(MtpOperationCode.SetDevicePropValue, new uint[1] { (uint)MtpDevicePropCode.StillCaptureMode }, BitConverter.GetBytes(((ushort)0x0001))); // 撮影モードをオートにする res = mtp.Execute(MtpOperationCode.SetDevicePropValue, new uint[1] { (uint)MtpDevicePropCode.ExposureProgramMode }, BitConverter.GetBytes(((ushort)0x0002))); // ストレージIDを取得する res = mtp.Execute(MtpOperationCode.GetStorageIDs, null, null); storageId = MtpData.GetUInt32Array(res)[0]; return(true); }
static public List <Device> GetDeviceList() { List <Device> answer = new List <Device>(); foreach (var currentMediaDevice in MediaDevice.GetDevices()) { Device device = new Device(); try { currentMediaDevice.Connect(); } catch (System.IO.DirectoryNotFoundException ex) { continue; } device.Name = currentMediaDevice.FriendlyName; if (currentMediaDevice.DeviceType == MediaDevices.DeviceType.Generic) { DriveInfo[] allDrives = DriveInfo.GetDrives(); foreach (var currentDriver in allDrives) { if (currentDriver.DriveType == DriveType.Removable) { if (currentDriver.VolumeLabel == device.Name) { device.Letter = currentDriver.Name[0]; device.Root = currentDriver.RootDirectory; device.TotalMemory = currentDriver.TotalSize; device.FreeMemory = currentDriver.TotalFreeSpace; device.BusyMemory = device.TotalMemory - device.FreeMemory; device.Extracted = true; break; } } } } else { device.Extracted = false; device.Name += " - " + currentMediaDevice.Model; MtpResponse res; MtpCommand command = new MtpCommand(); string[] deviceIds = command.GetDeviceIds(); if (deviceIds.Length == 0) { continue; } string targetDeviceId = String.Empty; foreach (string deviceId in deviceIds) { if (currentMediaDevice.FriendlyName.Equals(command.GetDeviceFriendlyName(deviceId))) { targetDeviceId = deviceId; break; } } if (targetDeviceId.Length == 0) { continue; } command.Open(targetDeviceId); uint[] storageIds = null; res = command.Execute(MtpOperationCode.GetStorageIDs, null, null); if (res != null) { storageIds = Utils.GetUIntArray(res.Data); if (storageIds.Length != 0) { try { res = command.Execute(MtpOperationCode.GetStorageInfo, new uint[1] { storageIds[0] }, null); StorageInfo storageInfo = new StorageInfo(res.Data); device.TotalMemory = (long)storageInfo.MaxCapacity; device.FreeMemory = (long)storageInfo.FreeSpaceInBytes; device.BusyMemory = device.TotalMemory - device.FreeMemory; } catch { device.TotalMemory = device.FreeMemory = device.BusyMemory = 0; } } } } currentMediaDevice.Disconnect(); answer.Add(device); } return(answer); }
static void Main(string[] args) { MtpResponse res; MtpCommand command = new MtpCommand(); // 接続されているデバイスIDを取得する string[] deviceIds = command.GetDeviceIds(); if (deviceIds.Length == 0) { return; } // RICOH THETA S デバイスを取得する string targetDeviceId = String.Empty; foreach (string deviceId in deviceIds) { if ("RICOH THETA S".Equals(command.GetDeviceFriendlyName(deviceId))) { targetDeviceId = deviceId; break; } } if (targetDeviceId.Length == 0) { return; } command.Open(targetDeviceId); // イベントを受け取れるようにする command.MtpEvent += MtpEventListener; // DeviceInfo res = command.Execute(MtpOperationCode.GetDeviceInfo, null, null); DeviceInfo deviceInfo = new DeviceInfo(res.Data); // DevicePropDesc(StillCaptureMode) res = command.Execute(MtpOperationCode.GetDevicePropDesc, new uint[1] { (uint)MtpDevicePropCode.StillCaptureMode }, null); DevicePropDesc dpd = new DevicePropDesc(res.Data); // シャッター優先 command.Execute(MtpOperationCode.SetDevicePropValue, new uint[1] { (uint)MtpDevicePropCode.ExposureProgramMode }, BitConverter.GetBytes((ushort)ExposureProgramMode.ShutterPriorityProgram)); // シャッター速度(Get) res = command.Execute(MtpOperationCode.GetDevicePropValue, new uint[1] { (uint)MtpDevicePropCode.ShutterSpeed }, null); ShutterSpeed ss = new ShutterSpeed(res.Data); // シャッター速度(Set) ss = new ShutterSpeed(1, 100); // 1/100 res = command.Execute(MtpOperationCode.SetDevicePropValue, new uint[1] { (uint)MtpDevicePropCode.ShutterSpeed }, ss.Data); // シャッター速度(Get) res = command.Execute(MtpOperationCode.GetDevicePropValue, new uint[1] { (uint)MtpDevicePropCode.ShutterSpeed }, null); ss = new ShutterSpeed(res.Data); // DevicePropDesc(ExposureIndex) res = command.Execute(MtpOperationCode.GetDevicePropDesc, new uint[1] { (uint)MtpDevicePropCode.ExposureIndex }, null); dpd = new DevicePropDesc(res.Data); // StillCaptureMode res = command.Execute(MtpOperationCode.GetDevicePropValue, new uint[1] { (uint)MtpDevicePropCode.StillCaptureMode }, null); StillCaptureMode mode = (StillCaptureMode)BitConverter.ToUInt16(res.Data, 0); // ストレージIDをとる res = command.Execute(MtpOperationCode.GetStorageIDs, null, null); uint[] storageIds = Utils.GetUIntArray(res.Data); // ストレージ情報をとる res = command.Execute(MtpOperationCode.GetStorageInfo, new uint[1] { storageIds[0] }, null); StorageInfo storageInfo = new StorageInfo(res.Data); // オブジェクト数をとる res = command.Execute(MtpOperationCode.GetNumObjects, new uint[3] { storageIds[0], 0, 0 }, null); uint num = res.Parameter1; // GetObjectHandles res = command.Execute(MtpOperationCode.GetObjectHandles, new uint[3] { storageIds[0], 0, 0 }, null); uint[] objectHandles = Utils.GetUIntArray(res.Data); // 静止画か動画をデスクトップに保存する // objectHandlesの最初の3つはフォルダのようなので4つ目を取得する if (objectHandles.Length > 3) { // ファイル名を取得する res = command.Execute(MtpOperationCode.GetObjectInfo, new uint[1] { objectHandles[3] }, null); ObjectInfo objectInfo = new ObjectInfo(res.Data); // ファイルを取得する res = command.Execute(MtpOperationCode.GetObject, new uint[1] { objectHandles[3] }, null); if (res.ResponseCode == MtpResponseCode.OK) { // デスクトップへ保存する using (FileStream fs = new FileStream( Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) + "\\" + objectInfo.Filename, // ファイル名 FileMode.Create, FileAccess.Write)) { fs.Write(res.Data, 0, res.Data.Length); } } } // 撮影する res = command.Execute(MtpOperationCode.InitiateCapture, new uint[2] { 0, 0 }, null); // デバイスよさようなら command.Close(); }