HUAWEI ML Kit支持拍照购物功能,通过拍摄商品图片,在预先建立的商品图片库中在线检索同款或相似商品,返回相似商品ID和相关信息。
在这个codelab中,您将完成一个拍照购物的demo工程。
点击以下链接,下载codelab的demo工程包:
Demo工程包
解压下载的压缩包到本地,构建并用Xcode打开demo
由于真机调试需要证书,所以调试之前一定要确保证书和配置文件对应。
在demo工程TARGETS下,点击Signing&Capabilities,选择Debug,展开Signing,勾选Automatically manage signing。
如果您有对应的调试证书和配置文件(对应企业账号或者个人账号),那么请在展开的Signing下选择对应的Team,输入Bundle Identifier。如果证书、配置文件以及Bundle Identifier等不对应,就会显示错误并无法进行真机调试。错误示例如下:
如果您没有对应的证书和配置文件,就不能调试了吗?答案是否定的。
苹果已经考虑到了这一点,只需要您有一个Apple ID账号,就可以为我们免费提供调试机会。
首先选择Xcode下的Preferences选项,点击Accounts下的➕按钮,选择Apple ID,按照提示输入账号和密码,添加完成后便可在展开的Signing下配置证书。
接着您需要输入一个Bundle Identifier值(Bundle Identifier值是唯一的),选择对应的Team,此时Xcode会为您自动生成证书和配置文件。
如果您遇到这样类似的错误:
那么可能您输入的Bundle Identifier已经被占用,此时您需要更换一个Bundle Identifier值。
另外要注意,苹果的免费调试次数是有限制的,如果提示您超过了最大次数,您可以换一个Apple ID账号再进行调用。
证书配置完成后,您可以按住Command+B键编译demo,没有显示报错即代表编译成功。
如果您已经添加过此权限可以忽略此步骤。
在demo工程目录下,选择info.plist文件,右击选择Open As,选择Source Code,增加相机和摄像头的权限。
<key>NSCameraUsageDescription</key>
<string>App needs your consent to access the camera</string>
<key>NSPhotoLibraryUsageDescription</key>
<string>App needs your consent to access the album</string>
在此步骤中,我们将在App中配置检测参数。
MLDocumentViewController类文件中的requestImage方法:
MLDocumentViewController.m
MLRemoteProductVisionSearchAnalyzerSetting *setting = [[MLRemoteProductVisionSearchAnalyzerSetting alloc] init];
setting.maxResult = 20;
// setting.productSetId = @"**********";
[MLRemoteProductVisionSearchAnalyzer setRemoteProductVisionSearchAnalyzerSetting:setting];
在此步骤中,我们将在App中调用检测的接口。
MLDocumentViewController类的requestImage方法中:
MLDocumentViewController.m
[MLRemoteProductVisionSearchAnalyzer asyncAnalyseImage:self.selectedImage addOnSuccessListener:^(MLProductVisionSearch * _Nonnull productModel) {
// 成功后的结果显示
NSTimeInterval codeTime = -[startTime timeIntervalSinceNow];
self->navView.timeShowLabel.text = [NSString stringWithFormat:@"%.4f S",codeTime];
self->list.selectedImage = self.selectedImage;
self->list.dataArr = productModel;
if(productModel.productList&&productModel.productList.count>0){
MLProductSearchView *searchView = [[MLProductSearchView alloc] initWithFrame:self.fullImageview.frame box:@[@(productModel.border.left_top_x),@(productModel.border.left_top_y),@(productModel.border.right_bottom_x),@(productModel.border.right_bottom_y)]];
searchView.imageSize = [self calculateTextScale:self.selectedImage];
[self.view addSubview:searchView];
searchView.backgroundColor = [UIColor clearColor];
}
UIView *maskView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kMLDocumentDeviceWidth, kMLDocumentDeviceHeight)];
maskView.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.3];
maskView.userInteractionEnabled = YES;
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClick:)];
[maskView addGestureRecognizer:tapGesture];
[self.view addSubview:maskView];
[self.view addSubview:self->list];
[self addImageView];
} addOnFailureListener:^(NSInteger errCode, NSString * _Nonnull errMsg) {
// 成功后的显示
NSTimeInterval codeTime = -[startTime timeIntervalSinceNow];
self->navView.timeShowLabel.text = [NSString stringWithFormat:@"%.4f S",codeTime];
}]
恭喜您,您已经成功完成了此次codelab任务,并学到了以下技能:
本文介绍的工程仅用于简单演示,实际开发过程中应严格遵循开发指南。访问以下链接可以获取详细信息: