Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS: ZBar SDK unicode characters

When scanning QR codes with ZBar the string resulting from the process does not display unicode characters properly. The word Márti encoded as a QR code by any free to use QR code generator (like http://qrcode.kaywa.com) would result in Mテ。rti.

In other SO questions (1, 2) it was suggested to embed a BOM at the start of the resulting string, but doing this:

NSString *qrString = [NSString stringWithFormat:@"\xEF\xBB\xBF%@",symbol.data];

or this:

NSString *qrString = [[NSString alloc] initWithFormat:@"\357\273\277%@", symbol.data];

resulted in the same, flawed result with the Asian character. symbol.data is the resulting NSString provided by ZBar.

UPDATE: Based on dda's answer, the solution was the following:

NSString *qrString = symbol.data;
//look for misinterpreted acute characters and convert them to UTF-8
if ([qrString canBeConvertedToEncoding:NSShiftJISStringEncoding]) {
  qrString = [NSString stringWithCString:[symbol.data cStringUsingEncoding: NSShiftJISStringEncoding] encoding:NSUTF8StringEncoding];
}
like image 986
Zoltán Matók Avatar asked Nov 15 '12 10:11

Zoltán Matók


2 Answers

According to the Wikipedia page about QR, the encoding of binary data [for which Márti would apply] is ISO 8859-1. It could be an encoding-as-unicode-encoding problem. But seeing a kanji there, it could be that the problem is an encoding-as-QR-encoding issue: maybe the text, being not ASCII, is encoded by default as Shift JIS X 0208 (ie kanji/kana).

like image 184
dda Avatar answered Sep 30 '22 18:09

dda


I could create QR codes of "日本語"(japanese) and "Márti" with following libraries:

  • iOS-QR-Code-Encoder
  • QR-Code-Encoder-for-Objective-C.

You can read those QR codes with ZBar.

iOS-QR-Code-Encoder:

NSString* orginalString = @"Márti"(or "日本語"(japanese));  
NSString *data = [[NSString alloc] initWithFormat:@"\357\273\277%@", orginalString];  
UIImage* qrcodeImage = [QRCodeGenerator qrImageForString:data imageSize:imageView.bounds.size.width];

QR-Code-Encoder-for-Objective-C:

NSString* orginalString = @"Márti"(or "日本語"(japanese));
NSString *data = [[NSString alloc] initWithFormat:@"\357\273\277%@", orginalString];

//first encode the string into a matrix of bools, TRUE for black dot and FALSE for white. Let the encoder decide the error correction level and version
DataMatrix* qrMatrix = [QREncoder encodeWithECLevel:QR_ECLEVEL_AUTO version:QR_VERSION_AUTO string:data];

//then render the matrix
UIImage* qrcodeImage = [QREncoder renderDataMatrix:qrMatrix imageDimension:qrcodeImageDimension];
like image 28
dadachi Avatar answered Sep 30 '22 17:09

dadachi