PCSBaseViewController.m 125 KB

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