4

zbarsdk を使用してバーコードを読み取ります。正常に動作していますが、私の問題は、オーバーレイ ビューを ZBarReaderViewController (私のコードのリーダー) に追加したことです。そこで、タップしてフォーカス機能を追加しようとしました。しかし、クラッシュしています。以下は私のコードです。前もって感謝します 。どんなアイデアでも感謝します。

-(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

2 に答える 2

10

MacN00b さんの回答ありがとうございます。それは私にとって正しい方向性を示しています。zbarViewController のタップフォーカスを実装しました。これがアイデアです:

カスタム ビューを cameraOverlayView に割り当てることで、zbarViewController にカスタム ビューを追加できます。次に、TapGestureRecagonizer をカスタム ビューに追加して、タップをキャッチします。次に、タッチ ポイントを取得し、カメラの焦点をタッチ ポイントに合わせます。タッチポイントの周りに小さな長方形を追加したいかもしれません(それが私がしたことです)。

ここにコードがあります(カスタムビューをcameraOverlayViewに割り当てます:

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

次に、セレクターfocusAtPointで:

- (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];
                }
            }
        }
    }
}

タッチ ポイントの周りに白い四角形を追加し、セレクターの rejectFocusRect を使用してこの四角形を閉じます。コードは次のとおりです。

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

これが役立つことを願っています!

于 2013-01-31T14:38:13.643 に答える