扫描此(https://www.barcoderobot.com/static/bcgen/01026/478064310081_3b120.jpg?preview=True)条码,我们应该获得以下结果:47806431008
使用iOS7 API读取条形码,我得到以下结果:047806431008
任何想法如何处理?
部分代码:
CGRect highlightViewRect = CGRectZero;
AVMetadataMachineReadableCodeObject *barCodeObject;
NSString *detectionString = nil;
NSArray *barCodeTypes = @[AVMetadataObjectTypeUPCECode, AVMetadataObjectTypeCode39Code, AVMetadataObjectTypeCode39Mod43Code,
AVMetadataObjectTypeEAN13Code, AVMetadataObjectTypeEAN8Code, AVMetadataObjectTypeCode93Code, AVMetadataObjectTypeCode128Code,
AVMetadataObjectTypePDF417Code, AVMetadataObjectTypeQRCode, AVMetadataObjectTypeAztecCode];
for (AVMetadataObject *metadata in metadataObjects) {
for (NSString *type in barCodeTypes) {
if ([metadata.type isEqualToString:type])
{
barCodeObject = (AVMetadataMachineReadableCodeObject *)[_prevLayer transformedMetadataObjectForMetadataObject:(AVMetadataMachineReadableCodeObject *)metadata];
highlightViewRect = barCodeObject.bounds;
detectionString = [(AVMetadataMachineReadableCodeObject *)metadata stringValue];
break;
}
}
if (detectionString != nil)
{
[_session stopRunning];
[self scanResult:detectionString];
break;
}
最佳答案
我发现了问题。苹果只需添加一个前导零就可以将每个UPC-A条码转换为EAN13。
解决方案是验证条形码是否为EAN13,以及字符串结果中是否带有前导零,可以安全地将其删除并获得UPC-A条形码。
if(detectionString!=nil){
if([metadata.type isEqualToString:AVMetadataObjectTypeEAN13Code]){
if ([detectionString hasPrefix:@"0"] && [detectionString length] > 1)
detectionString = [detectionString substringFromIndex:1];
}
}
链接到Apple技术说明:https://developer.apple.com/library/content/technotes/tn2325/_index.html#//apple_ref/doc/uid/DTS40013824-CH1-IS_UPC_A_SUPPORTED_
更多信息,请访问:http://www-01.ibm.com/support/docview.wss?uid=pos1R1002813
关于iOS7条码扫描器API为UPCA条码格式添加了零,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22767584/