PCSBaseViewController.m 123 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083
  1. //
  2. // PCSBaseViewController.m
  3. // PanoramicCameraSDK
  4. //
  5. // Created by 王昭威 on 2023/1/14.
  6. //
  7. #import "PCSBaseViewController.h"
  8. #import <AVFoundation/AVFoundation.h>
  9. #import "Masonry.h"
  10. #import "CameraManager.h"
  11. #import "PCSRoundButton.h"
  12. #import "PCSModeList.h"
  13. #import "UIImage+ext.h"
  14. #import "PCSTools.h"
  15. #import "OperationNodeProtocol.h"
  16. #import "OperationNodeSingle.h"
  17. #import "OperationNodeMultiple.h"
  18. #import "OperationNodePanorama.h"
  19. #import "QuitMultipleModeAlertViewController.h"
  20. #import <CoreMotion/CoreMotion.h>
  21. #import "OperationNodeMovie.h"
  22. #import "SDKParameters.h"
  23. #import "PhotoListViewController.h"
  24. #import "CustomModalViewTransition.h"
  25. #import "MovieListViewController.h"
  26. #import "TimerLabel.h"
  27. #import "MoviePlayerViewController.h"
  28. #import "PanoramaOrientationView.h"
  29. #import "PanoramaGuideView.h"
  30. #import "Renderer.h"
  31. #import "CameraTextureRenderPass.h"
  32. #import "LenzSDKConstant.h"
  33. #import "DBManager.h"
  34. #import <Photos/Photos.h>
  35. #import "BaseViewTapGestureDelegate.h"
  36. #import "MovieListCellModel.h"
  37. #import "PreviewSingleViewController.h"
  38. #import "OperationNodeAIPanorama.h"
  39. #import "PhotoAlbumListViewController.h"
  40. #import "PhotoAlbumModel.h"
  41. //#import "LenStitcher.h"
  42. //#import <LenzStitchSDK/LenzStitchSDK.h>
  43. #import "LenzDataManager.h"
  44. #import "LenzHeader.h"
  45. #import "LenzCachedResourceModel.h"
  46. #import "LenzDataManager.h"
  47. #import "PCSPreviewViewController.h"
  48. #import "SVProgressHUD.h"
  49. #import "PCSAutherView.h"
  50. #import "PCSDeviceOrientationManager.h"
  51. #import <LenzTensorFlowSDK/LenzTensorFlow.h>
  52. #define MODE_LIST_INDEX_2_MODE_ENUM(x) ((x))
  53. // PCS means panoramic camera sdk
  54. @interface PCSBaseViewController ()<AVCapturePhotoCaptureDelegate,
  55. AVCaptureFileOutputRecordingDelegate,
  56. AVCaptureAudioDataOutputSampleBufferDelegate,
  57. UIViewControllerTransitioningDelegate,
  58. AVCaptureVideoDataOutputSampleBufferDelegate,
  59. PhotoListViewControllerDelegate,
  60. PanoramaOrientationViewDelegate,
  61. PCSDeviceOrientationDelegate>
  62. @property (nonatomic, assign) CGFloat accX;
  63. @property (nonatomic, nullable, readonly) AVCaptureDevice* currentCamera;
  64. @property (nonatomic, nullable, readonly) AVCaptureDeviceInput* currentCameraInput;
  65. @property (nonatomic, strong) AVCaptureSession* captureSession;
  66. @property (nonatomic, strong) AVCaptureVideoDataOutput* videoDataOutput;
  67. @property (nonatomic, strong) AVCaptureMovieFileOutput* movieFileOutput;
  68. @property (nonatomic, strong) AVCapturePhotoOutput* photoOutput;
  69. @property (nonatomic, strong) AVCapturePhotoSettings* outputSettings;
  70. @property (nonatomic, copy) dispatch_queue_t videoDataQueue;
  71. @property (nonatomic, strong) AVAssetWriter* videoWriter;
  72. @property (nonatomic, strong) AVAssetWriterInput* videoWriterInput;
  73. @property (nonatomic, assign) BOOL cameraPrepared;
  74. @property (nonatomic, assign) BOOL flag;
  75. @property (nonatomic, assign) BOOL hasCache;
  76. @property (nonatomic, strong) UIView* focusView;
  77. @property (nonatomic, strong, nullable) NSTimer* recordTimer;
  78. @property (nonatomic) dispatch_source_t touchSourceTimer;
  79. @property (nonatomic) CGFloat touchTime;
  80. @property (nonatomic, strong, nullable) NSTimer* motionTimer;
  81. @property (nonatomic, strong) BaseViewTapGestureDelegate* tapDelegate;
  82. /**
  83. key: PhotographModeEnum
  84. value: subclass of OperationNodeProtocol
  85. */
  86. @property (nonatomic, strong) NSDictionary<NSNumber*, id<OperationNodeProtocol>>* operationNodes;
  87. @property (nonatomic, strong) NSArray<id<OperationNodeProtocol>>* operationNodesArray;
  88. @property (nonatomic) dispatch_semaphore_t videoStitchSemaphore;
  89. @property (nonatomic) LenzCachedResourceModel *resourceModel;
  90. @property (nonatomic) UIView *stitchView;
  91. @property (nonatomic) UIImageView *stitchImageView;
  92. @property (nonatomic) UILabel *stitchLabel;
  93. @property (nonatomic) CABasicAnimation *loadingAnimation;
  94. @property (nonatomic) BOOL isStitchBigImage;
  95. @property (nonatomic) CGFloat brightnessValue;
  96. @property (nonatomic) CMMotionManager *motionManager;
  97. @property (nonatomic) BOOL isTakePhoto;
  98. @property (nonatomic) NSLock *stitchkLock;
  99. @property (nonatomic) LenVideoStitchResult stitchResult;
  100. @property (nonatomic) NSNumber *startPitchX;
  101. @property (nonatomic) NSNumber *startRollY;
  102. @property (nonatomic) LenzTensorFlow *tensorFlow;
  103. @property (nonatomic) BOOL isContainSingleInReatinMode;
  104. @property (nonatomic) UILabel *aiPanPromtLabel;
  105. @property (nonatomic) PCSAutherView *authView;
  106. @property (nonatomic) dispatch_queue_t videoQueue;
  107. @property (nonatomic) BOOL isDuringTakePhoto;
  108. @property (nonatomic) PCSDeviceOrientationManager *orientationManager;
  109. @property (nonatomic) TgDirection currentTgDirection;
  110. @property (nonatomic) UIView *svContentView;
  111. @end
  112. @implementation PCSBaseViewController
  113. #pragma mark - Life Circle
  114. - (void)dealloc{
  115. [self stopAllSensors];
  116. [[NSNotificationCenter defaultCenter] removeObserver:self];
  117. [[LenzDBManager shared] close];
  118. }
  119. - (void)awakeFromNib{
  120. [super awakeFromNib];
  121. [self setup];
  122. }
  123. - (void)viewDidLoad {
  124. [super viewDidLoad];
  125. // Do any additional setup after loading the view.
  126. [SVProgressHUD setDefaultStyle:SVProgressHUDStyleLight];
  127. [SVProgressHUD setMinimumDismissTimeInterval:1.5];
  128. [SVProgressHUD setMaximumDismissTimeInterval:3];
  129. [SVProgressHUD setFont:[UIFont systemFontOfSize:14]];
  130. self.tensorFlow = [[LenzTensorFlow alloc]initWithFileName:@"moire_mobilenetv2_79"];
  131. if(@available(iOS 14, *)){
  132. self.videoQueue = dispatch_queue_create("com.lenzSDK.videoQueue", NULL);
  133. } else {
  134. self.videoQueue = dispatch_get_main_queue();
  135. }
  136. self.stitchkLock = [[NSLock alloc]init];
  137. self.totalArray = [[NSMutableArray alloc]init];
  138. self.cachesArray = [[NSMutableArray alloc]init];
  139. self.typeArray = [[NSMutableArray alloc]init];
  140. self.isContainSingleInReatinMode = NO;
  141. self.resourceModel = [[LenzCachedResourceModel alloc]init];
  142. self.motionManager = [[CMMotionManager alloc]init];
  143. self.motionManager.deviceMotionUpdateInterval = 1/5;
  144. self.orientationManager = [[PCSDeviceOrientationManager alloc] initWithDelegate:self];
  145. [self.orientationManager startMonitor];
  146. [self.view addSubview:self.panoramaGuideView];
  147. // [self panoramaOrientationViewslt:(PanoramaOrientationViewLeft)];
  148. self.panoramaGuideView.hidden = YES;
  149. [self.view addSubview:self.aiPanPromtLabel];
  150. [self.aiPanPromtLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  151. make.centerX.mas_equalTo(self.view);
  152. make.top.mas_equalTo(self.modeLabel.mas_bottom).mas_offset(20);
  153. make.width.mas_offset(240);
  154. make.height.mas_offset(20);
  155. }];
  156. [self.view addSubview:self.authView];
  157. __weak typeof(self) weakSelf = self;
  158. self.authView.closeBlock = ^{
  159. weakSelf.panGesture.enabled = YES;
  160. };
  161. [self.authView mas_makeConstraints:^(MASConstraintMaker *make) {
  162. make.edges.mas_offset(0);
  163. }];
  164. if (!JK_IS_IPHONE_X) {
  165. self.safeBottom.constant = 25;
  166. self.safeTop.constant = 15;
  167. }else{
  168. self.safeBottom.constant = 0;
  169. }
  170. [self.timerLabel updateWith:100];
  171. self.albumButton.layer.cornerRadius = 6;
  172. self.albumButton.clipsToBounds = YES;
  173. [self updateFlashButton];
  174. [PCSMotionManager shared];
  175. __weak typeof(self) weakself = self;
  176. [self.modeList reloadWith:self.operationNodesArray];
  177. self.modeList.indexChangedHandler = ^BOOL(NSUInteger newMode, NSUInteger oldMode, NSUInteger newIndex) {
  178. __strong typeof(weakself) strongSelf = weakself;
  179. if (newMode == SDK_CAPTURE_MODE_MOVIE) {
  180. if ([self showAuthBoth]) {
  181. return NO;
  182. }
  183. if ([self showAudioCamerIfNeed]) {
  184. return NO;
  185. }
  186. } else {
  187. if ([self showAuthorCamerIfNeed]) {
  188. return NO;
  189. }
  190. }
  191. if ([SDKParameters shared].retainedMode == SDK_DATA_RETAINED_CLEAR) {
  192. BOOL needAlert = NO;
  193. switch (self.mode) {
  194. case SDK_CAPTURE_MODE_CONTINUOUS:
  195. needAlert = self.resourceModel.continousArray.count;
  196. break;
  197. case SDK_CAPTURE_MODE_SINGLE:
  198. needAlert = self.resourceModel.singleArray.count;
  199. break;
  200. case SDK_CAPTURE_MODE_MOVIE:
  201. needAlert = self.resourceModel.movieArray.count;
  202. break;
  203. case SDK_CAPTURE_MODE_PANORAMA:
  204. needAlert = self.resourceModel.panoramArray.count;
  205. break;
  206. case SDK_CAPTURE_MODE_INTELLEGENCE_PANORAMA:
  207. needAlert = self.resourceModel.aiPanoramArray.count;
  208. break;
  209. default:
  210. break;
  211. }
  212. if (needAlert) {
  213. // [QuitMultipleModeAlertViewController show:self title:@"确认提醒" text:@"切换模式会清除已拍摄数据,请确认" leftBtnTitle:@"取消" rightBtnTitle:@"确定" withLeftButtonCallBack:^(QuitMultipleModeAlertViewController * _Nonnull alertController) {
  214. // [alertController dismissViewControllerAnimated:NO completion:^{}];
  215. // } rightButtonCallBack:^(QuitMultipleModeAlertViewController * _Nonnull alertController) {
  216. switch (self.mode) {
  217. case SDK_CAPTURE_MODE_CONTINUOUS: {
  218. [self.resourceModel.continousArray removeAllObjects];
  219. [self updateViewStatus];
  220. }
  221. break;
  222. case SDK_CAPTURE_MODE_SINGLE: {
  223. [self.resourceModel.singleArray removeAllObjects];
  224. [self updateViewStatus];
  225. }
  226. break;
  227. case SDK_CAPTURE_MODE_MOVIE:
  228. {
  229. [self.resourceModel.movieArray removeAllObjects];
  230. [self updateViewStatus];
  231. }
  232. break;
  233. case SDK_CAPTURE_MODE_PANORAMA:
  234. {
  235. [self.resourceModel.panoramArray removeAllObjects];
  236. [self updateViewStatus];
  237. }
  238. break;
  239. case SDK_CAPTURE_MODE_INTELLEGENCE_PANORAMA:
  240. {
  241. [self.resourceModel.aiPanoramArray removeAllObjects];
  242. [self updateViewStatus];
  243. }
  244. break;
  245. default:
  246. break;
  247. }
  248. self.modeList.index = newIndex;
  249. [self.modeList updateItemsConstraints];
  250. [strongSelf modeWillChange:newMode oldIndex:oldMode];
  251. id<OperationNodeProtocol> oldModelNode = strongSelf.currentOperationNode;
  252. PhotographModeEnum mode = (PhotographModeEnum)MODE_LIST_INDEX_2_MODE_ENUM(newMode);
  253. strongSelf.mode = mode;
  254. self.ai_fullView.hidden = YES;
  255. [strongSelf modeDidChange:newMode oldIndex:oldMode];
  256. [strongSelf updateCaptureSessionWith:oldMode];
  257. if([oldModelNode respondsToSelector:@selector(didQuit)]){
  258. [oldModelNode didQuit];
  259. }
  260. // }];
  261. return NO;
  262. }
  263. }
  264. [strongSelf modeWillChange:newMode oldIndex:oldMode];
  265. id<OperationNodeProtocol> oldModelNode = strongSelf.currentOperationNode;
  266. PhotographModeEnum mode = (PhotographModeEnum)MODE_LIST_INDEX_2_MODE_ENUM(newMode);
  267. strongSelf.mode = mode;
  268. self.ai_fullView.hidden = YES;
  269. [strongSelf modeDidChange:newMode oldIndex:oldMode];
  270. [strongSelf updateCaptureSessionWith:oldMode];
  271. if([oldModelNode respondsToSelector:@selector(didQuit)]){
  272. [oldModelNode didQuit];
  273. }
  274. return YES;
  275. };
  276. [self modeWillChange:[SDKParameters shared].modeIndex oldIndex:0lu];
  277. self.modeList.mode = [SDKParameters shared].modeIndex;
  278. self.mode = [SDKParameters shared].modeIndex;
  279. [self modeDidChange:[SDKParameters shared].modeIndex oldIndex:0lu];
  280. if(!self.cameraPrepared){
  281. [self prepareCamera];
  282. }
  283. [self modeDidChange:[SDKParameters shared].modeIndex oldIndex:0lu];
  284. [self updateCaptureSessionWith:0];
  285. self.panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGestureHandler:)];
  286. [self.view addGestureRecognizer:self.panGesture];
  287. UIImageView* loadingImageView = [[UIImageView alloc] initWithImage:[UIImage sdkImageNamed:@"loading"]];
  288. self.loadingIndicator = loadingImageView;
  289. self.loadingIndicator.hidden = YES;
  290. [self.view addSubview:self.loadingIndicator];
  291. [loadingImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
  292. make.center.equalTo(self.view);
  293. }];
  294. self.savePhotoButton.titleLabel.textColor = [UIColor whiteColor];
  295. self.discardPhotoButton.titleLabel.textColor = [UIColor whiteColor];
  296. [self updateUIByMode];
  297. self.panoramOrientationGuideView.delegate = self;
  298. self.ablumImageView = [[UIImageView alloc]init];
  299. [self.view addSubview:self.ablumImageView];
  300. self.ablumImageView.layer.cornerRadius = 6;
  301. self.ablumImageView.layer.masksToBounds = YES;
  302. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(showPreview:)];
  303. [self.ablumImageView addGestureRecognizer:tap];
  304. self.ablumImageView.userInteractionEnabled = YES;
  305. self.ablumImageView.hidden = YES;
  306. self.ablumImageView.contentMode = UIViewContentModeScaleAspectFill;
  307. [self.ablumImageView mas_makeConstraints:^(MASConstraintMaker *make) {
  308. make.centerY.mas_equalTo(self.takePhotoButton);
  309. make.left.mas_equalTo(20);
  310. make.width.height.mas_equalTo(60);
  311. }];
  312. UILongPressGestureRecognizer *ges = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPressGes:)];
  313. ges.minimumPressDuration = 1;
  314. [self.takePhotoButton addGestureRecognizer:ges];
  315. self.timerLabel.layer.cornerRadius = 6;
  316. self.timerLabel.layer.masksToBounds = YES;
  317. [self.ai_fullView addTarget:self action:@selector(alFullViewAction:) forControlEvents:UIControlEventTouchUpInside];
  318. [self.view addSubview:self.stitchView];
  319. [self.stitchView mas_makeConstraints:^(MASConstraintMaker *make) {
  320. make.centerY.mas_equalTo(self.view);
  321. make.centerX.mas_equalTo(self.view);
  322. make.width.mas_offset(200);
  323. make.height.mas_offset(200);
  324. }];
  325. self.svContentView = [[UIView alloc]init];
  326. self.svContentView.backgroundColor = [UIColor clearColor];
  327. self.svContentView.userInteractionEnabled = NO;
  328. [self.view addSubview:self.svContentView];
  329. [self.svContentView mas_makeConstraints:^(MASConstraintMaker *make) {
  330. make.edges.mas_equalTo(self.view);
  331. }];
  332. [SVProgressHUD setContainerView:self.svContentView];
  333. self.currentTgDirection = TgDirectionPortrait;
  334. self.panoramaGuideView.dir = self.currentTgDirection;
  335. self.panoramaGuideView.orginDir = self.panoramOrientationGuideView.selectedItemIndex;
  336. [self directionChange:self.currentTgDirection];
  337. }
  338. - (PCSAutherView *)authView {
  339. if (!_authView) {
  340. _authView = [[PCSAutherView alloc]init];
  341. _authView.hidden = YES;
  342. }
  343. return _authView;
  344. }
  345. - (void)viewWillAppear:(BOOL)animated{
  346. [super viewWillAppear:animated];
  347. self.panGesture.enabled = YES;
  348. if([self.currentOperationNode respondsToSelector:@selector(updateUI)])
  349. [self.currentOperationNode updateUI];
  350. }
  351. - (void)viewDidAppear:(BOOL)animated{
  352. [super viewDidAppear:animated];
  353. if(self.cameraPrepared){
  354. __block BOOL isAuthVideo = NO;
  355. switch ([AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo]) {
  356. case AVAuthorizationStatusAuthorized:
  357. isAuthVideo = YES;
  358. [self startCaptureSession];
  359. [[PCSMotionManager shared] startAccelerometerUpdates];
  360. break;
  361. case AVAuthorizationStatusNotDetermined:{
  362. [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
  363. if(granted){
  364. isAuthVideo = YES;
  365. [self startCaptureSession];
  366. [[PCSMotionManager shared] startAccelerometerUpdates];
  367. }
  368. else{
  369. [[NSNotificationCenter defaultCenter] postNotificationName:CameraNotificationNotPermission object:self];
  370. }
  371. }];
  372. break;
  373. }
  374. case AVAuthorizationStatusDenied:{
  375. [[NSNotificationCenter defaultCenter] postNotificationName:CameraNotificationNotPermission object:self];
  376. break;
  377. }
  378. case AVAuthorizationStatusRestricted:
  379. break;
  380. default:
  381. break;
  382. }
  383. switch ([AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeAudio]) {
  384. case AVAuthorizationStatusAuthorized:
  385. [self setupMicrophone];
  386. break;
  387. case AVAuthorizationStatusNotDetermined:{
  388. [AVCaptureDevice requestAccessForMediaType:AVMediaTypeAudio completionHandler:^(BOOL granted) {
  389. if(granted){
  390. [self setupMicrophone];
  391. if (self.mode != SDK_CAPTURE_MODE_MOVIE) {
  392. if (!isAuthVideo) {
  393. [self showAuthorCamerIfNeed];
  394. }
  395. }
  396. }
  397. else{
  398. if (self.mode == SDK_CAPTURE_MODE_MOVIE) {
  399. if (!isAuthVideo) {
  400. [self showAuthBoth];
  401. } else {
  402. [self showAudioCamerIfNeed];
  403. }
  404. } else {
  405. [self showAuthorCamerIfNeed];
  406. }
  407. [[NSNotificationCenter defaultCenter] postNotificationName:MicrophoneNotificationNotPermission object:self];
  408. }
  409. }];
  410. break;
  411. }
  412. case AVAuthorizationStatusDenied:{
  413. [[NSNotificationCenter defaultCenter] postNotificationName:MicrophoneNotificationNotPermission object:self];
  414. break;
  415. }
  416. case AVAuthorizationStatusRestricted:
  417. break;
  418. default:
  419. break;
  420. }
  421. }
  422. if (self.mode == SDK_CAPTURE_MODE_MOVIE) {
  423. if (![self showAuthBoth]) {
  424. [self showAudioCamerIfNeed];
  425. }
  426. } else {
  427. if (![self showAuthBoth]) {
  428. [self showAuthorCamerIfNeed];
  429. }
  430. }
  431. }
  432. - (void)updateFlashButton {
  433. __block SDKFlashModeIndex flashModel = SDK_FLASH_MODE_AUTO;
  434. [[SDKParameters shared].items enumerateObjectsUsingBlock:^(SDKParametersItem * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  435. if (obj.model == self.mode) {
  436. flashModel = obj.flashMode;
  437. }
  438. }];
  439. switch (flashModel) {
  440. case SDK_FLASH_MODE_AUTO:{
  441. self.outputSettings.flashMode = AVCaptureFlashModeAuto;
  442. [self.flashButton setImage:[UIImage sdkImageNamed:@"flash-auto-btn"] forState:UIControlStateNormal];
  443. break;
  444. }
  445. case SDK_FLASH_MODE_ON:{
  446. self.outputSettings.flashMode = AVCaptureFlashModeOn;
  447. [self.flashButton setImage:[UIImage sdkImageNamed:@"flash-btn"] forState:UIControlStateNormal];
  448. break;
  449. }
  450. case SDK_FLASH_MODE_OFF:{
  451. self.outputSettings.flashMode = AVCaptureFlashModeOff;
  452. [self.flashButton setImage:[UIImage sdkImageNamed:@"flash-disable-btn"] forState:UIControlStateNormal];
  453. break;
  454. }
  455. default:
  456. break;
  457. }
  458. }
  459. - (UIView *)stitchView {
  460. if (!_stitchView) {
  461. _stitchView = [[UIView alloc]init];
  462. [_stitchView addSubview:self.stitchImageView];
  463. [self.stitchImageView mas_makeConstraints:^(MASConstraintMaker *make) {
  464. make.centerX.mas_equalTo(_stitchView);
  465. make.top.mas_equalTo(0);
  466. make.width.height.mas_equalTo(68);
  467. }];
  468. [_stitchView addSubview:self.stitchLabel];
  469. [self.stitchLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  470. make.top.mas_equalTo(self.stitchImageView.mas_bottom).mas_offset(20);
  471. make.centerX.mas_equalTo(self.stitchImageView);
  472. }];
  473. _stitchView.hidden = YES;
  474. }
  475. return _stitchView;
  476. }
  477. - (UIImageView *)stitchImageView {
  478. if (!_stitchImageView) {
  479. _stitchImageView = [[UIImageView alloc]init];
  480. _stitchImageView.image = [UIImage loadNamed:@"loading"];
  481. }
  482. return _stitchImageView;
  483. }
  484. - (CABasicAnimation *)loadingAnimation {
  485. if (!_loadingAnimation) {
  486. _loadingAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
  487. _loadingAnimation.fromValue = [NSNumber numberWithFloat:0.f];
  488. _loadingAnimation.toValue = [NSNumber numberWithFloat: M_PI *2];
  489. _loadingAnimation.duration = 3;
  490. _loadingAnimation.autoreverses = NO;
  491. _loadingAnimation.fillMode = kCAFillModeForwards;
  492. _loadingAnimation.repeatCount = MAXFLOAT;
  493. }
  494. return _loadingAnimation;
  495. }
  496. - (UILabel *)aiPanPromtLabel {
  497. if (!_aiPanPromtLabel) {
  498. _aiPanPromtLabel = [[UILabel alloc]init];
  499. _aiPanPromtLabel.backgroundColor = [UIColor colorWithRed:33/255.0 green:33/255.0 blue:33/255.0 alpha:.2];
  500. _aiPanPromtLabel.layer.cornerRadius = 6;
  501. _aiPanPromtLabel.layer.masksToBounds = YES;
  502. _aiPanPromtLabel.textColor = [UIColor whiteColor];
  503. _aiPanPromtLabel.font = [UIFont systemFontOfSize:14];
  504. _aiPanPromtLabel.text = @"请保持手机与拍摄物平行,匀速拍摄";
  505. _aiPanPromtLabel.hidden = YES;
  506. _aiPanPromtLabel.textAlignment = NSTextAlignmentCenter;
  507. }
  508. return _aiPanPromtLabel;
  509. }
  510. - (UILabel *)stitchLabel {
  511. if (!_stitchLabel) {
  512. _stitchLabel = [[UILabel alloc]init];
  513. _stitchLabel.text = @"正在拼接中,请稍等";
  514. _stitchLabel.font = [UIFont systemFontOfSize:12];
  515. _stitchLabel.textColor = [UIColor whiteColor];
  516. }
  517. return _stitchLabel;
  518. }
  519. #pragma mark - Private Method
  520. - (void)alFullViewAction:(UIButton *)button {
  521. if (self.mode == SDK_CAPTURE_MODE_PANORAMA) {
  522. OperationNodePanorama *pan = nil;
  523. if ([self.currentOperationNode isKindOfClass:[OperationNodePanorama class]]) {
  524. pan = self.currentOperationNode;
  525. pan.needHiddenDir = !pan.needHiddenDir;
  526. }
  527. self.panoramOrientationGuideView.hidden = pan.needHiddenDir;
  528. } else if (self.mode == SDK_CAPTURE_MODE_INTELLEGENCE_PANORAMA) {
  529. OperationNodeAIPanorama *aiPan = nil;
  530. if ([self.currentOperationNode isKindOfClass:[OperationNodeAIPanorama class]]) {
  531. aiPan = self.currentOperationNode;
  532. aiPan.needHiddenDir = !aiPan.needHiddenDir;
  533. }
  534. self.panoramOrientationGuideView.hidden = aiPan.needHiddenDir;
  535. }
  536. }
  537. - (void)showPreview:(UITapGestureRecognizer *)tap {
  538. if (self.ablumImageView.hidden) {
  539. return;
  540. }
  541. if(self.mode != SDK_CAPTURE_MODE_SINGLE) {
  542. PCSPreviewViewController *vc = [[PCSPreviewViewController alloc]init];
  543. vc.modalPresentationStyle = UIModalPresentationFullScreen;
  544. vc.model = self.resourceModel;
  545. vc.selectIndex = self.mode;
  546. __weak typeof(self) weakSelf = self;
  547. vc.dataChangeBlock = ^{
  548. [weakSelf updateViewStatus];
  549. };
  550. [self presentViewController:vc animated:YES completion:nil];
  551. } else {
  552. PreviewSingleViewController* dst = [[PreviewSingleViewController alloc]init];
  553. dst.modalPresentationStyle = UIModalPresentationFullScreen;
  554. dst.singleModeNode = self.currentOperationNode;
  555. if (self.resourceModel.singleArray.count > 0) {
  556. LenzResourceItemModel *model = self.resourceModel.singleArray.firstObject;
  557. dst.image = model.image;
  558. }
  559. [self presentViewController:dst animated:YES completion:nil];
  560. }
  561. }
  562. + (instancetype)initWithParams:(NSDictionary *)params complete:(PCSBaseVCCompleteBLock)complete {
  563. [self parser:params];
  564. UIStoryboard* sb = [UIStoryboard storyboardWithName:[PCSTools shared].mainStoryboardName bundle:[PCSTools sdkBundle]];
  565. PCSBaseViewController *vc = (PCSBaseViewController *)[sb instantiateViewControllerWithIdentifier:@"sample"];
  566. vc.completeBlock = complete;
  567. return vc;
  568. }
  569. #pragma mark - Config param
  570. + (void)parser: (NSDictionary*)params{
  571. SDKParameters* sdkParams = [SDKParameters shared];
  572. NSString* retainedMode = params[@"dataRetainedMode"];
  573. if(retainedMode != nil){
  574. if([retainedMode isEqual:@"retain"]){
  575. sdkParams.retainedMode = SDK_DATA_RETAINED_RETAIN;
  576. }
  577. else if([retainedMode isEqual:@"clear"]){
  578. sdkParams.retainedMode = SDK_DATA_RETAINED_CLEAR;
  579. }
  580. }
  581. NSArray* arr = params[@"cameraMode"];
  582. NSDictionary<NSString*, NSNumber*>* table = @{
  583. @"continuous": @(SDK_CAPTURE_MODE_CONTINUOUS),
  584. @"single" : @(SDK_CAPTURE_MODE_SINGLE),
  585. @"video" : @(SDK_CAPTURE_MODE_MOVIE),
  586. @"panorama" : @(SDK_CAPTURE_MODE_PANORAMA),
  587. @"panoramaPlus" : @(SDK_CAPTURE_MODE_INTELLEGENCE_PANORAMA)
  588. };
  589. NSMutableArray<NSNumber*>* modeIndiciesTable = [NSMutableArray array];
  590. NSMutableArray *mutabArray = [NSMutableArray array];
  591. [arr enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  592. if ([obj isKindOfClass:[NSDictionary class]]) {
  593. NSDictionary *dict = (NSDictionary *)obj;
  594. NSString *mode = dict[@"mode"];
  595. if (!table[mode]) {
  596. return;
  597. }
  598. if (idx == 0) {
  599. sdkParams.modeIndex = (SDKCaptureModeIndex)table[mode].integerValue;
  600. }
  601. [modeIndiciesTable addObject:table[mode]];
  602. SDKParametersItem *item = [[SDKParametersItem alloc]init];
  603. NSString* cameraPosition = dict[@"type"];
  604. item.model = (SDKCaptureModeIndex)table[mode].integerValue;
  605. if(cameraPosition != nil){
  606. if([cameraPosition isEqual:@"back"]){
  607. item.cameraPosition = SDK_CAMERA_PISITION_BACK;
  608. item.cameraPositionString = cameraPosition;
  609. }
  610. else if([cameraPosition isEqual:@"front"]){
  611. item.cameraPosition = SDK_CAMERA_PISITION_FRONT;
  612. item.cameraPositionString = cameraPosition;
  613. }
  614. }
  615. NSNumber* recTime = dict[@"recTime"];
  616. if(recTime != nil){
  617. item.limitRecordTime = [recTime integerValue];
  618. }
  619. item.keyFrame = dict[@"keyframe"];
  620. item.isRemake = [dict[@"isRemake"] boolValue];
  621. NSNumber *quantity = dict[@"quantity"];
  622. if (quantity) {
  623. item.quantity = [quantity integerValue];
  624. }
  625. NSNumber *quality = dict[@"quality"];
  626. if (quality) {
  627. item.quality = [quality floatValue];
  628. }
  629. NSString* flashMode = dict[@"flashMode"];
  630. if(flashMode != nil){
  631. item.flashModeString = flashMode;
  632. if([flashMode isEqualToString:@"auto"]){
  633. item.flashMode = SDK_FLASH_MODE_AUTO;
  634. }
  635. else if([flashMode isEqualToString:@"on"]){
  636. item.flashMode = SDK_FLASH_MODE_ON;
  637. }
  638. else if([flashMode isEqualToString:@"off"]){
  639. item.flashMode = SDK_FLASH_MODE_OFF;
  640. }
  641. }
  642. NSNumber* videoQualityNum = dict[@"videoQuality"];
  643. if(videoQualityNum != nil){
  644. NSInteger videoQuality = [videoQualityNum integerValue];
  645. if(videoQuality == 480){
  646. item.videoQuality = SDK_VIDEO_QUALITY_480;
  647. }
  648. else if(videoQuality == 720){
  649. item.videoQuality = SDK_VIDEO_QUALITY_720;
  650. }
  651. else if(videoQuality == 1080){
  652. item.videoQuality = SDK_VIDEO_QUALITY_1080;
  653. }
  654. else if(videoQuality == 2160){
  655. item.videoQuality = SDK_VIDEO_QUALITY_2160;
  656. }
  657. }
  658. NSNumber* showPreviewForPanorama = dict[@"showPreview"];
  659. if(showPreviewForPanorama != nil){
  660. item.showPreviewForPanorama = [showPreviewForPanorama boolValue];
  661. }
  662. [mutabArray addObject:item];
  663. }
  664. }];
  665. sdkParams.modeIndices = [modeIndiciesTable copy];
  666. [SDKParameters shared].items = mutabArray;
  667. }
  668. // 拍照界面
  669. - (void)showPreviewView: (BOOL)show{
  670. self.previewLayer.hidden = !show;
  671. self.takePhotoButton.enabled = show;
  672. self.resultImageView.hidden = show;
  673. self.backButton.hidden = !show;
  674. self.flashButton.hidden = !show;
  675. self.switchButton.hidden = !show;
  676. self.discardPhotoButton.hidden = show;
  677. self.savePhotoButton.hidden = show;
  678. }
  679. - (void)setup{
  680. // 打开数据库
  681. [[LenzDBManager shared] openSqlDataBase];
  682. [LenzDataManager creatDirectory];
  683. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  684. [[PCSTools shared] cleanDiskCache];
  685. });
  686. _mode = SDK_CAPTURE_MODE_CONTINUOUS;
  687. _captureSession = [[AVCaptureSession alloc] init];
  688. _captureSession.sessionPreset = AVCaptureSessionPresetInputPriority;
  689. _movieFileOutput = [[AVCaptureMovieFileOutput alloc] init];
  690. _videoDataQueue = dispatch_queue_create("video data", DISPATCH_QUEUE_SERIAL);
  691. _videoDataOutput = [[AVCaptureVideoDataOutput alloc] init];
  692. _videoDataOutput.alwaysDiscardsLateVideoFrames = YES;
  693. _videoDataOutput.videoSettings = [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:kCVPixelFormatType_32BGRA]
  694. forKey:(id)kCVPixelBufferPixelFormatTypeKey];
  695. _photoOutput = [[AVCapturePhotoOutput alloc] init];
  696. NSDictionary *setDic = @{AVVideoCodecKey:AVVideoCodecTypeJPEG};
  697. _outputSettings = [AVCapturePhotoSettings photoSettingsWithFormat:setDic];
  698. [_photoOutput setPhotoSettingsForSceneMonitoring:_outputSettings];
  699. _previewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:self.captureSession];
  700. _previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
  701. NSMutableArray* nodesArr = [NSMutableArray array];
  702. NSMutableDictionary* nodesDic = [NSMutableDictionary dictionary];
  703. __block SDKCaptureModeIndex defaultModeIndex = SDK_CAPTURE_MODE_UNKNOW;
  704. NSDictionary<NSNumber*, Class>* nodeClsTable = @{
  705. @(SDK_CAPTURE_MODE_CONTINUOUS): [OperationNodeMultiple class],
  706. @(SDK_CAPTURE_MODE_SINGLE): [OperationNodeSingle class],
  707. @(SDK_CAPTURE_MODE_MOVIE): [OperationNodeMovie class],
  708. @(SDK_CAPTURE_MODE_PANORAMA): [OperationNodePanorama class],
  709. @(SDK_CAPTURE_MODE_INTELLEGENCE_PANORAMA): [OperationNodeAIPanorama class],
  710. };
  711. [[SDKParameters shared].modeIndices enumerateObjectsUsingBlock:^(NSNumber * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  712. if ([SDKParameters shared].retainedMode == SDK_DATA_RETAINED_RETAIN) {
  713. if (idx == 0 && obj.intValue != SDK_CAPTURE_MODE_SINGLE) {
  714. defaultModeIndex = (SDKCaptureModeIndex)obj.integerValue;
  715. } else if (idx == 1 && defaultModeIndex == SDK_CAPTURE_MODE_UNKNOW) {
  716. defaultModeIndex = (SDKCaptureModeIndex)obj.integerValue;
  717. }
  718. } else {
  719. if (idx == 0) {
  720. defaultModeIndex = (SDKCaptureModeIndex)obj.integerValue;
  721. }
  722. }
  723. if(nodeClsTable[obj] != nil){
  724. if ([SDKParameters shared].retainedMode == SDK_DATA_RETAINED_RETAIN && obj.intValue == SDK_CAPTURE_MODE_SINGLE) {
  725. self.isContainSingleInReatinMode = YES;
  726. return;
  727. } else {
  728. id node = [[nodeClsTable[obj] alloc] initWithController: self];
  729. if ([node isKindOfClass:[OperationNodeVideoBase class]]) {
  730. OperationNodeVideoBase *base = (OperationNodeVideoBase *)node;
  731. [[SDKParameters shared].items enumerateObjectsUsingBlock:^(SDKParametersItem * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  732. if (obj.model == self.mode) {
  733. base.recTime = obj.limitRecordTime;
  734. }
  735. }];
  736. }
  737. [nodesArr addObject: node];
  738. nodesDic[obj] = node;
  739. }
  740. }
  741. }];
  742. if ([SDKParameters shared].retainedMode == SDK_DATA_RETAINED_RETAIN && nodesArr.count == 0 && self.isContainSingleInReatinMode) {
  743. // reatin模式下只有single
  744. id node = [[OperationNodeSingle alloc] initWithController: self];
  745. [nodesArr addObject: node];
  746. nodesDic[@(SDK_CAPTURE_MODE_SINGLE)] = node;
  747. defaultModeIndex = SDK_CAPTURE_MODE_SINGLE;
  748. }
  749. [SDKParameters shared].modeIndex = defaultModeIndex;
  750. _operationNodesArray = [nodesArr copy];
  751. _operationNodes = [nodesDic copy];
  752. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(willEnterForeground:) name:UIApplicationWillEnterForegroundNotification object:nil];
  753. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(willEnterBackground:) name:UIApplicationDidEnterBackgroundNotification object:nil];
  754. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationWillChange:) name:UIDeviceOrientationDidChangeNotification object:nil];
  755. }
  756. - (void)startMotion {
  757. if (self.motionManager.deviceMotionAvailable) {
  758. __weak typeof(self)wearSelf = self;
  759. [self.motionManager startDeviceMotionUpdatesToQueue:[NSOperationQueue mainQueue] withHandler:^(CMDeviceMotion * _Nullable motion, NSError * _Nullable error) {
  760. if (!self.startPitchX && !self.startRollY) {
  761. self.startPitchX = @(motion.attitude.pitch * 100);
  762. self.startRollY = @(motion.attitude.roll *100);
  763. }
  764. double x = self.startPitchX.doubleValue - motion.attitude.pitch * 100;
  765. double y = self.startRollY.doubleValue - motion.attitude.roll * 100;
  766. LenVideoStitchDirction dir = (LenVideoStitchDirction)wearSelf.panoramOrientationGuideView.selectedItemIndex;
  767. if (dir == LenVideoStitchDirctionLeft || dir == LenVideoStitchDirctionRight) {
  768. if (fabs(x) < 3) {
  769. [wearSelf.panoramaGuideView updateArrowWith:PanoramaArrowDirCenter dir:dir];
  770. } else {
  771. if (x > 0) {
  772. [wearSelf.panoramaGuideView updateArrowWith:PanoramaArrowDirDown dir:dir];
  773. } else {
  774. [wearSelf.panoramaGuideView updateArrowWith:PanoramaArrowDirUp dir:dir];
  775. }
  776. }
  777. } else if (dir == LenVideoStitchDirctionUp || dir == LenVideoStitchDirctionDown) {
  778. if (fabs(y) < 3) {
  779. [wearSelf.panoramaGuideView updateArrowWith:PanoramaArrowDirCenter dir:dir];
  780. } else {
  781. if (y > 0) {
  782. [wearSelf.panoramaGuideView updateArrowWith:PanoramaArrowDirLeft dir:dir];
  783. } else {
  784. [wearSelf.panoramaGuideView updateArrowWith:PanoramaArrowDirRight dir:dir];
  785. }
  786. }
  787. }
  788. }];
  789. }
  790. }
  791. - (void)stopMotion {
  792. [self.motionManager stopDeviceMotionUpdates];
  793. self.startPitchX = nil;
  794. self.startRollY = nil;
  795. LenVideoStitchDirction dir = (LenVideoStitchDirction)self.panoramOrientationGuideView.selectedItemIndex;
  796. [self.panoramaGuideView updateArrowWith:PanoramaArrowDirCenter dir:dir];
  797. }
  798. #pragma mark - Lazy Load
  799. - (BaseViewTapGestureDelegate *)tapDelegate{
  800. if(_tapDelegate == nil){
  801. _tapDelegate = [[BaseViewTapGestureDelegate alloc] init];
  802. _tapDelegate.controller = self;
  803. }
  804. return _tapDelegate;
  805. }
  806. - (AVCaptureDevice *)currentCamera{
  807. __block AVCaptureDevice* camera = nil;
  808. [self.captureSession.inputs enumerateObjectsUsingBlock:^(__kindof AVCaptureInput * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  809. AVCaptureDeviceInput* inputDevice = obj;
  810. if([inputDevice.device hasMediaType:AVMediaTypeVideo]){
  811. camera = inputDevice.device;
  812. *stop = YES;
  813. }
  814. }];
  815. return self.currentCameraInput.device;
  816. }
  817. - (AVCaptureDeviceInput*)currentCameraInput{
  818. __block AVCaptureDeviceInput* cameraInput = nil;
  819. [self.captureSession.inputs enumerateObjectsUsingBlock:^(__kindof AVCaptureInput * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  820. AVCaptureDeviceInput* inputDevice = obj;
  821. if([inputDevice.device hasMediaType:AVMediaTypeVideo]){
  822. cameraInput = inputDevice;
  823. *stop = YES;
  824. }
  825. }];
  826. return cameraInput;
  827. }
  828. - (SDKParameters *)parameters{
  829. return [SDKParameters shared];
  830. }
  831. #pragma mark - Action
  832. - (void)focusOn: (CGPoint)point{
  833. CGPoint pt = [self.previewLayer captureDevicePointOfInterestForPoint:point];
  834. AVCaptureDevice* curr = self.currentCamera;
  835. [[CameraManager shared] focusOn:pt device:curr];
  836. [self showFocusViewAt:point];
  837. }
  838. - (void)showFocusViewAt: (CGPoint)point{
  839. [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(hideFocusView) object:nil];
  840. if(self.focusView == nil){
  841. UIImageView* imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 42, 42)];
  842. imgView.image = [UIImage sdkImageNamed:@"focus"];
  843. imgView.contentMode = UIViewContentModeScaleAspectFit;
  844. self.focusView = imgView;
  845. [self.view addSubview:self.focusView];
  846. }
  847. self.focusView.hidden = NO;
  848. CABasicAnimation* flash = [CABasicAnimation animationWithKeyPath:@"opacity"];
  849. flash.duration = 0.2;
  850. flash.fromValue = @(1);
  851. flash.toValue = @(0.1);
  852. flash.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
  853. flash.autoreverses = YES;
  854. flash.repeatCount = 3;
  855. [self.focusView.layer removeAnimationForKey:@"flash"];
  856. [self.focusView.layer addAnimation:flash forKey:@"flash"];
  857. self.focusView.frame = CGRectMake(point.x - 21, point.y - 21, 42, 42);
  858. [self performSelector:@selector(hideFocusView) withObject:nil afterDelay:3];
  859. }
  860. - (void)hideFocusView{
  861. [self.focusView.layer removeAnimationForKey:@"flash"];
  862. self.focusView.hidden = YES;
  863. }
  864. - (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo{
  865. if(error != nil){
  866. return;
  867. }
  868. if(self.mode == SDK_CAPTURE_MODE_SINGLE){
  869. [self stopCaptureSession];
  870. [[PCSMotionManager shared] stopAccelerometerUpdates];
  871. [self dismissViewControllerAnimated:YES completion:^{}];
  872. }
  873. else{
  874. [self startCaptureSession];
  875. [self showPreviewView:YES];
  876. }
  877. }
  878. - (AVCaptureVideoOrientation)videoOrientation{
  879. UIDeviceOrientation curr = UIDeviceOrientationUnknown;
  880. return [self videoOrientation:&curr];
  881. }
  882. - (AVCaptureVideoOrientation)videoOrientation:(UIDeviceOrientation*)currentDeviceOrientation{
  883. return [[PCSMotionManager shared] videoOrientation:currentDeviceOrientation];;
  884. }
  885. - (void)takeStillPhoto:(AVCaptureVideoPreviewLayer*)previewLayer {
  886. if ([self showAuthorCamerIfNeed]) {
  887. self.panGesture.enabled = YES;
  888. self.isDuringTakePhoto = NO;
  889. return;
  890. }
  891. AVCaptureConnection *connection = [self.photoOutput connectionWithMediaType:AVMediaTypeVideo];
  892. if ([connection isVideoStabilizationSupported]) {
  893. // connection.preferredVideoStabilizationMode = AVCaptureVideoStabilizationModeAuto;
  894. }
  895. if(connection == nil){
  896. self.panGesture.enabled = YES;
  897. self.isDuringTakePhoto = NO;
  898. return;
  899. }
  900. if (connection.supportsVideoOrientation) {
  901. // 根据motion设置照片方向
  902. AVCaptureVideoOrientation videoOrientation = [self videoOrientation];
  903. connection.videoMirrored = self.currentCamera == [CameraManager shared].front;
  904. connection.videoOrientation = videoOrientation;
  905. }
  906. // 创建 AVCapturePhotoSettings
  907. AVCapturePhotoSettings *photoSettings = [AVCapturePhotoSettings photoSettings];
  908. if ([self.photoOutput.availablePhotoCodecTypes containsObject:AVVideoCodecTypeJPEG]) {
  909. NSDictionary *format = @{AVVideoCodecKey: AVVideoCodecTypeJPEG};
  910. photoSettings = [AVCapturePhotoSettings photoSettingsWithFormat:format];
  911. if (self.currentCameraInput.device.position == SDK_CAMERA_PISITION_BACK) {
  912. photoSettings.flashMode = self.outputSettings.flashMode;
  913. }
  914. photoSettings.previewPhotoFormat = @{
  915. (NSString*)kCVPixelBufferPixelFormatTypeKey : photoSettings.availablePreviewPhotoPixelFormatTypes.firstObject,
  916. (NSString*)kCVPixelBufferWidthKey: @(160),
  917. (NSString*)kCVPixelBufferHeightKey: @(160),
  918. };
  919. }
  920. photoSettings.autoStillImageStabilizationEnabled = YES;
  921. // 拍照
  922. [self.photoOutput capturePhotoWithSettings:photoSettings delegate:self];
  923. }
  924. #pragma mark - Notification
  925. - (void)orientationWillChange: (NSNotification*)noti{
  926. }
  927. - (void)willEnterForeground: (NSNotification*)noti{
  928. }
  929. //UIBackgroundTaskIdentifier _bgTaskToken;
  930. - (void)willEnterBackground: (NSNotification*)noti{
  931. if([self.currentOperationNode conformsToProtocol:@protocol(OperationNodeBackgroundProtocol)]){
  932. id<OperationNodeBackgroundProtocol> obj = self.currentOperationNode;
  933. if ([obj respondsToSelector:@selector(willEnterBackground:)]) {
  934. [obj willEnterBackground:noti];
  935. }
  936. }
  937. }
  938. - (void)orientationDidChange: (NSNotification*)noti{
  939. if([self.currentOperationNode respondsToSelector:@selector(stop)])
  940. [self.currentOperationNode stop];
  941. }
  942. #pragma mark - alert
  943. - (void)showAlertForAuthorizationWithText: (NSString*)text{
  944. [QuitMultipleModeAlertViewController show:self title:@"" text:text leftBtnTitle:@"取消" rightBtnTitle:@"确认" withLeftButtonCallBack:^(QuitMultipleModeAlertViewController * _Nonnull alertController) {
  945. [alertController dismissViewControllerAnimated:YES completion:^{
  946. }];
  947. } rightButtonCallBack:^(QuitMultipleModeAlertViewController * _Nonnull alertController) {
  948. [alertController dismissViewControllerAnimated:YES completion:^{
  949. NSURL* url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
  950. if(url != nil){
  951. [[UIApplication sharedApplication] openURL:url options:@{} completionHandler:^(BOOL success) {
  952. }];
  953. }
  954. }];
  955. }];
  956. }
  957. - (void)showAlertForAuthorization{
  958. [self showAlertForAuthorizationWithText:@"请在系统设置里打开摄像机访问权限后重试"];
  959. }
  960. - (BOOL)showAuthBoth {
  961. __block BOOL showVideoAlert = NO;
  962. AVAuthorizationStatus staus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
  963. switch (staus) {
  964. case AVAuthorizationStatusDenied:{
  965. showVideoAlert = YES;
  966. break;
  967. }
  968. default:
  969. showVideoAlert = NO;
  970. break;
  971. }
  972. __block BOOL showAlert = NO;
  973. switch ([AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeAudio]) {
  974. case AVAuthorizationStatusDenied:{
  975. showAlert = YES;
  976. break;
  977. }
  978. default:
  979. showAlert = NO;
  980. break;
  981. }
  982. if (showVideoAlert && showAlert) {
  983. self.panGesture.enabled = NO;
  984. [self.authView showAutherWith:PCSAuthPhoneAndMir];
  985. return YES;
  986. }
  987. return NO;
  988. }
  989. - (BOOL)showAuthorCamerIfNeed {
  990. __block BOOL showAlert = NO;
  991. AVAuthorizationStatus staus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
  992. switch (staus) {
  993. case AVAuthorizationStatusDenied:{
  994. [self.authView showAutherWith:PCSAuthPhone];
  995. self.panGesture.enabled = NO;
  996. showAlert = YES;
  997. break;
  998. }
  999. default:
  1000. showAlert = NO;
  1001. break;
  1002. }
  1003. return showAlert;
  1004. }
  1005. - (BOOL)showAudioCamerIfNeed {
  1006. __block BOOL showAlert = NO;
  1007. switch ([AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeAudio]) {
  1008. case AVAuthorizationStatusDenied:{
  1009. [self.authView showAutherWith:PCSAuthMir];
  1010. self.panGesture.enabled = NO;
  1011. showAlert = YES;
  1012. break;
  1013. }
  1014. default:
  1015. showAlert = NO;
  1016. break;
  1017. }
  1018. return showAlert;
  1019. }
  1020. #pragma mark - Capture
  1021. - (void)takeStillPhoto{
  1022. if (self.isDuringTakePhoto) {
  1023. return;
  1024. }
  1025. self.panGesture.enabled = NO;
  1026. self.isDuringTakePhoto = YES;
  1027. [self takeStillPhoto:self.previewLayer];
  1028. }
  1029. - (BOOL)startRecording{
  1030. if (![self showAuthBoth]) {
  1031. if ([self showAuthorCamerIfNeed] || [self showAudioCamerIfNeed]) {
  1032. return NO;
  1033. }
  1034. } else {
  1035. return NO;
  1036. }
  1037. if(self.movieFileOutput.isRecording){
  1038. return NO;
  1039. }
  1040. [[SDKParameters shared].items enumerateObjectsUsingBlock:^(SDKParametersItem * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  1041. if (obj.model == self.mode) {
  1042. self.movieFileOutput.maxRecordedDuration = CMTimeMake(obj.limitRecordTime, 1);
  1043. }
  1044. }];
  1045. [self.recordTimer invalidate];
  1046. dispatch_async(dispatch_get_main_queue(), ^{
  1047. self.recordTimer = [NSTimer timerWithTimeInterval:0.2 repeats:YES block:^(NSTimer * _Nonnull timer) {
  1048. [self.timerLabel updateWith:self.movieFileOutput.recordedDuration.value / self.movieFileOutput.recordedDuration.timescale];
  1049. }];
  1050. [[NSRunLoop currentRunLoop] addTimer:self.recordTimer forMode:NSDefaultRunLoopMode];
  1051. });
  1052. NSURL* tmpMovieFileUrl = [NSURL fileURLWithPath:[LenzDataManager moviePath]];
  1053. AVCaptureConnection* connection = [self.movieFileOutput connectionWithMediaType:AVMediaTypeVideo];
  1054. if ([connection isVideoStabilizationSupported]) {
  1055. // connection.preferredVideoStabilizationMode = AVCaptureVideoStabilizationModeAuto;
  1056. }
  1057. if(connection == nil){
  1058. return NO;
  1059. }
  1060. UIDeviceOrientation currOrientation = UIDeviceOrientationUnknown;
  1061. connection.videoOrientation = [self videoOrientation:&currOrientation];
  1062. AVCaptureDevice* camera = self.currentCamera;
  1063. [camera lockForConfiguration:nil];
  1064. if ([camera hasFlash] && self.currentCameraInput.device.position == SDK_CAMERA_PISITION_BACK) {
  1065. if (self.outputSettings.flashMode == AVCaptureFlashModeOff) {
  1066. [camera setTorchMode:AVCaptureTorchModeOff];
  1067. } else if (self.outputSettings.flashMode == AVCaptureFlashModeOn) {
  1068. [camera setTorchMode:AVCaptureTorchModeOn];
  1069. } else {
  1070. [camera setTorchMode:AVCaptureTorchModeAuto];
  1071. }
  1072. }
  1073. [camera unlockForConfiguration];
  1074. self.thumbnailOritaionByCurrentVideoOrientation = [UIImage imageOrientationByDeviceOrientation:currOrientation isFrontCamera:camera == [CameraManager shared].front];
  1075. [self.movieFileOutput startRecordingToOutputFileURL: tmpMovieFileUrl recordingDelegate:self];
  1076. return YES;
  1077. }
  1078. - (void)stopRecording{
  1079. [self.movieFileOutput stopRecording];
  1080. AVCaptureDevice* camera = self.currentCamera;
  1081. [camera lockForConfiguration:nil];
  1082. if ([camera hasFlash] && self.currentCameraInput.device.position == SDK_CAMERA_PISITION_BACK) {
  1083. [camera setTorchMode:AVCaptureTorchModeOff];
  1084. }
  1085. [camera unlockForConfiguration];
  1086. [self.recordTimer invalidate];
  1087. self.recordTimer = nil;
  1088. }
  1089. - (BOOL)startVideoDataOutputing{
  1090. [[NSNotificationCenter defaultCenter] removeObserver:self name:PCS_NotificationNameOrientationDidChange object:nil];
  1091. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationDidChange:) name:PCS_NotificationNameOrientationDidChange object:nil];
  1092. UIDeviceOrientation deviceOrientation = UIDeviceOrientationPortrait;
  1093. AVCaptureVideoOrientation orientation = [self videoOrientation:&deviceOrientation];
  1094. CGFloat degree = 0;
  1095. AVCaptureDevice* camera = self.currentCamera;
  1096. CameraManager* cameraMgr = [CameraManager shared];
  1097. BOOL isFront = camera == cameraMgr.front;
  1098. BOOL isLandscape = NO;
  1099. // 前摄像头需要镜像一下
  1100. switch (deviceOrientation) {
  1101. case UIDeviceOrientationPortrait:
  1102. degree = 0;
  1103. break;
  1104. case UIDeviceOrientationLandscapeRight:
  1105. degree = isFront ? 90 : -90;
  1106. isLandscape = YES;
  1107. break;
  1108. case UIDeviceOrientationPortraitUpsideDown:
  1109. degree = 180;
  1110. break;
  1111. case UIDeviceOrientationLandscapeLeft:
  1112. degree = isFront ? -90 : 90;
  1113. isLandscape = YES;
  1114. break;
  1115. default:
  1116. break;
  1117. }
  1118. __block BOOL ret = NO;
  1119. dispatch_sync(self.videoDataQueue, ^{
  1120. if(self.videoDataOutput.sampleBufferDelegate != nil){
  1121. return;
  1122. }
  1123. if([self configureVideoWritter]){
  1124. AVCaptureConnection *connection = [self.videoDataOutput connectionWithMediaType:AVMediaTypeVideo];
  1125. if ([connection isVideoStabilizationSupported]) {
  1126. // connection.preferredVideoStabilizationMode = AVCaptureVideoStabilizationModeAuto;
  1127. }
  1128. if(connection == nil){
  1129. return;
  1130. }
  1131. [connection setVideoOrientation:orientation];
  1132. CGFloat aspect = isFront ? cameraMgr.frontAspect : cameraMgr.backAspect;
  1133. aspect = isLandscape ? 1.0 / aspect : aspect;
  1134. [self.videoDataOutput setSampleBufferDelegate:self queue:self.videoDataQueue];
  1135. ret = YES;
  1136. }
  1137. });
  1138. return ret;
  1139. }
  1140. - (PanoramaGuideView *)panoramaGuideView{
  1141. if (_panoramaGuideView == nil) {
  1142. _panoramaGuideView = [[PanoramaGuideView alloc]init];
  1143. _panoramaGuideView.hidden = YES;
  1144. }
  1145. return _panoramaGuideView;;
  1146. }
  1147. - (void)stopVideoDataOutputing{
  1148. [self.motionTimer invalidate];
  1149. self.motionTimer = nil;
  1150. [[NSNotificationCenter defaultCenter] removeObserver:self name:PCS_NotificationNameOrientationDidChange object:nil];
  1151. dispatch_async(self.videoDataQueue, ^{
  1152. [self.videoDataOutput setSampleBufferDelegate:nil queue:nil];
  1153. [Renderer shared].cameraPass.texture = nil;
  1154. });
  1155. [self stopMotion];
  1156. [self.panoramaGuideView showImageWith:nil];
  1157. [self updateViewStatus];
  1158. }
  1159. - (void)updateViewStatusWhenStitchSave:(BOOL)isSaveIng {
  1160. dispatch_async(dispatch_get_main_queue(), ^{
  1161. self.stitchView.hidden = !isSaveIng;
  1162. if (self.stitchView.hidden) {
  1163. [self.stitchImageView.layer removeAnimationForKey:@"loadingAnimation"];
  1164. } else {
  1165. [self.stitchImageView.layer addAnimation:self.loadingAnimation forKey:@"loadingAnimation"];
  1166. }
  1167. self.flashButton.hidden = isSaveIng;
  1168. if (self.mode == SDK_CAPTURE_MODE_INTELLEGENCE_PANORAMA) {
  1169. self.panoramOrientationGuideView.hidden = isSaveIng;
  1170. self.ai_fullView.hidden = YES;
  1171. self.panoramaGuideView.hidden = isSaveIng;
  1172. } else {
  1173. self.panoramOrientationGuideView.hidden = isSaveIng;
  1174. self.ai_fullView.hidden = YES;
  1175. self.panoramaGuideView.hidden = isSaveIng;
  1176. }
  1177. self.modeList.hidden = isSaveIng;
  1178. self.savePhotoButton.hidden = isSaveIng;
  1179. self.takePhotoButton.hidden = isSaveIng;
  1180. self.discardPhotoButton.hidden = isSaveIng;
  1181. self.backButton.hidden = !isSaveIng;
  1182. self.ablumImageView.hidden = isSaveIng;
  1183. });
  1184. }
  1185. - (void)updateViewStatusWhenStitchFail {
  1186. dispatch_async(dispatch_get_main_queue(), ^{
  1187. self.stitchView.hidden = YES;
  1188. self.flashButton.hidden = NO;
  1189. self.modeList.hidden = NO;
  1190. self.savePhotoButton.hidden = NO;
  1191. self.takePhotoButton.hidden = NO;
  1192. if (self.mode == SDK_CAPTURE_MODE_INTELLEGENCE_PANORAMA) {
  1193. self.ai_fullView.hidden = YES;
  1194. self.panoramOrientationGuideView.hidden = NO;
  1195. self.panoramaGuideView.hidden = YES;
  1196. } else {
  1197. self.ai_fullView.hidden = YES;
  1198. self.panoramOrientationGuideView.hidden = NO;
  1199. self.panoramaGuideView.hidden = NO;
  1200. }
  1201. self.discardPhotoButton.hidden = NO;
  1202. self.backButton.hidden = YES;
  1203. self.ablumImageView.hidden = NO;
  1204. [self.takePhotoButton setImage:[UIImage loadNamed:@"take-photo-btn"] forState:UIControlStateNormal];
  1205. });
  1206. }
  1207. - (BOOL)getStitchBigImageIfNeed {
  1208. BOOL isSuccess = NO;
  1209. NSString *s = [LenStitcher getCurrentPanoramaShow];
  1210. if (s.length) {
  1211. isSuccess = YES;
  1212. }
  1213. return isSuccess;
  1214. }
  1215. - (BOOL)configureVideoWritter{
  1216. // setup asset writter for the video data output
  1217. NSURL* tmpMovieFileUrl = [NSURL fileURLWithPath:[[PCSTools shared].tmpPath stringByAppendingPathComponent:@"video-data-tmp-file.mov"] isDirectory:NO];
  1218. NSError* err = nil;
  1219. [[NSFileManager defaultManager] removeItemAtURL:tmpMovieFileUrl error:nil];
  1220. self.videoWriter = [[AVAssetWriter alloc] initWithURL:tmpMovieFileUrl fileType:AVFileTypeQuickTimeMovie error:&err];
  1221. if(err != nil){
  1222. self.videoWriter = nil;
  1223. return NO;
  1224. }
  1225. [[SDKParameters shared].items enumerateObjectsUsingBlock:^(SDKParametersItem * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  1226. if (obj.model == self.mode) {
  1227. NSDictionary* settings = [NSDictionary dictionaryWithObjectsAndKeys:
  1228. AVVideoCodecTypeH264, AVVideoCodecKey,
  1229. [NSNumber numberWithInteger: obj.resolution.width], AVVideoWidthKey,
  1230. [NSNumber numberWithInteger: obj.resolution.height], AVVideoHeightKey,
  1231. nil];
  1232. self.videoWriterInput = [AVAssetWriterInput assetWriterInputWithMediaType:AVMediaTypeVideo outputSettings:settings];
  1233. }
  1234. }];
  1235. self.videoWriterInput.expectsMediaDataInRealTime = YES;
  1236. [self.videoWriter addInput:self.videoWriterInput];
  1237. return YES;
  1238. }
  1239. - (void)prepareCamera{
  1240. if (self.cameraPrepared) {
  1241. return;
  1242. }
  1243. __block AVCaptureDevice* device = nil;
  1244. [[SDKParameters shared].items enumerateObjectsUsingBlock:^(SDKParametersItem * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  1245. if (obj.model == self.mode) {
  1246. device = obj.cameraPosition == SDK_CAMERA_PISITION_BACK ? [CameraManager shared].back : [CameraManager shared].front;
  1247. }
  1248. }];
  1249. if (device == nil) {
  1250. self.cameraPrepared = false;
  1251. return;
  1252. }
  1253. NSError* error;
  1254. AVCaptureDeviceInput* input = [[AVCaptureDeviceInput alloc] initWithDevice:device error:&error];
  1255. if(error != nil){
  1256. self.cameraPrepared = false;
  1257. return;
  1258. }
  1259. [self.captureSession beginConfiguration];
  1260. if([self.captureSession canAddInput:input]){
  1261. [self.captureSession addInput:input];
  1262. }
  1263. else{
  1264. return;
  1265. }
  1266. self.previewLayer = [AVCaptureVideoPreviewLayer layerWithSession:self.captureSession];
  1267. self.previewLayer.frame = self.view.layer.bounds;
  1268. self.previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
  1269. [self.view.layer insertSublayer:self.previewLayer atIndex:0u];
  1270. if([self.captureSession canAddOutput:self.photoOutput]){
  1271. [self.captureSession addOutput:self.photoOutput];
  1272. }
  1273. if([self.captureSession canAddOutput:self.movieFileOutput]){
  1274. [self.captureSession addOutput:self.movieFileOutput];
  1275. }
  1276. [self.captureSession commitConfiguration];
  1277. self.cameraPrepared = YES;
  1278. [[SDKParameters shared].items enumerateObjectsUsingBlock:^(SDKParametersItem * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  1279. if (obj.model == self.mode) {
  1280. [[CameraManager shared] setResolutionFor:AVCaptureDevicePositionBack with:obj.resolution.height model:self.mode];
  1281. [[CameraManager shared] setResolutionFor:AVCaptureDevicePositionFront with:obj.resolution.height model:self.mode];
  1282. }
  1283. }];
  1284. [self updatePreViewLayer];
  1285. }
  1286. #pragma mark - Gesture
  1287. - (void)tapGestureHandler: (UITapGestureRecognizer*)gesture{
  1288. switch(gesture.state){
  1289. case UIGestureRecognizerStateBegan:{
  1290. break;
  1291. }
  1292. case UIGestureRecognizerStateChanged:{
  1293. break;
  1294. }
  1295. case UIGestureRecognizerStateEnded:{
  1296. CGPoint pt = [gesture locationInView:self.view];
  1297. [self focusOn:pt];
  1298. break;
  1299. }
  1300. case UIGestureRecognizerStateCancelled:{
  1301. break;
  1302. }
  1303. default:
  1304. break;
  1305. }
  1306. }
  1307. - (void)panGestureHandler: (UIPanGestureRecognizer*)gesture{
  1308. switch(gesture.state){
  1309. case UIGestureRecognizerStateBegan:{
  1310. break;
  1311. }
  1312. case UIGestureRecognizerStateChanged:{
  1313. if(self.flag){
  1314. return;
  1315. }
  1316. self.flag = YES;
  1317. CGPoint translation = [gesture translationInView:self.view];
  1318. [self.modeList updateIndexByTanslation:translation];
  1319. break;
  1320. }
  1321. case UIGestureRecognizerStateEnded:{
  1322. self.flag = NO;
  1323. break;
  1324. }
  1325. case UIGestureRecognizerStateCancelled:{
  1326. self.flag = NO;
  1327. break;
  1328. }
  1329. default:
  1330. self.flag = NO;
  1331. break;
  1332. }
  1333. }
  1334. #pragma mark - orientation
  1335. //- (UIInterfaceOrientationMask)supportedInterfaceOrientations{
  1336. // return UIInterfaceOrientationMaskPortrait;
  1337. //}
  1338. - (void)updateUIByMode{
  1339. self.albumButton.hidden = YES;
  1340. self.numberOfImagesLabel.hidden = YES;
  1341. self.modeLabel.text = self.currentOperationNode.modeTitleString;
  1342. self.ai_fullView.hidden = YES;
  1343. self.panGesture.enabled = YES;
  1344. if([self.currentOperationNode respondsToSelector:@selector(updateUI)]){
  1345. [self.currentOperationNode updateUI];
  1346. }
  1347. [self updatePreViewLayer];
  1348. [self updateUIByDir];
  1349. }
  1350. - (void)updateUIByDir {
  1351. if (self.mode == SDK_CAPTURE_MODE_PANORAMA) {
  1352. [self updatePanormaViewWith:self.currentTgDirection];
  1353. } else {
  1354. [self.modeLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
  1355. make.centerX.mas_equalTo(self.view);
  1356. make.centerY.mas_equalTo(self.flashButton);
  1357. }];
  1358. self.modeLabel.transform = CGAffineTransformMakeRotation(0);
  1359. [self.timerLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
  1360. make.edges.mas_equalTo(self.modeLabel);
  1361. }];
  1362. self.timerLabel.transform = CGAffineTransformMakeRotation(0);
  1363. }
  1364. }
  1365. - (void)updatePreViewLayer {
  1366. CGFloat width = [UIScreen mainScreen].bounds.size.width;
  1367. CGFloat height = [UIScreen mainScreen].bounds.size.height;
  1368. self.previewLayer.frame = CGRectMake(0, 0, width, height);
  1369. switch (self.mode) {
  1370. case SDK_CAPTURE_MODE_CONTINUOUS: {
  1371. self.switchButton.hidden = NO;
  1372. self.panoramaGuideView.hidden = YES;
  1373. self.panoramOrientationGuideView.hidden = YES;
  1374. // self.previewLayer.frame = CGRectMake(0, (height - width*4/3)/2, width, width*4/3);
  1375. }
  1376. break;
  1377. case SDK_CAPTURE_MODE_SINGLE: {
  1378. self.switchButton.hidden = NO;
  1379. self.panoramaGuideView.hidden = YES;
  1380. self.panoramOrientationGuideView.hidden = YES;
  1381. // self.previewLayer.frame = CGRectMake(0, (height - width*4/3)/2, width, width*4/3);
  1382. }
  1383. break;
  1384. case SDK_CAPTURE_MODE_MOVIE: {
  1385. self.switchButton.hidden = NO;
  1386. self.panoramaGuideView.hidden = YES;
  1387. self.panoramOrientationGuideView.hidden = YES;
  1388. // self.previewLayer.frame = CGRectMake(0, 0, width, height);
  1389. }
  1390. break;
  1391. case SDK_CAPTURE_MODE_PANORAMA: {
  1392. self.switchButton.hidden = YES;
  1393. self.panoramaGuideView.hidden = NO;
  1394. self.panoramOrientationGuideView.hidden = NO;
  1395. // self.previewLayer.frame = CGRectMake(0, (height - width*16/9)/2, width, width*16/9);
  1396. }
  1397. break;
  1398. case SDK_CAPTURE_MODE_INTELLEGENCE_PANORAMA: {
  1399. self.switchButton.hidden = YES;
  1400. self.panoramaGuideView.hidden = YES;
  1401. self.panoramOrientationGuideView.hidden = NO;
  1402. // self.previewLayer.frame = CGRectMake(0, (height - width*16/9)/2, width, width*16/9);
  1403. }
  1404. break;
  1405. default:
  1406. break;
  1407. }
  1408. [self.view setNeedsDisplay];
  1409. [self.view layoutIfNeeded];
  1410. }
  1411. - (void)modeWillChange: (NSUInteger)newIndex oldIndex: (NSUInteger)oldIndex{
  1412. if(newIndex >= INT_MAX){
  1413. return;
  1414. }
  1415. [self.currentOperationNode willQuitOnCompletion:^() {}];
  1416. }
  1417. - (void)modeDidChange: (NSUInteger)newIndex oldIndex: (NSUInteger)oldIndex{
  1418. if(newIndex >= INT_MAX){
  1419. return;
  1420. }
  1421. // if(self.effectView.superview != nil){
  1422. // [UIView animateWithDuration:0.5 animations:^{
  1423. // self.effectView.effect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
  1424. // }];
  1425. // }
  1426. if(newIndex != SDK_CAPTURE_MODE_MOVIE){
  1427. [[SDKParameters shared].items enumerateObjectsUsingBlock:^(SDKParametersItem * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  1428. if (obj.model == self.mode) {
  1429. [[CameraManager shared] setResolutionFor:AVCaptureDevicePositionBack with:obj.resolution.height model:self.mode];
  1430. [[CameraManager shared] setResolutionFor:AVCaptureDevicePositionFront with:obj.resolution.height model:self.mode];
  1431. }
  1432. }];
  1433. }
  1434. else{
  1435. [[SDKParameters shared].items enumerateObjectsUsingBlock:^(SDKParametersItem * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  1436. if (obj.model == self.mode) {
  1437. [[CameraManager shared] setResolutionFor:AVCaptureDevicePositionBack with:obj.videoResolution.height model:self.mode];
  1438. [[CameraManager shared] setResolutionFor:AVCaptureDevicePositionFront with:obj.videoResolution.height model:self.mode];
  1439. }
  1440. }];
  1441. }
  1442. // self.effectView.effect = nil;
  1443. [self updateUIByMode];
  1444. [self switchDevicePositionBack];
  1445. [self updateNumberLabel];
  1446. }
  1447. - (void)updateCaptureSessionWith:(NSInteger)oldIndex {
  1448. PhotographModeEnum oldModeEnum = (PhotographModeEnum)(oldIndex);
  1449. if((self.mode == SDK_CAPTURE_MODE_PANORAMA || self.mode == SDK_CAPTURE_MODE_INTELLEGENCE_PANORAMA)
  1450. && oldModeEnum != SDK_CAPTURE_MODE_PANORAMA
  1451. && oldModeEnum != SDK_CAPTURE_MODE_INTELLEGENCE_PANORAMA){
  1452. if([self.captureSession canAddOutput:self.videoDataOutput]){
  1453. [self.captureSession addOutput:self.videoDataOutput];
  1454. [self.captureSession removeOutput:self.movieFileOutput];
  1455. }
  1456. }
  1457. else if((self.mode == SDK_CAPTURE_MODE_MOVIE)
  1458. && (oldModeEnum == SDK_CAPTURE_MODE_PANORAMA
  1459. || oldModeEnum == SDK_CAPTURE_MODE_INTELLEGENCE_PANORAMA)){
  1460. if([self.captureSession canAddOutput:self.movieFileOutput]){
  1461. [self.captureSession addOutput:self.movieFileOutput];
  1462. [self.captureSession removeOutput:self.videoDataOutput];
  1463. }
  1464. }
  1465. }
  1466. - (void)setupMicrophone{
  1467. NSError* audioError = nil;
  1468. AVCaptureDevice* audioDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeAudio];
  1469. AVCaptureDeviceInput* audioInput = [AVCaptureDeviceInput deviceInputWithDevice:audioDevice error:&audioError];
  1470. if(audioError == nil){
  1471. AVCaptureAudioDataOutput* audioOutput = [[AVCaptureAudioDataOutput alloc] init];
  1472. [audioOutput setSampleBufferDelegate:self queue:dispatch_get_global_queue(0, 0)];
  1473. if([self.captureSession canAddInput:audioInput]){
  1474. [self.captureSession addInput:audioInput];
  1475. }
  1476. if([self.captureSession canAddOutput:audioOutput]){
  1477. [self.captureSession addOutput:audioOutput];
  1478. }
  1479. }
  1480. }
  1481. - (UIStatusBarStyle)preferredStatusBarStyle{
  1482. return UIStatusBarStyleLightContent;
  1483. }
  1484. #pragma mark - DeviceOrigatrion Delegate
  1485. - (void)directionChange:(TgDirection)direction {
  1486. self.currentTgDirection = direction;
  1487. if (self.mode != SDK_CAPTURE_MODE_PANORAMA && self.mode != SDK_CAPTURE_MODE_INTELLEGENCE_PANORAMA) {
  1488. return;
  1489. }
  1490. OperationNodeVideoBase *base = nil;
  1491. if ([self.currentOperationNode isKindOfClass:[OperationNodeVideoBase class]]) {
  1492. base = (OperationNodeVideoBase *)self.currentOperationNode;
  1493. }
  1494. if (base.isRecording) {
  1495. return;
  1496. }
  1497. self.panoramaGuideView.dir = self.currentTgDirection;
  1498. self.panoramaGuideView.orginDir = self.panoramOrientationGuideView.selectedItemIndex;
  1499. [self updatePanormaViewWith:direction];
  1500. }
  1501. - (void)updatePanormaViewWith:(TgDirection)direction {
  1502. CGFloat height = [UIScreen mainScreen].bounds.size.height;
  1503. CGFloat width = [UIScreen mainScreen].bounds.size.width;
  1504. switch (direction) {
  1505. case TgDirectionPortrait:{
  1506. [self panoramaOrientationViewslt:self.panoramOrientationGuideView.selectedItemIndex];
  1507. [self.modeLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
  1508. make.centerX.mas_equalTo(self.view);
  1509. make.centerY.mas_equalTo(self.flashButton);
  1510. }];
  1511. [self.aiPanPromtLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
  1512. make.centerX.mas_equalTo(self.view);
  1513. make.top.mas_equalTo(self.modeLabel.mas_bottom).mas_offset(20);
  1514. make.width.mas_offset(240);
  1515. make.height.mas_offset(20);
  1516. }];
  1517. self.modeLabel.transform = CGAffineTransformMakeRotation(0);
  1518. [self.timerLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
  1519. make.edges.mas_equalTo(self.modeLabel);
  1520. }];
  1521. self.timerLabel.transform = CGAffineTransformMakeRotation(0);
  1522. self.svContentView.transform = CGAffineTransformMakeRotation(0);
  1523. self.aiPanPromtLabel.transform = CGAffineTransformMakeRotation(0);
  1524. }
  1525. break;
  1526. case TgDirectionDown:{
  1527. if (self.panoramOrientationGuideView.selectedItemIndex == PanoramaOrientationViewLeft) {
  1528. [self panoramaOrientationViewslt:PanoramaOrientationViewRight];
  1529. } else if (self.panoramOrientationGuideView.selectedItemIndex == PanoramaOrientationViewRight) {
  1530. [self panoramaOrientationViewslt:PanoramaOrientationViewLeft];
  1531. } else if (self.panoramOrientationGuideView.selectedItemIndex == PanoramaOrientationViewUp) {
  1532. [self panoramaOrientationViewslt:PanoramaOrientationViewDown];
  1533. } else if (self.panoramOrientationGuideView.selectedItemIndex == PanoramaOrientationViewDown) {
  1534. [self panoramaOrientationViewslt:PanoramaOrientationViewUp];
  1535. }
  1536. [self.modeLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
  1537. make.centerX.mas_equalTo(self.view);
  1538. make.centerY.mas_equalTo(self.flashButton);
  1539. }];
  1540. [self.aiPanPromtLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
  1541. make.centerX.mas_equalTo(self.view);
  1542. make.top.mas_equalTo(self.modeLabel.mas_bottom).mas_offset(20);
  1543. make.width.mas_offset(240);
  1544. make.height.mas_offset(20);
  1545. }];
  1546. self.modeLabel.transform = CGAffineTransformMakeRotation(0);
  1547. [self.timerLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
  1548. make.edges.mas_equalTo(self.modeLabel);
  1549. }];
  1550. self.timerLabel.transform = CGAffineTransformMakeRotation(0);
  1551. self.svContentView.transform = CGAffineTransformMakeRotation(0);
  1552. self.aiPanPromtLabel.transform = CGAffineTransformMakeRotation(0);
  1553. }
  1554. break;
  1555. case TgDirectionRight:{
  1556. if (self.panoramOrientationGuideView.selectedItemIndex == PanoramaOrientationViewLeft) {
  1557. [self panoramaOrientationViewslt:PanoramaOrientationViewDown];
  1558. [self.modeLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
  1559. make.top.mas_equalTo((height - 40)/2);
  1560. make.height.mas_offset(22);
  1561. make.right.mas_offset(-(width-80));
  1562. }];
  1563. [self.aiPanPromtLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
  1564. make.centerY.mas_equalTo(self.view);
  1565. make.right.mas_offset(-(width-200));
  1566. make.width.mas_offset(240);
  1567. make.height.mas_offset(20);
  1568. }];
  1569. self.modeLabel.transform = CGAffineTransformMakeRotation(-M_PI/2);
  1570. self.aiPanPromtLabel.transform = CGAffineTransformMakeRotation(-M_PI/2);
  1571. [self.timerLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
  1572. make.edges.mas_equalTo(self.modeLabel);
  1573. }];
  1574. self.timerLabel.transform = CGAffineTransformMakeRotation(-M_PI/2);
  1575. self.svContentView.transform = CGAffineTransformMakeRotation(-M_PI/2);
  1576. } else if (self.panoramOrientationGuideView.selectedItemIndex == PanoramaOrientationViewRight) {
  1577. [self panoramaOrientationViewslt:PanoramaOrientationViewUp];
  1578. [self.modeLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
  1579. make.top.mas_equalTo((height - 40)/2);
  1580. make.height.mas_offset(22);
  1581. make.right.mas_offset(-(width-80));
  1582. }];
  1583. [self.aiPanPromtLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
  1584. make.centerY.mas_equalTo(self.view);
  1585. make.right.mas_offset(-(width-200));
  1586. make.width.mas_offset(240);
  1587. make.height.mas_offset(20);
  1588. }];
  1589. self.modeLabel.transform = CGAffineTransformMakeRotation(-M_PI/2);
  1590. self.aiPanPromtLabel.transform = CGAffineTransformMakeRotation(-M_PI/2);
  1591. [self.timerLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
  1592. make.edges.mas_equalTo(self.modeLabel);
  1593. }];
  1594. self.timerLabel.transform = CGAffineTransformMakeRotation(-M_PI/2);
  1595. self.svContentView.transform = CGAffineTransformMakeRotation(-M_PI/2);
  1596. } else if (self.panoramOrientationGuideView.selectedItemIndex == PanoramaOrientationViewUp) {
  1597. [self panoramaOrientationViewslt:PanoramaOrientationViewLeft];
  1598. [self.modeLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
  1599. make.centerX.mas_equalTo(self.view);
  1600. make.centerY.mas_equalTo(self.flashButton);
  1601. }];
  1602. [self.aiPanPromtLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
  1603. make.centerX.mas_equalTo(self.view);
  1604. make.top.mas_equalTo(self.modeLabel.mas_bottom).mas_offset(20);
  1605. make.width.mas_offset(240);
  1606. make.height.mas_offset(20);
  1607. }];
  1608. self.modeLabel.transform = CGAffineTransformMakeRotation(0);
  1609. [self.timerLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
  1610. make.edges.mas_equalTo(self.modeLabel);
  1611. }];
  1612. self.timerLabel.transform = CGAffineTransformMakeRotation(0);
  1613. self.svContentView.transform = CGAffineTransformMakeRotation(0);
  1614. self.aiPanPromtLabel.transform = CGAffineTransformMakeRotation(0);
  1615. } else if (self.panoramOrientationGuideView.selectedItemIndex == PanoramaOrientationViewDown) {
  1616. [self panoramaOrientationViewslt:PanoramaOrientationViewRight];
  1617. [self.modeLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
  1618. make.centerX.mas_equalTo(self.view);
  1619. make.centerY.mas_equalTo(self.flashButton);
  1620. }];
  1621. [self.aiPanPromtLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
  1622. make.centerX.mas_equalTo(self.view);
  1623. make.top.mas_equalTo(self.modeLabel.mas_bottom).mas_offset(20);
  1624. make.width.mas_offset(240);
  1625. make.height.mas_offset(20);
  1626. }];
  1627. self.modeLabel.transform = CGAffineTransformMakeRotation(0);
  1628. [self.timerLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
  1629. make.edges.mas_equalTo(self.modeLabel);
  1630. }];
  1631. self.timerLabel.transform = CGAffineTransformMakeRotation(0);
  1632. self.svContentView.transform = CGAffineTransformMakeRotation(0);
  1633. self.aiPanPromtLabel.transform = CGAffineTransformMakeRotation(0);
  1634. }
  1635. }
  1636. break;
  1637. case TgDirectionLeft:{
  1638. if (self.panoramOrientationGuideView.selectedItemIndex == PanoramaOrientationViewLeft) {
  1639. [self panoramaOrientationViewslt:PanoramaOrientationViewUp];
  1640. [self.modeLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
  1641. make.top.mas_equalTo((height - 40)/2);
  1642. make.height.mas_offset(22);
  1643. make.left.mas_offset(width-80);
  1644. }];
  1645. [self.aiPanPromtLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
  1646. make.centerY.mas_equalTo(self.view);
  1647. make.left.mas_offset(width-200);
  1648. make.width.mas_offset(240);
  1649. make.height.mas_offset(20);
  1650. }];
  1651. self.modeLabel.transform = CGAffineTransformMakeRotation(M_PI/2);
  1652. self.aiPanPromtLabel.transform = CGAffineTransformMakeRotation(M_PI/2);
  1653. [self.timerLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
  1654. make.edges.mas_equalTo(self.modeLabel);
  1655. }];
  1656. self.timerLabel.transform = CGAffineTransformMakeRotation(M_PI/2);
  1657. self.svContentView.transform = CGAffineTransformMakeRotation(M_PI/2);
  1658. } else if (self.panoramOrientationGuideView.selectedItemIndex == PanoramaOrientationViewRight) {
  1659. [self panoramaOrientationViewslt:PanoramaOrientationViewDown];
  1660. [self.modeLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
  1661. make.top.mas_equalTo((height - 40)/2);
  1662. make.height.mas_offset(22);
  1663. make.left.mas_offset(width-80);
  1664. }];
  1665. [self.aiPanPromtLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
  1666. make.centerY.mas_equalTo(self.view);
  1667. make.left.mas_offset(width-200);
  1668. make.width.mas_offset(240);
  1669. make.height.mas_offset(20);
  1670. }];
  1671. self.modeLabel.transform = CGAffineTransformMakeRotation(M_PI/2);
  1672. self.aiPanPromtLabel.transform = CGAffineTransformMakeRotation(M_PI/2);
  1673. [self.timerLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
  1674. make.edges.mas_equalTo(self.modeLabel);
  1675. }];
  1676. self.timerLabel.transform = CGAffineTransformMakeRotation(M_PI/2);
  1677. self.svContentView.transform = CGAffineTransformMakeRotation(M_PI/2);
  1678. } else if (self.panoramOrientationGuideView.selectedItemIndex == PanoramaOrientationViewUp) {
  1679. [self panoramaOrientationViewslt:PanoramaOrientationViewRight];
  1680. [self.modeLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
  1681. make.centerX.mas_equalTo(self.view);
  1682. make.centerY.mas_equalTo(self.flashButton);
  1683. }];
  1684. [self.aiPanPromtLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
  1685. make.centerX.mas_equalTo(self.view);
  1686. make.top.mas_equalTo(self.modeLabel.mas_bottom).mas_offset(20);
  1687. make.width.mas_offset(240);
  1688. make.height.mas_offset(20);
  1689. }];
  1690. self.modeLabel.transform = CGAffineTransformMakeRotation(0);
  1691. [self.timerLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
  1692. make.edges.mas_equalTo(self.modeLabel);
  1693. }];
  1694. self.timerLabel.transform = CGAffineTransformMakeRotation(0);
  1695. self.svContentView.transform = CGAffineTransformMakeRotation(0);
  1696. self.aiPanPromtLabel.transform = CGAffineTransformMakeRotation(0);
  1697. } else if (self.panoramOrientationGuideView.selectedItemIndex == PanoramaOrientationViewDown) {
  1698. [self panoramaOrientationViewslt:PanoramaOrientationViewLeft];
  1699. [self.modeLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
  1700. make.centerX.mas_equalTo(self.view);
  1701. make.centerY.mas_equalTo(self.flashButton);
  1702. }];
  1703. [self.aiPanPromtLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
  1704. make.centerX.mas_equalTo(self.view);
  1705. make.top.mas_equalTo(self.modeLabel.mas_bottom).mas_offset(20);
  1706. make.width.mas_offset(240);
  1707. make.height.mas_offset(20);
  1708. }];
  1709. self.modeLabel.transform = CGAffineTransformMakeRotation(0);
  1710. [self.timerLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
  1711. make.edges.mas_equalTo(self.modeLabel);
  1712. }];
  1713. self.timerLabel.transform = CGAffineTransformMakeRotation(0);
  1714. self.svContentView.transform = CGAffineTransformMakeRotation(0);
  1715. self.aiPanPromtLabel.transform = CGAffineTransformMakeRotation(0);
  1716. }
  1717. }
  1718. break;
  1719. default:
  1720. break;
  1721. }
  1722. }
  1723. #pragma mark 方向调用的代理
  1724. - (void)panoramDirDidChange {
  1725. [self directionChange:self.currentTgDirection];
  1726. }
  1727. - (void)panoramaOrientationViewslt:(PanoramaOrientationViewDirectionEnum)direction{
  1728. if (self.mode == SDK_CAPTURE_MODE_PANORAMA) {
  1729. self.panoramaGuideView.hidden = NO;
  1730. } else {
  1731. OperationNodeVideoBase *base = nil;
  1732. if ([self.currentOperationNode isKindOfClass:[OperationNodeVideoBase class]]) {
  1733. base = (OperationNodeVideoBase *)self.currentOperationNode;
  1734. }
  1735. if (base.isRecording) {
  1736. self.panoramaGuideView.hidden = NO;
  1737. } else {
  1738. self.panoramaGuideView.hidden = YES;
  1739. }
  1740. }
  1741. self.panoramaGuideView.hidden = !(self.mode == SDK_CAPTURE_MODE_PANORAMA);
  1742. [UIView animateWithDuration:0.25 animations:^{
  1743. [self.panoramaGuideView mas_remakeConstraints:^(MASConstraintMaker *make) {
  1744. switch (direction) {
  1745. case PanoramaOrientationViewLeft:
  1746. case PanoramaOrientationViewRight:
  1747. {
  1748. make.left.right.offset(0);
  1749. make.centerY.mas_equalTo(self.view);
  1750. make.height.mas_equalTo(PanoramaGuideViewWOrH);
  1751. }
  1752. break;
  1753. case PanoramaOrientationViewUp:
  1754. case PanoramaOrientationViewDown:
  1755. {
  1756. make.centerX.mas_equalTo(self.view);
  1757. make.width.mas_equalTo(PanoramaGuideViewWOrH);
  1758. make.bottom.mas_equalTo(self.panoramOrientationGuideView.mas_top).offset(-25);
  1759. if (self.currentTgDirection == TgDirectionLeft || self.currentTgDirection == TgDirectionRight) {
  1760. make.top.offset(50);
  1761. } else {
  1762. make.top.mas_equalTo(self.timerLabel.mas_bottom).offset(15);
  1763. }
  1764. }
  1765. break;
  1766. case PanoramaOrientationViewAny: {
  1767. self.panoramaGuideView.hidden = YES;
  1768. }
  1769. break;
  1770. default:
  1771. break;
  1772. }
  1773. }];
  1774. // if (self.mode != SDK_CAPTURE_MODE_INTELLEGENCE_PANORAMA) {
  1775. // [self.panoramaGuideView setNeedsLayout];
  1776. // [self.panoramaGuideView layoutIfNeeded];
  1777. // }
  1778. } completion:^(BOOL finished) {
  1779. self.panoramaGuideView.dir = self.currentTgDirection;
  1780. self.panoramaGuideView.orginDir = self.panoramOrientationGuideView.selectedItemIndex;
  1781. [self.panoramaGuideView direction:direction];
  1782. // [self updatePanormaViewWith:direction];
  1783. }];
  1784. }
  1785. - (void)stopAllSensors{
  1786. dispatch_async(self.videoQueue, ^{
  1787. [self.captureSession stopRunning];
  1788. });
  1789. [[PCSMotionManager shared] stopAccelerometerUpdates];
  1790. }
  1791. #pragma mark - session
  1792. - (void)turnTorchOff{
  1793. AVCaptureDevice* currentDevice = self.currentCamera;
  1794. if(currentDevice == nil || !currentDevice.hasTorch){
  1795. return;
  1796. }
  1797. NSError* error;
  1798. if(error == nil){
  1799. [currentDevice lockForConfiguration:&error];
  1800. currentDevice.torchMode = AVCaptureTorchModeOff;
  1801. [currentDevice unlockForConfiguration];
  1802. }
  1803. }
  1804. - (void)stopCaptureSession{
  1805. [self turnTorchOff];
  1806. if (self.captureSession.isRunning) {
  1807. dispatch_async(self.videoQueue, ^{
  1808. [self.captureSession stopRunning];
  1809. });
  1810. }
  1811. }
  1812. - (void)startCaptureSession{
  1813. if (!self.cameraPrepared) {
  1814. [self prepareCamera];
  1815. }
  1816. if (self.cameraPrepared && (!self.captureSession.isRunning)) {
  1817. // dispatch_async(dispatch_get_global_queue(0, 0), ^{
  1818. dispatch_async(self.videoQueue, ^{
  1819. [self.captureSession startRunning];
  1820. });
  1821. // [self.captureSession startRunning];
  1822. // });
  1823. }
  1824. }
  1825. #pragma mark - Btn call back
  1826. -(IBAction)switchCameraTapped:(id)sender
  1827. {
  1828. if(self.captureSession == nil){
  1829. return;
  1830. }
  1831. AVCaptureDeviceInput* currInput = self.currentCameraInput;
  1832. if(currInput == nil){
  1833. return;
  1834. }
  1835. AVCaptureDevice* currentDevice = currInput.device;
  1836. AVCaptureDevice *newCamera = nil;
  1837. if(currentDevice.position == AVCaptureDevicePositionBack){
  1838. newCamera = [CameraManager shared].front;
  1839. }
  1840. else if(currentDevice.position == AVCaptureDevicePositionFront){
  1841. newCamera = [CameraManager shared].back;
  1842. }
  1843. else{
  1844. return;
  1845. }
  1846. [self.captureSession beginConfiguration];
  1847. if(currentDevice.hasTorch){
  1848. NSError* error;
  1849. [currentDevice lockForConfiguration:&error];
  1850. currentDevice.torchMode = AVCaptureTorchModeOff;
  1851. [currentDevice unlockForConfiguration];
  1852. }
  1853. [self.captureSession removeInput:currInput];
  1854. self.flashButton.hidden = !newCamera.hasFlash;
  1855. NSError *err = nil;
  1856. AVCaptureDeviceInput *newVideoInput = [[AVCaptureDeviceInput alloc] initWithDevice:newCamera error:&err];
  1857. if(newVideoInput == nil || err != nil){
  1858. }
  1859. else if([self.captureSession canAddInput:newVideoInput]){
  1860. [self.captureSession addInput:newVideoInput];
  1861. }
  1862. [self.captureSession commitConfiguration];
  1863. }
  1864. - (void)switchDevicePositionBack {
  1865. if(self.captureSession == nil ||
  1866. (self.mode != SDK_CAPTURE_MODE_PANORAMA && self.mode != SDK_CAPTURE_MODE_INTELLEGENCE_PANORAMA)){
  1867. return;
  1868. }
  1869. AVCaptureDeviceInput* currInput = self.currentCameraInput;
  1870. if(currInput == nil){
  1871. return;
  1872. }
  1873. AVCaptureDevice* currentDevice = currInput.device;
  1874. AVCaptureDevice *newCamera = nil;
  1875. if(currentDevice.position == AVCaptureDevicePositionBack){
  1876. return;
  1877. }
  1878. if(currentDevice.position == AVCaptureDevicePositionFront){
  1879. newCamera = [CameraManager shared].back;
  1880. }
  1881. [self.captureSession beginConfiguration];
  1882. if(currentDevice.hasTorch){
  1883. NSError* error;
  1884. [currentDevice lockForConfiguration:&error];
  1885. currentDevice.torchMode = AVCaptureTorchModeOff;
  1886. [currentDevice unlockForConfiguration];
  1887. }
  1888. [self.captureSession removeInput:currInput];
  1889. NSError *err = nil;
  1890. AVCaptureDeviceInput *newVideoInput = [[AVCaptureDeviceInput alloc] initWithDevice:newCamera error:&err];
  1891. if(newVideoInput == nil || err != nil){
  1892. } else if([self.captureSession canAddInput:newVideoInput]){
  1893. [self.captureSession addInput:newVideoInput];
  1894. }
  1895. [self.captureSession commitConfiguration];
  1896. }
  1897. - (void)takePhotoButtonTouchUpInside:(id)sender{
  1898. if(![self.currentOperationNode respondsToSelector:@selector(enable)] || self.currentOperationNode.enable){
  1899. if (self.mode == SDK_CAPTURE_MODE_MOVIE) {
  1900. __block SDKParametersItem *videoItems = nil;
  1901. [[SDKParameters shared].items enumerateObjectsUsingBlock:^(SDKParametersItem * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  1902. if (obj.model == self.mode) {
  1903. videoItems = obj;
  1904. }
  1905. }];
  1906. if (videoItems.quantity > 0 && self.resourceModel.movieArray.count >= videoItems.quantity) {
  1907. [SVProgressHUD showImage:[UIImage imageNamed:@""] status:[NSString stringWithFormat:@"最多录制%ld条视频", videoItems.quantity]];
  1908. return;
  1909. }
  1910. }
  1911. if (self.mode == SDK_CAPTURE_MODE_INTELLEGENCE_PANORAMA) {
  1912. OperationNodeAIPanorama *aiPan = nil;
  1913. if ([self.currentOperationNode isKindOfClass:[OperationNodeAIPanorama class]]) {
  1914. aiPan = self.currentOperationNode;
  1915. if (!aiPan.isRecording) {
  1916. aiPan.AIType = OperationAITypeTap;
  1917. }
  1918. }
  1919. }
  1920. [self.currentOperationNode takePhotoButtonTouched];
  1921. if (self.mode == SDK_CAPTURE_MODE_PANORAMA && !self.isVideoStitchStopedWhenRecording) {
  1922. [LenStitcher videoResetPath];
  1923. LenzResourceItemModel *itemModel = [[LenzResourceItemModel alloc]init];
  1924. [self.resourceModel.panoramArray addObject:itemModel];
  1925. [self startMotion];
  1926. }
  1927. }
  1928. }
  1929. - (void)longPressGes:(UILongPressGestureRecognizer *)ges {
  1930. switch (ges.state) {
  1931. case UIGestureRecognizerStateBegan:
  1932. {
  1933. if (self.mode == SDK_CAPTURE_MODE_INTELLEGENCE_PANORAMA) {
  1934. OperationNodeAIPanorama *aiPan = nil;
  1935. if ([self.currentOperationNode isKindOfClass:[OperationNodeAIPanorama class]]) {
  1936. [LenStitcher videoResetPath];
  1937. LenzResourceItemModel *itemModel = [[LenzResourceItemModel alloc]init];
  1938. itemModel.isStitch = YES;
  1939. [self.resourceModel.aiPanoramArray addObject:itemModel];
  1940. [self startMotion];
  1941. aiPan = self.currentOperationNode;
  1942. aiPan.AIType = OperationAITypeLongPress;
  1943. [self.currentOperationNode takePhotoButtonTouched];
  1944. }
  1945. }
  1946. }
  1947. break;
  1948. case UIGestureRecognizerStateEnded:
  1949. break;
  1950. default:
  1951. break;
  1952. }
  1953. }
  1954. - (void)takePhotoButtonTouchDownInside:(id)sender{
  1955. }
  1956. - (void)flashButtonTouchUpInside:(id)sender{
  1957. AVCaptureDevice* currentDevice = self.currentCamera;
  1958. if(currentDevice == nil || !currentDevice.hasFlash){
  1959. return;
  1960. }
  1961. NSError* error = nil;
  1962. [currentDevice lockForConfiguration:&error];
  1963. if(error != nil){
  1964. return;
  1965. }
  1966. if(self.outputSettings.flashMode == AVCaptureFlashModeOn){
  1967. self.outputSettings.flashMode = AVCaptureFlashModeAuto;
  1968. [self.flashButton setImage:[UIImage sdkImageNamed:@"flash-auto-btn"] forState:UIControlStateNormal];
  1969. }
  1970. else if(self.outputSettings.flashMode == AVCaptureFlashModeAuto){
  1971. self.outputSettings.flashMode = AVCaptureFlashModeOff;
  1972. [self.flashButton setImage:[UIImage sdkImageNamed:@"flash-disable-btn"] forState:UIControlStateNormal];
  1973. }
  1974. else{
  1975. self.outputSettings.flashMode = AVCaptureFlashModeOn;
  1976. [self.flashButton setImage:[UIImage sdkImageNamed:@"flash-btn"] forState:UIControlStateNormal];
  1977. }
  1978. [currentDevice unlockForConfiguration];
  1979. }
  1980. - (void)backButtonTouchUpInside:(id)sender{
  1981. if (!self.stitchView.hidden) {
  1982. [QuitMultipleModeAlertViewController show:self title:@"确认提醒" text:@"图片拼接中,返回会清空正在拼接的数据" leftBtnTitle:@"取消" rightBtnTitle:@"确定" withLeftButtonCallBack:^(QuitMultipleModeAlertViewController * _Nonnull alertController) {
  1983. [alertController dismissViewControllerAnimated:NO completion:^{}];
  1984. } rightButtonCallBack:^(QuitMultipleModeAlertViewController * _Nonnull alertController) {
  1985. [self updateViewStatusWhenStitchSave:NO];
  1986. [self.panoramaGuideView clearImage];
  1987. [self.panoramaGuideView showImageWith:nil];
  1988. [alertController dismissViewControllerAnimated:NO completion:^{
  1989. }];
  1990. }];
  1991. } else {
  1992. [self stopCaptureSession];
  1993. [[PCSMotionManager shared] stopAccelerometerUpdates];
  1994. [self dismissViewControllerAnimated:YES completion:^{
  1995. }];
  1996. }
  1997. }
  1998. - (void)makeResData:(NSMutableArray <LenzResourceItemModel *> *)array {
  1999. [array enumerateObjectsUsingBlock:^(LenzResourceItemModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  2000. }];
  2001. }
  2002. - (void)savePhotoButtonTouchUpInside:(id)sender{
  2003. //待优化
  2004. NSMutableArray *res = [NSMutableArray array];
  2005. __block NSInteger idIndex = 1;
  2006. [self.resourceModel.singleArray enumerateObjectsUsingBlock:^(LenzResourceItemModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  2007. idIndex += idx;
  2008. NSMutableDictionary *dict = [NSMutableDictionary dictionary];
  2009. dict[@"id"] = [NSString stringWithFormat:@"%ld", idIndex];
  2010. dict[@"path"] = obj.path;
  2011. dict[@"cameraMode"] = obj.cameraMode;
  2012. dict[@"cameraType"] = obj.cameraType;
  2013. dict[@"isRemake"] = @(obj.isRemake);
  2014. [res addObject:dict];
  2015. }];
  2016. [self.resourceModel.continousArray enumerateObjectsUsingBlock:^(LenzResourceItemModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  2017. idIndex += idx;
  2018. NSMutableDictionary *dict = [NSMutableDictionary dictionary];
  2019. dict[@"id"] = [NSString stringWithFormat:@"%ld", idIndex];
  2020. dict[@"path"] = obj.path;
  2021. dict[@"cameraMode"] = obj.cameraMode;
  2022. dict[@"cameraType"] = obj.cameraType;
  2023. dict[@"isRemake"] = @(obj.isRemake);
  2024. [res addObject:dict];
  2025. }];
  2026. [self.resourceModel.movieArray enumerateObjectsUsingBlock:^(LenzResourceItemModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  2027. idIndex += idx;
  2028. NSMutableDictionary *dict = [NSMutableDictionary dictionary];
  2029. dict[@"id"] = [NSString stringWithFormat:@"%ld", idIndex];
  2030. dict[@"path"] = obj.path;
  2031. dict[@"cameraMode"] = obj.cameraMode;
  2032. dict[@"cameraType"] = obj.cameraType;
  2033. dict[@"isRemake"] = @(obj.isRemake);
  2034. [res addObject:dict];
  2035. }];
  2036. [self.resourceModel.panoramArray enumerateObjectsUsingBlock:^(LenzResourceItemModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  2037. if (obj.path.length) {
  2038. idIndex += idx;
  2039. NSMutableDictionary *dict = [NSMutableDictionary dictionary];
  2040. dict[@"id"] = [NSString stringWithFormat:@"%ld", idIndex];
  2041. dict[@"mosaicPictureUrl"] = obj.path;
  2042. dict[@"cameraMode"] = obj.cameraMode;
  2043. dict[@"cameraType"] = obj.cameraType;
  2044. dict[@"isRemake"] = @(obj.isRemake);
  2045. NSMutableArray *keyFrame = [NSMutableArray array];
  2046. [obj.keyFrame enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  2047. [keyFrame addObject:obj];
  2048. }];
  2049. dict[@"keyframe"] = keyFrame;
  2050. [res addObject:dict];
  2051. }
  2052. }];
  2053. [self.resourceModel.aiPanoramArray enumerateObjectsUsingBlock:^(LenzResourceItemModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  2054. if (obj.path.length) {
  2055. idIndex += idx;
  2056. NSMutableDictionary *dict = [NSMutableDictionary dictionary];
  2057. dict[@"id"] = [NSString stringWithFormat:@"%ld", idIndex];
  2058. dict[@"cameraMode"] = obj.cameraMode;
  2059. dict[@"cameraType"] = obj.cameraType;
  2060. dict[@"isRemake"] = @(obj.isRemake);
  2061. if (obj.isStitch) {
  2062. NSMutableArray *keyFrame = [NSMutableArray array];
  2063. [obj.keyFrame enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  2064. [keyFrame addObject:obj];
  2065. }];
  2066. dict[@"keyframe"] = keyFrame;
  2067. dict[@"mosaicPictureUrl"] = obj.path;
  2068. } else {
  2069. dict[@"path"] = obj.path;
  2070. }
  2071. [res addObject:dict];
  2072. }
  2073. }];
  2074. if (self.completeBlock) {
  2075. NSMutableDictionary *dict = [NSMutableDictionary dictionary];
  2076. dict[@"code"] = @(200);
  2077. dict[@"message"] = @"成功";
  2078. dict[@"data"] = res;
  2079. [self stopCaptureSession];
  2080. [[PCSMotionManager shared] stopAccelerometerUpdates];
  2081. self.completeBlock(dict);
  2082. [self dismissViewControllerAnimated:YES completion:^{
  2083. }];
  2084. }
  2085. }
  2086. - (void)discardPhotoResultButtonTouchUpInside:(id)sender{
  2087. self.panGesture.enabled = YES;
  2088. NSString *title = @"返回会清空已拍摄数据,请确认!";
  2089. [QuitMultipleModeAlertViewController show:self title:@"确认提醒" text:title leftBtnTitle:@"取消" rightBtnTitle:@"确定" withLeftButtonCallBack:^(QuitMultipleModeAlertViewController * _Nonnull alertController) {
  2090. [alertController dismissViewControllerAnimated:NO completion:^{}];
  2091. } rightButtonCallBack:^(QuitMultipleModeAlertViewController * _Nonnull alertController) {
  2092. [self.resourceModel.continousArray removeAllObjects];
  2093. [self.resourceModel.movieArray removeAllObjects];
  2094. [self.resourceModel.panoramArray removeAllObjects];
  2095. [self.resourceModel.aiPanoramArray removeAllObjects];
  2096. [alertController dismissViewControllerAnimated:NO completion:^{
  2097. [self stopCaptureSession];
  2098. [[PCSMotionManager shared] stopAccelerometerUpdates];
  2099. [self dismissViewControllerAnimated:YES completion:^{}];
  2100. }];
  2101. }];
  2102. }
  2103. - (void)albumButtonTouchUpInside:(id)sender{
  2104. [self performSegueWithIdentifier:@"show-photoalbum" sender:nil];
  2105. }
  2106. - (__kindof id<OperationNodeProtocol>)currentOperationNode{
  2107. return self.operationNodes[@(self.mode)];
  2108. }
  2109. - (void)updateNumberLabel {
  2110. self.aiPanPromtLabel.hidden = YES;
  2111. switch (self.mode) {
  2112. case SDK_CAPTURE_MODE_CONTINUOUS: {
  2113. if (self.resourceModel.continousArray.count > 0) {
  2114. self.numberOfImagesLabel.hidden = NO;
  2115. self.numberOfImagesLabel.text = [NSString stringWithFormat:@"已连拍%ld张", self.resourceModel.continousArray.count];
  2116. } else {
  2117. self.numberOfImagesLabel.hidden = YES;
  2118. }
  2119. }
  2120. break;
  2121. case SDK_CAPTURE_MODE_MOVIE:{
  2122. if (self.resourceModel.movieArray.count > 0) {
  2123. self.numberOfImagesLabel.hidden = NO;
  2124. self.numberOfImagesLabel.text = [NSString stringWithFormat:@"已拍视频%ld条", self.resourceModel.movieArray.count];
  2125. } else {
  2126. self.numberOfImagesLabel.hidden = YES;
  2127. }
  2128. }
  2129. break;
  2130. case SDK_CAPTURE_MODE_PANORAMA:
  2131. {
  2132. if (self.resourceModel.panoramArray.count > 0) {
  2133. self.numberOfImagesLabel.hidden = NO;
  2134. self.numberOfImagesLabel.text = [NSString stringWithFormat:@"已拼接%ld张", self.resourceModel.panoramArray.count];
  2135. } else {
  2136. self.numberOfImagesLabel.hidden = YES;
  2137. }
  2138. }
  2139. break;
  2140. case SDK_CAPTURE_MODE_INTELLEGENCE_PANORAMA:
  2141. {
  2142. self.aiPanPromtLabel.hidden = NO;
  2143. if (self.resourceModel.aiPanoramArray.count > 0) {
  2144. self.numberOfImagesLabel.hidden = NO;
  2145. self.numberOfImagesLabel.text = [NSString stringWithFormat:@"已拍摄%ld张", self.resourceModel.aiPanoramArray.count];
  2146. } else {
  2147. self.numberOfImagesLabel.text = @"轻点拍照,长按录制";
  2148. self.numberOfImagesLabel.hidden = NO;
  2149. }
  2150. }
  2151. break;
  2152. default:
  2153. self.numberOfImagesLabel.hidden = YES;
  2154. break;
  2155. }
  2156. }
  2157. - (void)savePhotoWith:(NSData *)data isRemake:(BOOL)isRemake{
  2158. UIImage *image = [[UIImage alloc] initWithData:data];
  2159. NSString *path = [LenzDataManager saveImageWith:data mode:self.mode];
  2160. LenzResourceItemModel *itemModel = [[LenzResourceItemModel alloc]init];
  2161. itemModel.path = path;
  2162. [[SDKParameters shared].items enumerateObjectsUsingBlock:^(SDKParametersItem * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  2163. if (obj.model == self.mode) {
  2164. itemModel.cameraType = obj.cameraPositionString;
  2165. }
  2166. }];
  2167. itemModel.mode = self.mode;
  2168. itemModel.image = image;
  2169. itemModel.isRemake = isRemake;
  2170. switch (self.mode) {
  2171. case SDK_CAPTURE_MODE_CONTINUOUS:{
  2172. [self.resourceModel.continousArray addObject:itemModel];
  2173. }
  2174. break;
  2175. case SDK_CAPTURE_MODE_SINGLE: {
  2176. [self.resourceModel.singleArray addObject:itemModel];
  2177. }
  2178. break;
  2179. case SDK_CAPTURE_MODE_INTELLEGENCE_PANORAMA: {
  2180. [self.resourceModel.aiPanoramArray addObject:itemModel];
  2181. }
  2182. break;
  2183. default:
  2184. break;
  2185. }
  2186. self.resourceModel.lastIndex = self.mode;
  2187. [self updateViewStatus];
  2188. [self.currentOperationNode triggered];
  2189. if(self.mode == SDK_CAPTURE_MODE_SINGLE){
  2190. [self stopCaptureSession];
  2191. [self performSegueWithIdentifier:@"preview_single" sender:nil];
  2192. }
  2193. else if(self.mode == SDK_CAPTURE_MODE_CONTINUOUS){
  2194. self.discardPhotoButton.hidden = NO;
  2195. self.savePhotoButton.hidden = NO;
  2196. }
  2197. self.resultImageView.image = image;
  2198. }
  2199. #pragma mark 相机单拍,和连拍的代理
  2200. - (void)captureOutput:(AVCapturePhotoOutput *)output didFinishProcessingPhoto:(AVCapturePhoto *)photo error:(NSError *)error{
  2201. self.panGesture.enabled = YES;
  2202. self.isDuringTakePhoto = NO;
  2203. NSData* imageData = [photo fileDataRepresentation];
  2204. if (imageData == nil){
  2205. return;
  2206. }
  2207. UIImage *image = [[UIImage alloc] initWithData:imageData];
  2208. BOOL isRemake = [self.tensorFlow remake:image];
  2209. __block BOOL needCheckRemake = NO;
  2210. [[SDKParameters shared].items enumerateObjectsUsingBlock:^(SDKParametersItem * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  2211. if (obj.model == self.mode) {
  2212. needCheckRemake = obj.isRemake;
  2213. }
  2214. }];
  2215. if (isRemake && needCheckRemake) {
  2216. [QuitMultipleModeAlertViewController show:self title:@"确认提醒" text:@"检测到该图片为翻拍,是否继续使用?" leftBtnTitle:@"取消" rightBtnTitle:@"确定" withLeftButtonCallBack:^(QuitMultipleModeAlertViewController * _Nonnull alertController) {
  2217. [alertController dismissViewControllerAnimated:YES completion:nil];
  2218. } rightButtonCallBack:^(QuitMultipleModeAlertViewController * _Nonnull alertController) {
  2219. [alertController dismissViewControllerAnimated:YES completion:nil];
  2220. [self savePhotoWith:imageData isRemake:isRemake];
  2221. }];
  2222. } else {
  2223. [self savePhotoWith:imageData isRemake:isRemake];
  2224. }
  2225. }
  2226. - (void)updateViewStatus {
  2227. dispatch_async(dispatch_get_main_queue(), ^{
  2228. UIImage *image = nil;
  2229. NSMutableArray *continousMutArray = [NSMutableArray array];
  2230. [self.resourceModel.continousArray enumerateObjectsUsingBlock:^(LenzResourceItemModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  2231. if (obj.path && obj.image) {
  2232. [continousMutArray addObject:obj];
  2233. }
  2234. }];
  2235. self.resourceModel.continousArray = continousMutArray;
  2236. NSMutableArray *singleMutArray = [NSMutableArray array];
  2237. [self.resourceModel.singleArray enumerateObjectsUsingBlock:^(LenzResourceItemModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  2238. if (obj.path && obj.image) {
  2239. [singleMutArray addObject:obj];
  2240. }
  2241. }];
  2242. self.resourceModel.singleArray = singleMutArray;
  2243. NSMutableArray *movieMutArray = [NSMutableArray array];
  2244. [self.resourceModel.movieArray enumerateObjectsUsingBlock:^(LenzResourceItemModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  2245. if (obj.path && obj.image) {
  2246. [movieMutArray addObject:obj];
  2247. }
  2248. }];
  2249. self.resourceModel.movieArray = movieMutArray;
  2250. NSMutableArray *panoramMutArray = [NSMutableArray array];
  2251. [self.resourceModel.panoramArray enumerateObjectsUsingBlock:^(LenzResourceItemModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  2252. if (obj.path && obj.image) {
  2253. [panoramMutArray addObject:obj];
  2254. }
  2255. }];
  2256. self.resourceModel.panoramArray = panoramMutArray;
  2257. NSMutableArray *aiPanoramMutArray = [NSMutableArray array];
  2258. [self.resourceModel.aiPanoramArray enumerateObjectsUsingBlock:^(LenzResourceItemModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  2259. if (obj.path && obj.image) {
  2260. [aiPanoramMutArray addObject:obj];
  2261. }
  2262. }];
  2263. self.resourceModel.aiPanoramArray = aiPanoramMutArray;
  2264. switch (self.resourceModel.lastIndex) {
  2265. case SDK_CAPTURE_MODE_CONTINUOUS: {
  2266. if (self.resourceModel.continousArray.count > 0) {
  2267. LenzResourceItemModel *model = self.resourceModel.continousArray.lastObject;
  2268. image = model.image;
  2269. } else {
  2270. if (self.resourceModel.singleArray.count > 0) {
  2271. LenzResourceItemModel *model = self.resourceModel.singleArray.lastObject;
  2272. image = model.image;
  2273. } else if (self.resourceModel.movieArray.count > 0) {
  2274. LenzResourceItemModel *model = self.resourceModel.movieArray.lastObject;
  2275. image = model.image;
  2276. } else if (self.resourceModel.panoramArray.count > 0) {
  2277. LenzResourceItemModel *model = self.resourceModel.panoramArray.lastObject;
  2278. image = model.image;
  2279. } else if (self.resourceModel.aiPanoramArray.count > 0) {
  2280. LenzResourceItemModel *model = self.resourceModel.aiPanoramArray.lastObject;
  2281. image = model.image;
  2282. }
  2283. }
  2284. }
  2285. break;
  2286. case SDK_CAPTURE_MODE_SINGLE:{
  2287. if (self.resourceModel.singleArray.count > 0) {
  2288. LenzResourceItemModel *model = self.resourceModel.singleArray.lastObject;
  2289. image = model.image;
  2290. } else {
  2291. if (self.resourceModel.continousArray.count > 0) {
  2292. LenzResourceItemModel *model = self.resourceModel.continousArray.lastObject;
  2293. image = model.image;
  2294. } else if (self.resourceModel.movieArray.count > 0) {
  2295. LenzResourceItemModel *model = self.resourceModel.movieArray.lastObject;
  2296. image = model.image;
  2297. } else if (self.resourceModel.panoramArray.count > 0) {
  2298. LenzResourceItemModel *model = self.resourceModel.panoramArray.lastObject;
  2299. image = model.image;
  2300. } else if (self.resourceModel.aiPanoramArray.count > 0) {
  2301. LenzResourceItemModel *model = self.resourceModel.aiPanoramArray.lastObject;
  2302. image = model.image;
  2303. }
  2304. }
  2305. }
  2306. break;
  2307. case SDK_CAPTURE_MODE_MOVIE:{
  2308. if (self.resourceModel.movieArray.count > 0) {
  2309. LenzResourceItemModel *model = self.resourceModel.movieArray.lastObject;
  2310. image = model.image;
  2311. } else {
  2312. if (self.resourceModel.singleArray.count > 0) {
  2313. LenzResourceItemModel *model = self.resourceModel.singleArray.lastObject;
  2314. image = model.image;
  2315. } else if (self.resourceModel.continousArray.count > 0) {
  2316. LenzResourceItemModel *model = self.resourceModel.continousArray.lastObject;
  2317. image = model.image;
  2318. } else if (self.resourceModel.panoramArray.count > 0) {
  2319. LenzResourceItemModel *model = self.resourceModel.panoramArray.lastObject;
  2320. image = model.image;
  2321. } else if (self.resourceModel.aiPanoramArray.count > 0) {
  2322. LenzResourceItemModel *model = self.resourceModel.aiPanoramArray.lastObject;
  2323. image = model.image;
  2324. }
  2325. }
  2326. }
  2327. break;
  2328. case SDK_CAPTURE_MODE_PANORAMA:{
  2329. if (self.resourceModel.panoramArray.count > 0) {
  2330. LenzResourceItemModel *model = self.resourceModel.panoramArray.lastObject;
  2331. image = model.image;
  2332. } else {
  2333. if (self.resourceModel.singleArray.count > 0) {
  2334. LenzResourceItemModel *model = self.resourceModel.singleArray.lastObject;
  2335. image = model.image;
  2336. } else if (self.resourceModel.movieArray.count > 0) {
  2337. LenzResourceItemModel *model = self.resourceModel.movieArray.lastObject;
  2338. image = model.image;
  2339. } else if (self.resourceModel.continousArray.count > 0) {
  2340. LenzResourceItemModel *model = self.resourceModel.continousArray.lastObject;
  2341. image = model.image;
  2342. } else if (self.resourceModel.aiPanoramArray.count > 0) {
  2343. LenzResourceItemModel *model = self.resourceModel.aiPanoramArray.lastObject;
  2344. image = model.image;
  2345. }
  2346. }
  2347. }
  2348. break;
  2349. case SDK_CAPTURE_MODE_INTELLEGENCE_PANORAMA:{
  2350. if (self.resourceModel.aiPanoramArray.count > 0) {
  2351. LenzResourceItemModel *model = self.resourceModel.aiPanoramArray.lastObject;
  2352. image = model.image;
  2353. } else {
  2354. if (self.resourceModel.singleArray.count > 0) {
  2355. LenzResourceItemModel *model = self.resourceModel.singleArray.lastObject;
  2356. image = model.image;
  2357. } else if (self.resourceModel.movieArray.count > 0) {
  2358. LenzResourceItemModel *model = self.resourceModel.movieArray.lastObject;
  2359. image = model.image;
  2360. } else if (self.resourceModel.panoramArray.count > 0) {
  2361. LenzResourceItemModel *model = self.resourceModel.panoramArray.lastObject;
  2362. image = model.image;
  2363. } else if (self.resourceModel.continousArray.count > 0) {
  2364. LenzResourceItemModel *model = self.resourceModel.continousArray.lastObject;
  2365. image = model.image;
  2366. }
  2367. }
  2368. }
  2369. break;
  2370. default:
  2371. break;
  2372. }
  2373. if (image) {
  2374. self.ablumImageView.image = image;
  2375. self.ablumImageView.hidden = NO;
  2376. self.discardPhotoButton.hidden = NO;
  2377. self.savePhotoButton.hidden = NO;
  2378. self.backButton.hidden = YES;
  2379. } else {
  2380. self.ablumImageView.image = nil;
  2381. self.ablumImageView.hidden = YES;
  2382. self.discardPhotoButton.hidden = YES;
  2383. self.savePhotoButton.hidden = YES;
  2384. self.backButton.hidden = NO;
  2385. }
  2386. [self updateNumberLabel];
  2387. });
  2388. }
  2389. #pragma mark - AVCaptureFileOutputRecordingDelegate
  2390. - (void)captureOutput:(AVCaptureFileOutput *)output didFinishRecordingToOutputFileAtURL:(NSURL *)outputFileURL fromConnections:(NSArray<AVCaptureConnection *> *)connections error:(NSError *)error{
  2391. //视频录制
  2392. [self.timerLabel updateWith:0];
  2393. BOOL recordedSuccessfully = YES;
  2394. if(error.code == AVErrorMaximumDurationReached){
  2395. } else if (error.code == AVErrorDiskFull){
  2396. dispatch_async(dispatch_get_main_queue(), ^{
  2397. [[NSNotificationCenter defaultCenter] postNotificationName:LenzSDKNotificationDiskIsFull object:error];
  2398. });
  2399. }else if (error.code == AVErrorSessionWasInterrupted){
  2400. }
  2401. id value = [[error userInfo] objectForKey:AVErrorRecordingSuccessfullyFinishedKey];
  2402. if (value) {
  2403. recordedSuccessfully = [value boolValue];
  2404. [self.recordTimer invalidate];
  2405. self.recordTimer = nil;
  2406. }
  2407. if(!recordedSuccessfully){
  2408. return;
  2409. }
  2410. OperationNodeMovie *node = nil;
  2411. if ([self.currentOperationNode isKindOfClass:[OperationNodeMovie class]]) {
  2412. node = (OperationNodeMovie *)self.currentOperationNode;
  2413. if (node.isRecording) {
  2414. [self.currentOperationNode takePhotoButtonTouched];
  2415. }
  2416. }
  2417. LenzResourceItemModel *itemModel = [[LenzResourceItemModel alloc]init];
  2418. itemModel.path = outputFileURL.absoluteString;
  2419. [[SDKParameters shared].items enumerateObjectsUsingBlock:^(SDKParametersItem * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  2420. if (obj.model == self.mode) {
  2421. itemModel.cameraType = obj.cameraPositionString;
  2422. }
  2423. }];
  2424. itemModel.mode = self.mode;
  2425. itemModel.image = [self videoFrameImageWith:outputFileURL];
  2426. [self.resourceModel.movieArray addObject:itemModel];
  2427. self.resourceModel.lastIndex = self.mode;
  2428. [self updateViewStatus];
  2429. dispatch_async(dispatch_get_main_queue(), ^{
  2430. [self.currentOperationNode triggered];
  2431. self.thumbnailOritaionByCurrentVideoOrientation = UIImageOrientationUp;
  2432. });
  2433. }
  2434. - (BOOL)encodeFrame:(CMSampleBufferRef) sampleBuffer{
  2435. if (CMSampleBufferDataIsReady(sampleBuffer)) {
  2436. if (self.videoWriter.status == AVAssetWriterStatusUnknown) {
  2437. CMTime startTime = CMSampleBufferGetPresentationTimeStamp(sampleBuffer);
  2438. [self.videoWriter startWriting];
  2439. [self.videoWriter startSessionAtSourceTime:startTime];
  2440. }
  2441. if (self.videoWriter.status == AVAssetWriterStatusFailed) {
  2442. return NO;
  2443. }
  2444. if (self.videoWriterInput.readyForMoreMediaData) {
  2445. [self.videoWriterInput appendSampleBuffer:sampleBuffer];
  2446. return YES;
  2447. }
  2448. }
  2449. return NO;
  2450. }
  2451. #pragma mark - AVCaptureAudioDataOutputSampleBufferDelegate
  2452. #pragma mark - UIViewControllerTransitioningDelegate
  2453. - (id<UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source{
  2454. return [[CustomModalViewTransitionPresent alloc] init];
  2455. }
  2456. - (id<UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed{
  2457. return [[CustomModalViewTransitionDismiss alloc] init];
  2458. }
  2459. #pragma mark - PhotoListViewControllerDelegate
  2460. //删除视频
  2461. - (void)deleteMovie: (MovieListCellModel*)model completion: (DeleteImageCompletionCallBackType)completion{
  2462. for (int i = 0; i < self.totalArray.count; i ++) {
  2463. UIImage *image = self.totalArray[i];
  2464. if (image == (UIImage *)model.movieModel.thumbnail) {
  2465. [self.totalArray removeObjectAtIndex:i];
  2466. [self.typeArray removeObjectAtIndex:i];
  2467. break;;
  2468. }
  2469. }
  2470. id<OperationNodeProtocol> curr = (__kindof id<OperationNodeProtocol>)self.operationNodes[@(2)];
  2471. if([curr respondsToSelector:@selector(deleteCacheForKey:completion:)]){
  2472. [curr deleteCacheForKey:model.movieModel.fileUrl.path completion:completion];
  2473. }
  2474. }
  2475. //连拍删除
  2476. - (void)deleteImage:(UIImage *)image completion:(nonnull DeleteImageCompletionCallBackType)completion{
  2477. for (int i = 0; i < self.totalArray.count; i ++) {
  2478. UIImage *img = self.totalArray[i];
  2479. if (img == image) {
  2480. [self.totalArray removeObjectAtIndex:i];
  2481. [self.typeArray removeObjectAtIndex:i];
  2482. break;;
  2483. }
  2484. }
  2485. id<OperationNodeProtocol> curr = (__kindof id<OperationNodeProtocol>)self.operationNodes[@(0)];
  2486. if([curr respondsToSelector:@selector(deleteCacheForImage:completion:)]){
  2487. [curr deleteCacheForImage:image completion:completion];
  2488. }
  2489. }
  2490. - (void)deleteImageWithName:(NSString *)name completion:(nonnull DeleteImageCompletionCallBackType)completion{
  2491. id<OperationNodeProtocol> curr = self.currentOperationNode;
  2492. if([curr respondsToSelector:@selector(deleteCacheForKey:completion:)]){
  2493. [curr deleteCacheForKey:name completion:completion];
  2494. }
  2495. }
  2496. #pragma mark - AVCaptureVideoDataOutputSampleBufferDelegate
  2497. - (void)captureOutput:(AVCaptureOutput *)output didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection {
  2498. @autoreleasepool {
  2499. if (connection == [self.videoDataOutput connectionWithMediaType:AVMediaTypeVideo]) {
  2500. [self.stitchkLock lock];
  2501. OperationNodeVideoBase *base = nil;
  2502. if ([self.currentOperationNode isKindOfClass:[OperationNodeVideoBase class]]) {
  2503. base = (OperationNodeVideoBase *)self.currentOperationNode;
  2504. }
  2505. if (self.isVideoStitchStopedWhenRecording &&
  2506. (self.stitchResult != LenVideoStitchResultFail && self.stitchResult != LenVideoStitchResultStitchFail)) {
  2507. [self updateViewStatusWhenStitchSave:YES];
  2508. self.isStitchBigImage = YES;
  2509. }
  2510. [LenStitcher videoStitchWith:sampleBuffer
  2511. dirction:(LenVideoStitchDirction)self.panoramOrientationGuideView.selectedItemIndex
  2512. isStop:self.isVideoStitchStopedWhenRecording
  2513. complete:^(LenVideoStitchResult result, LenVideoStitchNeedSave needSave) {
  2514. self.stitchResult = result;
  2515. self.isVideoStitchStopedWhenRecording = NO;
  2516. UIImage *image = [LenStitcher getCurrentPanoramaShowImage];
  2517. LenzResourceItemModel *model = nil;
  2518. if (self.mode == SDK_CAPTURE_MODE_PANORAMA) {
  2519. model = self.resourceModel.panoramArray.lastObject;
  2520. } else if (self.mode == SDK_CAPTURE_MODE_INTELLEGENCE_PANORAMA) {
  2521. model = self.resourceModel.aiPanoramArray.lastObject;
  2522. }
  2523. if (!image || !model) {
  2524. [self.stitchkLock unlock];
  2525. return;
  2526. }
  2527. if (result == LenVideoStitchResultSuccess) {
  2528. dispatch_async(dispatch_get_main_queue(), ^{
  2529. if (self.isStitchBigImage) {
  2530. //保存大图拼接路径
  2531. [[SDKParameters shared].items enumerateObjectsUsingBlock:^(SDKParametersItem * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  2532. if (obj.model == self.mode) {
  2533. model.cameraType = obj.cameraPositionString;
  2534. }
  2535. }];
  2536. model.mode = self.mode;
  2537. if ([LenStitcher getCurrentPanoramaShow]) {
  2538. model.image = image;
  2539. NSString *path = [LenzDataManager saveImageWith:UIImageJPEGRepresentation(image, .8) mode:self.mode];
  2540. model.path = path;
  2541. }
  2542. [self updateViewStatusWhenStitchSave:NO];
  2543. self.isStitchBigImage = NO;
  2544. [self.panoramaGuideView clearImage];
  2545. [self updateViewStatus];
  2546. [self.panoramaGuideView showImageWith:nil];
  2547. } else {
  2548. if (base.isRecording) {
  2549. __block BOOL keyFrame = NO;
  2550. [[SDKParameters shared].items enumerateObjectsUsingBlock:^(SDKParametersItem * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  2551. if (obj.model == self.mode) {
  2552. keyFrame = [obj.keyFrame boolValue];
  2553. }
  2554. }];
  2555. if (needSave && keyFrame) {
  2556. NSString *path = [LenzDataManager saveImageWith:UIImageJPEGRepresentation(image, .6) mode:self.mode];
  2557. if (path) {
  2558. [model.keyFrame addObject:path];
  2559. }
  2560. }
  2561. if (self.panoramOrientationGuideView.selectedItemIndex != PanoramaOrientationViewAny) {
  2562. [self.panoramaGuideView showImageWith:image];
  2563. } else {
  2564. self.ablumImageView.hidden = NO;
  2565. self.ablumImageView.image = image;
  2566. }
  2567. }
  2568. }
  2569. self.resourceModel.lastIndex = self.mode;
  2570. });
  2571. } else {
  2572. if (!base.isRecording) {
  2573. //停止拼接,移除保存中 非success数据
  2574. if (self.mode == SDK_CAPTURE_MODE_PANORAMA) {
  2575. NSMutableArray *array = [NSMutableArray array];
  2576. [self.resourceModel.panoramArray enumerateObjectsUsingBlock:^(LenzResourceItemModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  2577. if (obj.path.length) {
  2578. [array addObject:obj];
  2579. }
  2580. }];
  2581. self.resourceModel.panoramArray = array;
  2582. } else if (self.mode == SDK_CAPTURE_MODE_INTELLEGENCE_PANORAMA) {
  2583. NSMutableArray *array = [NSMutableArray array];
  2584. [self.resourceModel.aiPanoramArray enumerateObjectsUsingBlock:^(LenzResourceItemModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  2585. if (obj.path.length) {
  2586. [array addObject:obj];
  2587. }
  2588. }];
  2589. self.resourceModel.aiPanoramArray = array;
  2590. }
  2591. [self updateViewStatusWhenStitchSave:NO];
  2592. [self updateViewStatus];
  2593. [self.stitchkLock unlock];
  2594. return;
  2595. }
  2596. if (result == LenVideoStitchResultFail || result == LenVideoStitchResultStitchFail) {
  2597. if (result == LenVideoStitchResultFail) {
  2598. [SVProgressHUD showImage:[UIImage imageNamed:@""] status:@"拼接失败,流程结束"];
  2599. } else {
  2600. [SVProgressHUD showImage:[UIImage imageNamed:@""] status:@"拼接大图失败,流程结束"];
  2601. }
  2602. self.isVideoStitchStopedWhenRecording = NO;
  2603. dispatch_async(dispatch_get_main_queue(), ^{
  2604. [base takePhotoButtonTouched];
  2605. [self stopMotion];
  2606. [self removeLastStich];
  2607. [self updateViewStatusWhenStitchFail];
  2608. [self.panoramaGuideView clearImage];
  2609. [self updateViewStatus];
  2610. [self.panoramaGuideView showImageWith:nil];
  2611. });
  2612. [SVProgressHUD dismissWithDelay:3 completion:^{
  2613. }];
  2614. } else if(result == LenVideoStitchResultSlowMore) {
  2615. [SVProgressHUD showImage:[UIImage imageNamed:@""] status:@"目标物较少,请对准目标物低速且匀速拍摄"];
  2616. } else if (result == LenVideoStitchResultSlow) {
  2617. [SVProgressHUD showImage:[UIImage imageNamed:@""] status:@"目标物较少,请对准目标物低速且匀速拍摄"];
  2618. } else {
  2619. // [SVProgressHUD showImage:[UIImage imageNamed:@""] status:@"请勿往反方向移动"];
  2620. }
  2621. }
  2622. [self.stitchkLock unlock];
  2623. }];
  2624. }
  2625. }
  2626. }
  2627. - (void)removeLastStich {
  2628. if (self.mode == SDK_CAPTURE_MODE_PANORAMA) {
  2629. if (self.resourceModel.panoramArray.count > 0) {
  2630. [self.resourceModel.panoramArray removeLastObject];
  2631. }
  2632. } else if (self.mode == SDK_CAPTURE_MODE_INTELLEGENCE_PANORAMA) {
  2633. if (self.resourceModel.aiPanoramArray.count > 0) {
  2634. [self.resourceModel.aiPanoramArray removeLastObject];
  2635. }
  2636. }
  2637. }
  2638. #pragma mark - Navigation
  2639. // In a storyboard-based application, you will often want to do a little preparation before navigation
  2640. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  2641. // Get the new view controller using [segue destinationViewController].
  2642. if([segue.identifier isEqualToString:@"preview_single"]){
  2643. PreviewSingleViewController* dst = segue.destinationViewController;
  2644. dst.singleModeNode = self.currentOperationNode;
  2645. if (self.resourceModel.singleArray.count > 0) {
  2646. LenzResourceItemModel *model = self.resourceModel.singleArray.firstObject;
  2647. dst.image = model.image;
  2648. }
  2649. }
  2650. }
  2651. - (void)clearSingleData {
  2652. [self.resourceModel.singleArray removeAllObjects];
  2653. [self updateViewStatus];
  2654. }
  2655. - (UIImage *)videoFrameImageWith:(NSURL *)url {
  2656. AVURLAsset* asset = [[AVURLAsset alloc] initWithURL:url options:nil];
  2657. AVAssetImageGenerator *generateImg = [[AVAssetImageGenerator alloc] initWithAsset:asset];
  2658. NSError *err = NULL;
  2659. CMTime time = CMTimeMake(1, 65);
  2660. CGImageRef refImg = [generateImg copyCGImageAtTime:time actualTime:NULL error:&err];
  2661. UIImage *frameImage= [[UIImage alloc] initWithCGImage:refImg scale:1.0 orientation:self.thumbnailOritaionByCurrentVideoOrientation];
  2662. return frameImage;
  2663. }
  2664. @end