Friday, May 15, 2009

iPhone:Using PLCameraController


The PLCameraController class is for controlling iPhone's built-in camera and one of Apple's private frameworks. If you want to get the preview of incoming video stream from the camera, it is quite easy to do it. Here are steps to do it. 

1. Get the header of PLCameraController. This can be obtained by dumping iPhone OS frame works by using class-dump-X, or you can download it from somewhere.  

2. Add the PhotoLibrary framework from the SDK directory. With the SDK 2.2.1, the framework is in /Developer/Platforms/iPhoneOS.platform/ Developer/SDKs/iPhoneOS2.0.sdk/System/Library/
PrivateFrameworks/PhotoLibrary.framework

3. Import the PLCameraController header to your application delegate. 

4. Add the following code to 'applicationDidFinishLaunching' 


- (void)applicationDidFinishLaunching:(UIApplication *)application { 

// Hide the status bar 

[[UIApplication sharedApplication] setStatusBarHidden:YES animated:NO];

// Get the view for preview and start preview 

PLCameraController *cam = [PLCameraController sharedInstance]; 

UIView *view = [cam previewView]; 

[cam startPreview]; 


// Add the preview view to window 

[window addSubview:view]; 

// Override point for customization after app launch 

[window makeKeyAndVisible]; 


Now, run the application then you will see something like the screen show below. 


Basically, the resolution of the video coming from the camera is 304x400, which is a little weird. The preview video stream is resized in the preview view. If we do not set a new frame size, it is resized with a fixed aspect ratio and thus, there are white area at the bottom of the screen. 


For full screen preview, set the frame size of the preview view like this : 

view.frame = CGRectMake(0,0, 320, 480) ;


Update :  I tried several tests and it turns out that the preview is actually 304x400.



 

14 comments:

  1. Hi- you wouldn't happen to know how to get access to the data of the image for processing, would you? I had it working on OS 2.2 using a callback installed on the cameradevice, but that doesn't work on OS3.0 anymore. How are you reading the actual pixels? Thanks!

    ReplyDelete
  2. Hello!

    Just curious: have you tried this in iPhoneOS 3.0 (or, indeed on the iPhone 3GS)? I've been trying to achieve this, but I can't find the right magic to get the camera properly initialized - it seems that just calling startPreview isn't enough.

    Thanks for any thoughts you can provide!

    Regards,
    Chris

    ReplyDelete
  3. This is definatelly not working on 3.0 this way. Any ideas how to fix issue above?

    ReplyDelete
  4. Josh/ Sorry, I haven't tried video capturing on 3.0.

    ReplyDelete
  5. On 3.0, startPreview does not work.
    There are some posts talking about this issue. They say that 'loadView' is enough. I found this thread: http://www.modmyi.com/forums/iphone-ipod-touch-sdk-development-discussion/642261-cameracontroller-3gs-3-0-a-2.html

    ReplyDelete
  6. HELP!!!

    I've included PhotoLibrary.framework to my project and used class-dump-X to get the header PLCameraController. But how shall include the the header in the project?
    I've added the header to the project and imported it like this:
    #import "PLCameraController.h" but by doing that I get the error message:
    error: ___CFConstantStringClassReference.h: No such file or directory which is imported in PLCameraController.h.

    Any ideas? I'm developing on 3.0

    ReplyDelete
  7. I am not getting the video displayed.
    i get blank black screen after dumping to the phone.

    ReplyDelete
  8. I am not getting the images on the camera and i can see only blank screen. Any help can be appreciated.but after i have used the [perviewView retain]; i am getting error as undeclared first use in this function.

    ReplyDelete
  9. Wonwoo Lee thanks!
    this is info is like a ray of light in the dark!!!
    But Did anyone really achieve it in iPhone 3G of 3GS with iPhone OS 3.x ?
    I am having a hard time with this.

    ReplyDelete
  10. Use [cameraController retain] instead of [previewView retain]

    ReplyDelete
  11. "Get the header of PLCameraController. This can be obtained by dumping iPhone OS frame works by using class-dump-X, or you can download it from somewhere. "
    Not sure what that means

    ReplyDelete
  12. I have downloaded class-dump and now what do i do to dump the photolibray fremwork?

    ReplyDelete