Abstract: I describe how to capture video using MonoTouch. Source code of a sample application shown in a left picture has been pushed on github [1] under the new BSD license. The sample application compiled with MonoTouch 1.4.6 results in 20 fps (iPhone 3GS) and 9 fps (iPhone 3G). It would be a good starting point in a development of augmented reality or video streaming application on MonoTouch.
Later iPhone 3.0 SDK, objective-c video capturing applications uses an undocumented method UIGetScreenImage() to get successive screen images. In MonoTouch, CGImage.ScreenImage property which calls UIGetScreenImage() and creates a CGImage instance can be used as a following simple code:
1: using (var screenImage = CGImage.ScreenImage.WithImageInRect(Frame))
2: {
3: // without calling dispose() , app fails within a minute.
4: if (null != _imageView.Image)
5: _imageView.Image.Dispose();
6: _imageView.Image = UIImage.FromImage(screenImage);
7: }
Points in this code are: 1) to clip screen image CGImage.WithImageInRect() method is used because ScreenImage property gives whole screen image, and 2) use "using" directive to release memory buffer as fast as possible.
Calling Dispose() method explicitly is important point because Dispose() sends an objective-c "release" message to a memory buffer which ScreenImage property allocated sending a retain message. When it does not call dispose() method explicitly, even though iPhone (3GS) has plenty of application memory, application fails due to memory shortage before getting ~25 shots. Besides we have to override ReceiveMemoryWarning() method of UIApplicationDelegate class as following:
1: /// <summary>
2: /// GC is required to allocate a memory buffer in camera capturing class.
3: /// </summary>
4: public override void ReceiveMemoryWarning(UIApplication application)
5: {
6: System.GC.Collect();
7: }
Overlaying thumbnail screenshot view as show in above picture is quite easy, just setting a view instance to CameraOverlayView property of UIImagePickerViewController class. Overlay view should be transparent so the camera preview shows through it.
1: // important - it needs to be transparent so the camera preview shows through!
2: Opaque = false;
3: BackgroundColor = UIColor.Clear;
Some other points in camera view overlaying are: 1) height of camera control toolbar is 54px (not 44px), 2) show a alert modal dialog when hardware does not has a camera, and 3)application compiled in a debug configuration sometimes fails but does not with a release configuration. Sample code shown here [2] is useful to determine iPhone hardware model.
References
- Sample code on github. http://github.com/reinforce-lab/com.ReinforceLab.MonoTouch.Controls/tree/master/VideoCaptureSample/
- MonoTouch get hardware version.http://snippets.dzone.com/user/zachgris
3 コメント: