网上好多人说把下边的第一行第二个"UTF-8"改成 "GB18030",但我那样试的时候还是会有很多乱码,于是干脆都把改成了"UTF-8",居然就可以了,暂时还没有发现有乱码的情况
另外需要在程序这样写下:
for(symbol in [readscanImage:cgImageRef]){
test = YES;
NSString *textd = symbol.data;
if ([textdcanBeConvertedToEncoding:NSUTF8StringEncoding]){ //注意需要把这句话写前面
} elseif ([textd canBeConvertedToEncoding:NSShiftJISStringEncoding])
{
textd = [NSStringstringWithCString:[textd cStringUsingEncoding:
NSShiftJISStringEncoding]encoding:NSUTF8StringEncoding];
}
把我那个识别的代码都贴过来吧:
#pragma mark --
#pragma mark UIImagePickerControllerDelegate
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage * userHeadImage=[[infoobjectForKey:@"UIImagePickerControllerOriginalImage"]retain];
ZBarReaderController* read = [[[ZBarReaderControlleralloc]init]autorelease];
read.readerDelegate = self;
CGImageRef cgImageRef = userHeadImage.CGImage;
[userHeadImage release];
ZBarSymbol* symbol = nil;
BOOL test = NO;
for(symbol in [readscanImage:cgImageRef]){
test = YES;
NSString *textd = symbol.data;
if ([textdcanBeConvertedToEncoding:NSUTF8StringEncoding]) {
} elseif ([textd canBeConvertedToEncoding:NSShiftJISStringEncoding])
{
textd = [NSStringstringWithCString:[textd cStringUsingEncoding:
NSShiftJISStringEncoding]encoding:NSUTF8StringEncoding]; //这段就是网上说的转换日体编码的情况
}
NSString *message = [NSStringstringWithFormat:@"%@",textd];
// NSString *message = [NSString stringWithUTF8String:[textd UTF8String]];
// NSData *latin1Data = [textd dataUsingEncoding:NSUTF8StringEncoding];
// NSString *message = [[NSString alloc] initWithData:latin1Data encoding:NSUnicodeStringEncoding];
ShowImageQRViewController *viewCon;
if ([GNRegularisValidateURL:message]) {
viewCon = [[ShowImageQRViewControlleralloc] initCreateQRType:QRTYPE_WEB];
} else if ([messagehasPrefix:@"WIFI"]){
viewCon = [[ShowImageQRViewControlleralloc] initCreateQRType:QRTYPE_WIFI];
} else {
viewCon = [[ShowImageQRViewControlleralloc] initCreateQRType:QRTYPE_TEXT];
}
viewCon.topBackFlag = NO;
viewCon.strText = message;
viewCon.albumFlag = YES;
[picker pushViewController:viewCon animated:YES];
[viewCon release]; //cyy leak
}
if (!test) {
UIAlertView *alert = [[UIAlertViewalloc]initWithTitle:@"温馨提示"
message:@"未识别二维码请尝试重新操作"
delegate:self
cancelButtonTitle:@"确定"
otherButtonTitles:nil];
[alert show];
[alert release];
}
}