Add a crane to focus on the ZBarReaderViewController

I use zbarsdk to read barcodes. It works fine, but my problem is that I added an overlay look at the ZBarReaderViewController (reader in my code). So now I tried to add a focusing crane. But it is crumbling. Below is my code. Thank you in advance. Any ideas would be appreciated.

-(IBAction)scanBarCode:(id)sender { barcodeClicked = 1; NSLog(@"TBD: scan barcode here..."); // ADD: present a barcode reader that scans from the camera feed reader = [ZBarReaderViewController new]; reader.readerDelegate = self; reader.supportedOrientationsMask = ZBarOrientationMaskAll; ZBarImageScanner *scanner = reader.scanner; // TODO: (optional) additional reader configuration here // EXAMPLE: disable rarely used I2/5 to improve performance [scanner setSymbology: ZBAR_I25 config: ZBAR_CFG_ENABLE to: 0]; reader.showsZBarControls = NO; UIView *ovlView = [[UIView alloc] init]; [ovlView setFrame:CGRectMake(0, 0, 320, 480)]; [ovlView setBackgroundColor:[UIColor clearColor]]; UIImageView *leftBracket = [[UIImageView alloc] init]; [leftBracket setFrame:CGRectMake(21, 100, 278, 15)]; [leftBracket setImage:[UIImage imageNamed:@"TopBracket.png"]]; UIImageView *rightBracket = [[UIImageView alloc] init]; [rightBracket setFrame:CGRectMake(21, 240, 278, 15)]; [rightBracket setImage:[UIImage imageNamed:@"BottomBracket.png"]]; UIToolbar *bottomBar = [[UIToolbar alloc] init]; [bottomBar setBarStyle:UIBarStyleBlackOpaque]; if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone && [UIScreen mainScreen].bounds.size.height * [UIScreen mainScreen].scale >= 1136) { [bottomBar setFrame:CGRectMake(0, 524, 320, 44)]; } else [bottomBar setFrame:CGRectMake(0, 436, 320, 44)]; UIBarButtonItem *cancel = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancelCamera)]; /*UIBarButtonItem *flexItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];*/ //UIBarButtonItem *infoButton = [[UIBarButtonItem alloc] initWithTitle:@" Info " style:UIBarButtonItemStyleBordered target:self action:@selector(infoButton)]; /*UIButton *info = [UIButton buttonWithType:UIButtonTypeInfoLight]; [info addTarget:self action:@selector(infoButton) forControlEvents:UIControlEventTouchUpInside]; UIBarButtonItem *infoButton = [[UIBarButtonItem alloc] initWithCustomView:info]; [bottomBar setItems:[NSArray arrayWithObjects:cancel,flexItem,infoButton, nil]];*/ [bottomBar setItems:[NSArray arrayWithObjects:cancel, nil]]; [ovlView addSubview:leftBracket]; [ovlView addSubview:rightBracket]; [ovlView addSubview:bottomBar]; reader.cameraOverlayView = ovlView; // present and release the controller [self presentModalViewController:reader animated: YES]; [reader release]; } -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { UIView * previewView = [[[[[[[[[[ reader.view // UILayoutContainerView subviews] objectAtIndex:0] // UINavigationTransitionView subviews] objectAtIndex:0] // UIViewControllerWrapperView subviews] objectAtIndex:0] // UIView subviews] objectAtIndex:0] // PLCameraView subviews] objectAtIndex:0]; // PLPreviewView [previewView touchesBegan:touches withEvent:event]; } 
+4
source share
2 answers

Thanks for the answer MacN00b! This indicates the right direction for me. I implemented tap-focus for zbarViewController. Here is an idea:

You can add a custom view to zbarViewController by assigning a custom view to its CameraOverlayView. Then add the TapGestureRecagonizer to your custom view to catch the tap. Then get the touch point and make the camera focus tangent. You would like to add a small rectangle around the touch point (this is what I did).

Here comes the code (assigning a custom view for the OverlayView camera:

 UIView *view = [[UIView alloc] init]; UITapGestureRecognizer* tapScanner = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(focusAtPoint:)]; [view addGestureRecognizer:tapScanner]; reader.cameraOverlayView = view; 

Then in the focusAtPoint selector:

 - (void)focusAtPoint:(id) sender{ CGPoint touchPoint = [(UITapGestureRecognizer*)sender locationInView:_reader.cameraOverlayView]; double focus_x = touchPoint.x/_reader.cameraOverlayView.frame.size.width; double focus_y = (touchPoint.y+66)/_reader.cameraOverlayView.frame.size.height; NSError *error; NSArray *devices = [AVCaptureDevice devices]; for (AVCaptureDevice *device in devices){ NSLog(@"Device name: %@", [device localizedName]); if ([device hasMediaType:AVMediaTypeVideo]) { if ([device position] == AVCaptureDevicePositionBack) { NSLog(@"Device position : back"); CGPoint point = CGPointMake(focus_y, 1-focus_x); if ([device isFocusModeSupported:AVCaptureFocusModeContinuousAutoFocus] && [device lockForConfiguration:&error]){ [device setFocusPointOfInterest:point]; CGRect rect = CGRectMake(touchPoint.x-30, touchPoint.y-30, 60, 60); UIView *focusRect = [[UIView alloc] initWithFrame:rect]; focusRect.layer.borderColor = [UIColor whiteColor].CGColor; focusRect.layer.borderWidth = 2; focusRect.tag = 99; [_reader.cameraOverlayView addSubview:focusRect]; [NSTimer scheduledTimerWithTimeInterval: 1 target: self selector: @selector(dismissFocusRect) userInfo: nil repeats: NO]; [device setFocusMode:AVCaptureFocusModeAutoFocus]; [device unlockForConfiguration]; } } } } } 

I added a white rectangle around the touch point, and then use the rejectFocusRect selector to reject this rectangle. Here is the code:

 - (void) dismissFocusRect{ for (UIView *subView in _reader.cameraOverlayView.subviews) { if (subView.tag == 99) { [subView removeFromSuperview]; } } } 

Hope this helps!

+10
source

See this documentation with apple in the “Focus Modes” section: https://developer.apple.com/library/ios/#documentation/AudioVideo/Conceptual/AVFoundationPG/Articles/04_MediaCapture.html It talks about how to implement cranes for proper focus, I would try to implement this with

 CGRect screenRect = [[UIScreen mainScreen] bounds]; screenWidth = screenRect.size.width; screenHeight = screenRect.size.height; double focus_x = thisFocusPoint.center.x/screenWidth; double focus_y = thisFocusPoint.center.y/screenHeight; [[self captureManager].videoDevice lockForConfiguration:&error]; [[self captureManager].videoDevice setFocusPointOfInterest:CGPointMake(focus_x,focus_y)]; 

Well, if you use this view controller, how about adding (void), which should be well implemented in the barcodeviewcontroller.

 - (void) focusAtPoint:(CGPoint)point { AVCaptureDevice *device = [[self videoInput] device]; if ([device isFocusPointOfInterestSupported] && [device isFocusModeSupported:AVCaptureFocusModeAutoFocus]) { NSError *error; if ([device lockForConfiguration:&error]) { [device setFocusPointOfInterest:point]; [device setFocusMode:AVCaptureFocusModeAutoFocus]; [device unlockForConfiguration]; } else { id delegate = [self delegate]; if ([delegate respondsToSelector:@selector(acquiringDeviceLockFailedWithError:)]) { [delegate acquiringDeviceLockFailedWithError:error]; } } } } 
+1
source

All Articles