NemesisDoom
NemesisDoom

Reputation: 596

Showing UIView inside a Camera View

I'm making an app that has a camera preview in a View, inside this View I want to draw the camera data and also another View that shows a little rectangle when data it's captured, for example, in a BarCode scanning Scenario the Camera is shown in a view, when a BarCode it's found a Rectangle will be drawn showing that it has scanned a Barcode. My current View Hierarchy it's the following:

View
{
  -UIView cameraHolder
   {
        -UIView highlightView
   }
}

I'm managed to get the camera showed and scanning things, but the highlight View it's not being shown, why is this happening?

This is the code for initializing the highlight View:

-(void)setUpHiglightView{
    self.highlightView = [[UIView alloc] init];
    self.highlightView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin|UIViewAutoresizingFlexibleBottomMargin;
    self.highlightView.layer.borderColor = [UIColor greenColor].CGColor;
    self.highlightView.layer.borderWidth = 3;
}

and this is the code for when a data it's captured:

-(void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection{
    CGRect highlightViewRect = CGRectZero;
    AVMetadataMachineReadableCodeObject *barCodeObject;
    NSString *detectionString = nil;

    NSArray *barCodeTypes = @[AVMetadataObjectTypeUPCECode, AVMetadataObjectTypeCode39Code, AVMetadataObjectTypeCode39Mod43Code,
                              AVMetadataObjectTypeEAN13Code, AVMetadataObjectTypeEAN8Code, AVMetadataObjectTypeCode93Code, AVMetadataObjectTypeCode128Code,
                              AVMetadataObjectTypePDF417Code, AVMetadataObjectTypeQRCode, AVMetadataObjectTypeAztecCode];

    for(AVMetadataObject *metadata in metadataObjects){
        for(NSString *type in barCodeTypes){
            if([metadata.type isEqualToString:type]){
                barCodeObject = (AVMetadataMachineReadableCodeObject *)[prevLayer transformedMetadataObjectForMetadataObject:(AVMetadataMachineReadableCodeObject*)metadata];
                highlightViewRect = barCodeObject.bounds;
                detectionString = [(AVMetadataMachineReadableCodeObject*)metadata stringValue];
                break;
            }
        }
    }


    if(detectionString != nil){
        [self.itemIdTextField setText:detectionString];
    }else{
        //NSLog(@"Got Nothing");
    }

    NSLog(@"Position: [%f,%f][%f,%f]",highlightViewRect.origin.x, highlightViewRect.origin.y,highlightViewRect.size.height, highlightViewRect.size.width);

    self.highlightView.frame = highlightViewRect;
}

Also the code that initializes the camera:

-(void)setupBarCodeScanner{
    [self setUpHiglightView];

    session = [[AVCaptureSession alloc] init];
    device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    NSError *error = nil;

    input = [AVCaptureDeviceInput deviceInputWithDevice:device error:&error];
    if(input){
        [session addInput:input];
    }else{
        [self showAlertDialogWithTitle:@"Error" andMessage:@"There was an error while accessing your camera"];
        NSLog(@"Error: %@", error);
    }

    output = [[AVCaptureMetadataOutput alloc] init];
    [output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
    [session addOutput:output];

    output.metadataObjectTypes = [output availableMetadataObjectTypes];

    prevLayer = [AVCaptureVideoPreviewLayer layerWithSession:session];
    prevLayer.frame = self.cameraHolder.bounds;
    prevLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
    [self.cameraHolder.layer addSublayer:prevLayer];
}

Thank you very much!

Upvotes: 0

Views: 759

Answers (2)

David P
David P

Reputation: 617

Add self.highlightView to self.view at the end of captureOutput:

[self.view addSubview:self.highlightView];

Upvotes: 1

drhr
drhr

Reputation: 2281

It doesn't look like you're adding that view anywhere. You create it and set its frame, but I'm not seeing where you're adding to the view hierarchy.

Upvotes: 0

Related Questions