Search code examples
objective-ciosuiviewcamerazbar-sdk

Embed ZBar camera in a UIView


I am using ZBarSDK for scanning QR codes using an iPad. I have this successfully working currently. However, the code I have currently opens the camera in a fullscreen modal view controller but I want to embed the camera view inside of a UIView.

This is my code as it stands now, which works fine as a modal view controller. But I want to show the camera in a UIView I have created called "showCamera". I searched the net and these forums and there were some similar questions but didn't provide code in an answer.

ZBarReaderViewController *reader = [ZBarReaderViewController new];
reader.readerDelegate = self;
reader.showsZBarControls = TRUE;
reader.cameraDevice=UIImagePickerControllerCameraDeviceFront;

reader.readerView.torchMode = 0;

ZBarImageScanner *scanner = reader.scanner;
[scanner setSymbology: ZBAR_I25
               config: ZBAR_CFG_ENABLE
                   to: 0];

// present and release the controller
[self presentModalViewController: reader animated: YES];

As I said earlier, the UIView I want to show the camera in is:

@property (strong, nonatomic) IBOutlet UIView *showCamera;

Thanks in advance!


Solution

  • Until iOS 4 the camera would always open full screen. What you can do is overlay a part of that screen with your own view. You can do this by putting your view on top of it (with a transparent area):

    reader.cameraOverlayView = myView;
    

    Since iOS 4+ you can use the readerView. Since it is just a UIView you can use it like any other view. For instance:

    readerView = [ZBarReaderView new]; readerView.frame = CGRectMake(...); // other view setup... [self.view addSubview: readerView];