PCSBaseViewController.m 135 KB

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