1: //
2: // This work is licensed under a Creative Commons Attribution 3.0 Unported License.
3: //
4: // Thomas Dideriksen (thomas@dideriksen.com)
5: // Modified by M shi's Lab
6: //
7: using System;
8: using System.Collections.Generic;
9: using System.Linq;
10: using System.Text;
11: using System.Threading;
12: using System.IO;
13: using Nikon;
14:
15:
16:
17: //
18: // This demo shows how to perform continuous capture and store the images to disk
19: //
20:
21: namespace demo_continuouscapture
22: {
23: class DemoContinuousCapture
24: {
25: NikonDevice _device;
26: AutoResetEvent _waitForDevice = new AutoResetEvent(false);
27: AutoResetEvent _waitForCaptureComplete = new AutoResetEvent(false);
28: int _captureCount = 0;
29:
30: publicvoid Run()
31: {
32: try
33: {
34: // Create manager object - make sure you have the correct MD3 file for your Nikon DSLR
35: NikonManager manager = new NikonManager("Type0024.md3");
36:
37: // Listen for the 'DeviceAdded' event
38: manager.DeviceAdded += manager_DeviceAdded;
39:
40: // Wait for a device to arrive
41: _waitForDevice.WaitOne();
42: //感度・シャッター速度・連写モード・連写コマ数を設定して連写を行う
43: //Set Sensitivity 140
44: NikonEnum ISOSens = _device.GetEnum(eNkMAIDCapability.kNkMAIDCapability_Sensitivity);
45: ISOSens.Index = 3;
46: _device.SetEnum(eNkMAIDCapability.kNkMAIDCapability_Sensitivity, ISOSens);
47:
48: //set SS 1/500
49: NikonEnum SpeedMode = _device.GetEnum(eNkMAIDCapability.kNkMAIDCapability_ShutterSpeed);
50: SpeedMode.Index = 31;
51: _device.SetEnum(eNkMAIDCapability.kNkMAIDCapability_ShutterSpeed, SpeedMode);
52:
53: // Set shooting mode to 'continuous, highspeed'
54: NikonEnum shootingMode = _device.GetEnum(eNkMAIDCapability.kNkMAIDCapability_ShootingMode);
55: shootingMode.Index = (int)eNkMAIDShootingMode.kNkMAIDShootingMode_CH;
56: //shootingMode.Index = 3;
57: _device.SetEnum(eNkMAIDCapability.kNkMAIDCapability_ShootingMode, shootingMode);
58:
59: // Set number of continuous captures - in this case we want 50 でも15コマしか撮れない
60: _device.SetUnsigned(eNkMAIDCapability.kNkMAIDCapability_ContinuousShootingNum, 50);
61:
62: // Hook up capture events
63: _device.ImageReady += _device_ImageReady;
64: _device.CaptureComplete += _device_CaptureComplete;
65:
66: // Capture
67: _device.Capture();
68: //多段階撮像のため感度を上げ、続いてシャッター速度を1.5EVステップで変えながら撮像を5周します
69: //Set Sesitivity 1100
70: ISOSens.Index = 9;
71: _device.SetEnum(eNkMAIDCapability.kNkMAIDCapability_Sensitivity, ISOSens);
72:
73: int i = 0;
74: int j = 0;
75:
76: // Repeat 5 times 多段階撮像を5周りする
77: for (j = 1; j <= 1; j++)
78: {
79: //SS from 1/8000 - 1.5sec, 1.5EV step, 10 shoots, 1 loop 8sec.
80: for (i = 39; i >= 12; i =i -3)
81: {
82: SpeedMode.Index = i;
83: _device.SetEnum(eNkMAIDCapability.kNkMAIDCapability_ShutterSpeed, SpeedMode);
84:
85: // Set number of continuous captures - in this case we want 5
86: _device.SetUnsigned(eNkMAIDCapability.kNkMAIDCapability_ContinuousShootingNum, 1);
87:
88: // Hook up capture events
89: _device.ImageReady += _device_ImageReady;
90: _device.CaptureComplete += _device_CaptureComplete;
91:
92: // Capture
93: _device.Capture();
94:
95: // Wait for the capture to complete
96: _waitForCaptureComplete.WaitOne();
97: }
98: }
99: //ここかから再度連写する
100: //set SS 1/500
101: SpeedMode.Index = 31;
102: _device.SetEnum(eNkMAIDCapability.kNkMAIDCapability_ShutterSpeed, SpeedMode);
103:
104: //Set Sensitivity 140
105: ISOSens.Index = 3;
106: _device.SetEnum(eNkMAIDCapability.kNkMAIDCapability_Sensitivity, ISOSens);
107:
108: // Set number of continuous captures - in this case we want 50 でも15コマしか撮れない
109: _device.SetUnsigned(eNkMAIDCapability.kNkMAIDCapability_ContinuousShootingNum, 50);
110:
111: // Hook up capture events
112: _device.ImageReady += _device_ImageReady;
113: //_device.CaptureComplete += _device_CaptureComplete;
114:
115: // Capture
116: _device.Capture();
117:
118: // Wait for the capture to complete
119: _waitForCaptureComplete.WaitOne();
120:
121: // Shutdown
122: manager.Shutdown();
123: }
124: catch (NikonException ex)
125: {
126: Console.WriteLine(ex.Message);
127: }
128: }
129:
130: void _device_ImageReady(NikonDevice sender, NikonImage image)
131: {
132: // Save captured image to disk
133: string filename = "image" + _captureCount.ToString() + ((image.Type == NikonImageType.Jpeg) ? ".jpg" : ".nef");
134: _captureCount++;
135:
136: using (FileStream s = new FileStream(filename, FileMode.Create, FileAccess.Write))
137: {
138: s.Write(image.Buffer, 0, image.Buffer.Length);
139: }
140: }
141:
142: void _device_CaptureComplete(NikonDevice sender, int data)
143: {
144: // Signal the the capture completed
145: _waitForCaptureComplete.Set();
146: }
147:
148: void manager_DeviceAdded(NikonManager sender, NikonDevice device)
149: {
150: if (_device == null)
151: {
152: // Save device
153: _device = device;
154:
155: // Signal that we got a device
156: _waitForDevice.Set();
157: }
158: }
159: }
160:
161: class Program
162: {
163: staticvoid Main(string[] args)
164: {
165: DemoContinuousCapture demo = new DemoContinuousCapture();
166: demo.Run();
167: }
168: }
169: }