hikmat_11
hikmat_11

Reputation: 87

Cannot read QR Code with ASCII characters using AVFoundation

-(void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection{
unsigned char myKey[2];
myKey[0]=1;
myKey[1]=2;
if(metadataObjects == nil  || [metadataObjects count] ==0)
{
    qrCodeFrameView.frame=CGRectZero;
}
if (metadataObjects != nil && [metadataObjects count] > 0) {
    AVMetadataMachineReadableCodeObject *metadataObj = [metadataObjects objectAtIndex:0];
    if ([[metadataObj type] isEqualToString:AVMetadataObjectTypeQRCode]) {
        Encoder *encoder=[[Encoder alloc] init];
        NSLog(@"0000-----%@",[metadataObj stringValue]);
        unsigned char buffer_to_decrypt=[metadataObj stringValue];
        [encoder tripledes_decrypt:buffer_to_decrypt lenght:sizeof(buffer_to_decrypt) key:myKey];
        NSData *data = [NSData dataWithBytes:buffer_to_decrypt length:sizeof(buffer_to_decrypt)];
        NSLog(@"data = %@", data);
        NSString *result=[[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
        NSLog(@"-----<<<<result>>>>%@",result);
        AVMetadataMachineReadableCodeObject *barCodeObj=(AVMetadataMachineReadableCodeObject *)[_videoPreviewLayer transformedMetadataObjectForMetadataObject:metadataObj];
        qrCodeFrameView.frame=barCodeObj.bounds;
        AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
        [self stopReading];
        _isReading = NO;
    }
}}

Here I'm able to read normal QR Codes. But [metadataObj stringValue] is returning nil, when reading ASCII characters from QR Code. I use this to encrypt data in QR Code so that only my app can read my QR Code. How can I read ASCII characters? this is my encrypted QR Code QR Code image

Upvotes: 1

Views: 299

Answers (1)

Larme
Larme

Reputation: 26006

It wouldn't surprise me that Apple's SDK only transform using UTF8 encoding.

After downloading the app on a old iTunes and exploring it, I found references to ZXingObjc and ZBarSDK which I guess are used to read the QRCode, that would explain why the app can read it and yours can't.

So you can either use these third party library, or if you want to keep using the Apple's one, here is a possible solution:

NSString <=> EncryptedString <=> Base64EncryptedString <=> QRCode

Or wait for Apple to gives instead of only [metadataObj stringValue], [metadataObj stringValueUsingEncoding:], or [metadataObj rawData](that you can after used with [[NSString alloc] initWithData:[metadataObj rawData] encoding:NSASCIIEncoding]).

Upvotes: 1

Related Questions