為什么選EasyAR
AR的廣泛應(yīng)用
AR(Augmented Reality)即增強(qiáng)現(xiàn)實(shí),是一種實(shí)時(shí)地計(jì)算攝影機(jī)影像的位置及角度并加上相應(yīng)圖像帽氓、視頻盟猖、3D模型的技術(shù),這種技術(shù)的目標(biāo)是在屏幕上把虛擬世界套在現(xiàn)實(shí)世界并進(jìn)行互動(dòng)方援。
近幾年AR的應(yīng)用越來越廣泛没炒,樣式繁多的AR應(yīng)用場(chǎng)景給app的效果提供了更多的可能,AR的互動(dòng)性體驗(yàn)也逐漸被廣泛接受犯戏,成為用戶十分喜愛并樂于嘗試的功能送火。
主流SDK
ARToolKit
Vuforia
ARKit
ARCore
HiAR
EasyAR
EasyAR的優(yōu)勢(shì)
1. 較完善的中文文檔
1. 云識(shí)別
使用SDK創(chuàng)建EasyAR工程
資源下載與文檔查看
開發(fā)者可以登錄EasyAR官網(wǎng)查看EasyAR的服務(wù)和相關(guān)文檔;
通過資料下載可以下載相關(guān)SDK(含Unity或不含Unity)和示例程序先匪。
注冊(cè)Licence Key添加所需SDK
首先注冊(cè)Licence Key种吸,添加SDK到工程中。
所需添加其他庫如下圖:
運(yùn)行示例程序
從官網(wǎng)下載的示例程序必須運(yùn)行在真機(jī)上呀非,且需要將Enable Bitcode設(shè)置為NO坚俗。
實(shí)現(xiàn)掃描實(shí)物播放對(duì)應(yīng)視頻功能
該功能基于模式識(shí)別和AR技術(shù),官方示例程序中的HelloARVideo已經(jīng)實(shí)現(xiàn)了根據(jù)本地圖片識(shí)別播放對(duì)應(yīng)視頻的功能岸裙。在此基礎(chǔ)上猖败,本博客旨在靈活運(yùn)用EasyAR的相關(guān)功能,按類別將所要識(shí)別的目標(biāo)圖片儲(chǔ)存于云端降允,用戶可按需主動(dòng)獲取相關(guān)資源文件恩闻,在獲取目標(biāo)圖片的基礎(chǔ)下實(shí)現(xiàn)識(shí)別并在線播放對(duì)應(yīng)視頻的功能。
code
1. 從服務(wù)端下載目標(biāo)圖片壓縮包并解壓
- (void)downloadFileWithURL:(NSURL *)URL {
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
AFURLSessionManager *mannager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
hud.mode = MBProgressHUDModeAnnularDeterminate;
hud.label.text = @"正在更新資源包…";
_downloadTask = [mannager downloadTaskWithRequest:request progress:^(NSProgress * _Nonnull downloadProgress) {
if (downloadProgress) {
CGFloat currentProgress = (CGFloat)downloadProgress.completedUnitCount / downloadProgress.totalUnitCount;
dispatch_async(dispatch_get_main_queue(), ^{
hud.progress = currentProgress;
hud.label.text = [NSString stringWithFormat:@"正在更新資源包:%.0f%%\n",currentProgress * 100];
});
NSLog(@"%@",[NSString stringWithFormat:@"當(dāng)前進(jìn)度為:%.2f%%",currentProgress * 100]);
}
} destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) {
// 將zip文件下載保存到沙盒的Cache路徑下
NSString *cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
NSString *path = [cachePath stringByAppendingString:response.suggestedFilename];
return [NSURL fileURLWithPath:path];
} completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) {
[hud hideAnimated:YES];
NSString *imgFilePath = [filePath path];
NSFileManager *fileManger = [NSFileManager defaultManager];
NSString *imgaesPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
// remove
[fileManger removeItemAtPath:imgaesPath error:nil];
[fileManger createDirectoryAtPath:imgaesPath withIntermediateDirectories:YES attributes:nil error:nil];
// unzip 將zip文件解壓并保存到Document下
[SSZipArchive unzipFileAtPath:imgFilePath toDestination:imgaesPath];
// scan
ARViewController *arVC = [[ARViewController alloc] init];
[self presentViewController:arVC animated:YES completion:nil];
}];
[_downloadTask resume];
}
2. 加載targets
在helloar
的initialize
方法中使用loadFromImage
方法加載目標(biāo)圖片剧董。
for (int i = 0; i < targetCount; i++) {
loadFromImage(tracker, imageName,i);
}
在中loadFromImage
修改json生存方法
NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
easyar_ImageTarget * target = [easyar_ImageTarget create];
NSString * absolutePath = [documentPath stringByAppendingString:[NSString stringWithFormat:@"/targets/%@",name]];
NSString *uid = [NSString stringWithFormat:@"%d",index];
NSString * jstr = [@[@"{\n"
" \"images\" :\n"
" [\n"
" {\n"
" \"name\" : \"", name, @"\",\n"
" \"image\" : \"", absolutePath, @"\",\n"
" \"uid\" : \"", uid, @"\"\n"
" }\n"
" ]\n"
"}"] componentsJoinedByString:@""];
[target setup:jstr storageType:(easyar_StorageType_Assets | easyar_StorageType_Json) name:@""];
[tracker loadTarget:target callback:^(easyar_Target * target, bool status) {
NSLog(@"load target (%d): %@ (%d)", status, [target name], [target runtimeID]);
}];
3. 根據(jù)目標(biāo)圖片uid指定特定視頻鏈接
if (status == easyar_TargetStatus_Tracked) { // 追蹤到目標(biāo)
int runtimeID = [target runtimeID];
if (active_target != 0 && active_target != runtimeID) {
[video onLost];
video = nil;
tracked_target = 0;
active_target = 0;
}
if (tracked_target == 0) {
if (video == nil && [video_renderers count] > 0) {
NSString * target_uid = [target uid];
NSArray *videoArr = @[@"http://img.dpm.org.cn/Uploads/File/2018/04/19/haitang.mp4",
@"http://img.dpm.org.cn/Uploads/File/2017/11/27/wanhean.mp4",
@"http://img.dpm.org.cn/Uploads/File/2017/10/30/fxyxd.mp4",
@"http://img.dpm.org.cn/Uploads/File/2017/05/03/u5909ad6a125e1.mp4",
@"http://img.dpm.org.cn/Uploads/File/2017/05/03/u5909ad888a6c0.mp4",
@"http://img.dpm.org.cn/Uploads/File/2017/05/03/repair_new.mp4",
@"http://img.dpm.org.cn/Uploads/File/2017/03/22/u58d21fa8ed99a.mp4",
@"http://img.dpm.org.cn/Uploads/File/2017/03/22/u58d21f1215622.mp4",
@"http://img.dpm.org.cn/Uploads/File/2017/03/22/u58d21f012d948.mp4",
@"http://img.dpm.org.cn/Uploads/File/2017/03/22/u58d21fa8ed99a.mp4",
@"http://img.dpm.org.cn/Uploads/File/2017/03/22/u58d21f1215622.mp4",
@"http://img.dpm.org.cn/Uploads/File/2017/03/22/u58d21f012d948.mp4"];
if ([[video_renderers objectAtIndex:[target_uid integerValue]] texid] != 0) {
NSString *urlStr = [videoArr objectAtIndex:[target_uid integerValue]];
video = [[ARVideo alloc] init];
[video openStreamingVideo:urlStr texid:[[video_renderers objectAtIndex:[target_uid integerValue]] texid]];
current_video_renderer = [video_renderers objectAtIndex:[target_uid integerValue]];
}
Demo已上傳至GitHub
BTW: 由于EasyAR的文檔不盡完善幢尚,且對(duì)iOS的支持不是很好,可嘗試蘋果官方系統(tǒng)庫ARKit的實(shí)現(xiàn)ARKit應(yīng)用之識(shí)別圖像播放視頻