Tuesday, February 2, 2010

MonoTouch video capture in c# code

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



3 コメント:

  1. Hi,

    Can we use Monotouch for live streamign video from iPhone camera?

    Regards,
    Susanta
    ReplyDelete
  2. Live streaming from iPhone camera is possible. There are two technical methods:

    1. Motion jpeg. iPhone 3GS can convert captured a camera image in JPEG at 14 FPS (iPhone 3G, 7FPS).
    2. H.264 streaming. Video encoder like ffmpeg (the most famous open-source encoder) is required.

    Former method is easy and can implement in a few days.

    Latter is the best method because iPhone video player supports that format. I have been looking for iPhone live video streaming sample code but I can find nothing. If there is a obj-c working code, I can implement it within a week.
    ReplyDelete
  3. streaming videos are very popular in internet online videos is really very useful tips
    ReplyDelete