iOS: ZBar SDK unicode characters

南楼画角 提交于 2019-12-03 08:40:12

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

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];

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).

Just a word of caution, the solution as is will exclude use in Japan and scanning of QR Codes with actual Kanji coding inside. In fact it will probably create problems for any QR Code with Unicode characters inside that canBeConvertedToEncoding:NSShiftJISStringEncoding.

A more universal solution is to insert the BOM characters prior to the QR Code encoding to force UTF-8 coding (before it is created). ZBar was never the problem here, it is rooted in the creation of the QR Code.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!