Lê Khánh Vinh Lê Khánh Vinh - 1 month ago 13
iOS Question

IOS barcode scanning api capable of reading 3 barcode at the same time

The question is seem to be a bit abstract but generally I need to develop a barcode scanning app that is capable of detecting multiple barcodes (up to 3) at the same time. Any one know any good api for the task? (Swift or objective C is OK). Any help is much appreciate! Thanks. I already have an app with Zxing api but seem does not have an option for detecting multiple barcodes.

Answer

You need AVFoundation! AVFoundation can scan barcodes.

Primary interest in this function:

#pragma mark - AVCaptureMetadataOutputObjectsDelegate methods
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection {
//THIS YOU CAN USE ARRAY OF AVMetadataObjectType
    NSString *supportedBarcodeType = AVMetadataObjectTypeCode128Code;

    for (AVMetadataObject *barcodeMetadata in metadataObjects) {
        if ([barcodeMetadata.type isEqualToString:supportedBarcodeType]) {
            AVMetadataMachineReadableCodeObject *barcodeObject = (AVMetadataMachineReadableCodeObject *)[self.captureLayer transformedMetadataObjectForMetadataObject:barcodeMetadata];
        //barcode is FIND!
            NSString *capturedBarcode = [barcodeObject stringValue];

            return;
        }
    }
}

Configuration method:

static char * const metadataOutputQueue = "metadataOutputQueue";

- (void)configureSessionWithError:(NSError **)error {    

    self.captureSession = [[AVCaptureSession alloc] init];

    NSError *inputError = nil;
    AVCaptureDevice *captureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:captureDevice error:&inputError];

    if ( nil != inputError || nil == input) {
        *error = inputError;
        return;
    }

    [self.captureSession addInput:input];

    AVCaptureMetadataOutput *captureMetadataOutput = [[AVCaptureMetadataOutput alloc] init];
    [self.captureSession addOutput:captureMetadataOutput];

    dispatch_queue_t dispatchQueue = dispatch_queue_create(metadataOutputQueue, NULL);
    [captureMetadataOutput setMetadataObjectsDelegate:self queue:dispatchQueue];
//THIS YOU CAN ADD ARRAY OF AVMetadataObjectType FOR DETECTING BARCODES
    [captureMetadataOutput setMetadataObjectTypes:@[AVMetadataObjectTypeCode128Code]];

    self.captureLayer = [AVCaptureVideoPreviewLayer layerWithSession:self.captureSession];
    [self.captureLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];
    [self.captureLayer setFrame:self.cameraPreviewView.layer.bounds];

    [self.cameraPreviewView.layer addSublayer:self.captureLayer];

    self.sessionConfigured = YES;
}

Technical Note this! Tutorial you can find on this site

Comments