Example #1
0
        void Connect(String deviceId, Action block)
        {
            bool find = false;

            foreach (String did in deviceList())
            {
                if (deviceId.Equals(did))
                {
                    find = true;
                    break;
                }
            }
            if (!find)
            {
                throw new MtpHelperRuntimeException("Device not found");
            }

            deviceId = USBDEVICEPATH_HEADER + deviceId;
            try
            {
                command.Open(deviceId);
            }
            catch (Exception e) // System.IO.FileNotFoundException, System.ArgumentException
            {
                Console.Error.WriteLine("# onConnect: {0}", e.GetType().ToString());
                throw new MtpHelperRuntimeException("Device not found");
            }

            try
            {
                block();
            }
            catch (System.Runtime.InteropServices.COMException e)
            {
                // System.Runtime.InteropServices.COMException (0x802A0002)
                Console.Error.WriteLine("# onBlock: {0}", e.GetType().ToString());
                throw new MtpHelperRuntimeException("Device not found");
            }
            finally {
                try
                {
                    if (command != null)
                    {
                        command.Close();
                    }
                    else
                    {
                        command = new MtpCommand();
                    }
                }
                catch (Exception e)
                {
                    Console.Error.WriteLine("# onClose: {0}", e.GetType().ToString());
                    command = new MtpCommand();
                }
            }
        }
Example #2
0
        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;
                    }
                }
            }
        }
Example #3
0
        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);
        }
Example #4
0
 public MtpHelper()
 {
     command           = new MtpCommand();
     command.MtpEvent += MtpEventListener;
 }
Example #5
0
 /// <summary>
 /// シングルトンにする
 /// </summary>
 private Theta()
 {
     IsConnected = false;
     mtp         = new MtpCommand();
 }
Example #6
0
        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();
        }