iosobjective-cencodingutf-8qr-code

iOS native QRCode scanner in Chinese Encoding


This QRCode creates by Chinese Big5 Encoding not UTF-8, I am using iOS native class, AVMetadataMachineReadableCodeObject, try to get the result, but the string unreadable, like following:

**·s¦¡Àô«O­I:1:2:¯Â³ð¯ùºñ¯ù:2:20:¨Å­»¥@®a¥þ:1:83:­ìµÑ¤é¦¡ºñ:2:25:¥Í¬¡ªwªjºñ:2:10

I tried to convert the string to NSData by UTF-8, then I convert again from NSData to NSString, by others Encoding, and I thnik the original string already get wrong encoding that is why I can convert to the correct string.

This is correct content:

**新式環保背:1:2:純喫茶綠茶:2:20:乳香世家全:1:83:原萃日式綠:2:25:生活泡沫綠:2:10

The QRCode is here

Does any way to get correct result using iOS native class? thanks


Solution

  • I find the answer, That QRCode contents string encoding is useing NSISOLatin1StringEncoding, not NSUTF8StringEncoding.

    Code:

    - (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection
    {
    
        NSMutableString *message = [NSMutableString string];
        for (AVMetadataMachineReadableCodeObject *codeObject in metadataObjects) {
    
            NSInteger index = [metadataObjects indexOfObject:codeObject];
    
            NSString *readedString = codeObject.stringValue;
    
            // Restore raw data using NSISOLatin1StringEncoding.
            NSData *dataString = [readedString dataUsingEncoding:NSISOLatin1StringEncoding allowLossyConversion:NO];
    
            // Create big5 encoding.
            NSStringEncoding encoding = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingBig5_HKSCS_1999);
    
            // Decode data using big5 encoding.
            readedString = [[NSString alloc] initWithData:dataString encoding:encoding];
    
            [message appendFormat:@"%zd. \"%@\"\n\n", index, readedString];
        }
    
        NSLog(@"%@", message);
    }