您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  1. /*
  2. * Copyright 2012 ZXing authors
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import <ImageIO/ImageIO.h>
  17. #import "ZXBinaryBitmap.h"
  18. #import "ZXCapture.h"
  19. #import "ZXCaptureDelegate.h"
  20. #import "ZXCGImageLuminanceSource.h"
  21. #import "ZXDecodeHints.h"
  22. #import "ZXHybridBinarizer.h"
  23. #import "ZXReader.h"
  24. #import "ZXResult.h"
  25. @interface ZXCapture ()
  26. @property (nonatomic, strong) CALayer *binaryLayer;
  27. @property (nonatomic, assign) BOOL cameraIsReady;
  28. @property (nonatomic, assign) int captureDeviceIndex;
  29. @property (nonatomic, strong) dispatch_queue_t captureQueue;
  30. @property (nonatomic, assign) BOOL hardStop;
  31. @property (nonatomic, strong) AVCaptureDeviceInput *input;
  32. @property (nonatomic, strong) AVCaptureVideoPreviewLayer *layer;
  33. @property (nonatomic, strong) CALayer *luminanceLayer;
  34. @property (nonatomic, assign) int orderInSkip;
  35. @property (nonatomic, assign) int orderOutSkip;
  36. @property (nonatomic, assign) BOOL onScreen;
  37. @property (nonatomic, strong) AVCaptureVideoDataOutput *output;
  38. @property (nonatomic, assign) BOOL running;
  39. @property (nonatomic, strong) AVCaptureSession *session;
  40. @end
  41. @implementation ZXCapture
  42. - (ZXCapture *)init {
  43. if (self = [super init]) {
  44. _captureDeviceIndex = -1;
  45. _captureQueue = dispatch_queue_create("com.zxing.captureQueue", NULL);
  46. _focusMode = AVCaptureFocusModeContinuousAutoFocus;
  47. _hardStop = NO;
  48. _hints = [ZXDecodeHints hints];
  49. _lastScannedImage = NULL;
  50. _onScreen = NO;
  51. _orderInSkip = 0;
  52. _orderOutSkip = 0;
  53. if (NSClassFromString(@"ZXMultiFormatReader")) {
  54. _reader = [NSClassFromString(@"ZXMultiFormatReader") performSelector:@selector(reader)];
  55. }
  56. _rotation = 0.0f;
  57. _running = NO;
  58. _transform = CGAffineTransformIdentity;
  59. _scanRect = CGRectZero;
  60. }
  61. return self;
  62. }
  63. - (void)dealloc {
  64. if (_lastScannedImage) {
  65. CGImageRelease(_lastScannedImage);
  66. }
  67. if (_session && _session.inputs) {
  68. for (AVCaptureInput *input in _session.inputs) {
  69. [_session removeInput:input];
  70. }
  71. }
  72. if (_session && _session.outputs) {
  73. for (AVCaptureOutput *output in _session.outputs) {
  74. [_session removeOutput:output];
  75. }
  76. }
  77. }
  78. #pragma mark - Property Getters
  79. - (CALayer *)layer {
  80. AVCaptureVideoPreviewLayer *layer = (AVCaptureVideoPreviewLayer *)_layer;
  81. if (!_layer) {
  82. layer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:self.session];
  83. layer.affineTransform = self.transform;
  84. layer.delegate = self;
  85. layer.videoGravity = AVLayerVideoGravityResizeAspect;
  86. layer.videoGravity = AVLayerVideoGravityResizeAspectFill;
  87. _layer = layer;
  88. }
  89. return layer;
  90. }
  91. - (AVCaptureVideoDataOutput *)output {
  92. if (!_output) {
  93. _output = [[AVCaptureVideoDataOutput alloc] init];
  94. [_output setVideoSettings:@{
  95. (NSString *)kCVPixelBufferPixelFormatTypeKey : [NSNumber numberWithUnsignedInt:kCVPixelFormatType_32BGRA]
  96. }];
  97. [_output setAlwaysDiscardsLateVideoFrames:YES];
  98. [_output setSampleBufferDelegate:self queue:_captureQueue];
  99. [self.session addOutput:_output];
  100. }
  101. return _output;
  102. }
  103. #pragma mark - Property Setters
  104. - (void)setCamera:(int)camera {
  105. if (_camera != camera) {
  106. _camera = camera;
  107. self.captureDeviceIndex = -1;
  108. self.captureDevice = nil;
  109. [self replaceInput];
  110. }
  111. }
  112. - (void)setDelegate:(id<ZXCaptureDelegate>)delegate {
  113. _delegate = delegate;
  114. if (delegate) {
  115. self.hardStop = NO;
  116. }
  117. [self startStop];
  118. }
  119. - (void)setFocusMode:(AVCaptureFocusMode)focusMode {
  120. if ([self.input.device isFocusModeSupported:focusMode] && self.input.device.focusMode != focusMode) {
  121. _focusMode = focusMode;
  122. [self.input.device lockForConfiguration:nil];
  123. self.input.device.focusMode = focusMode;
  124. [self.input.device unlockForConfiguration];
  125. }
  126. }
  127. - (void)setLastScannedImage:(CGImageRef)lastScannedImage {
  128. if (_lastScannedImage) {
  129. CGImageRelease(_lastScannedImage);
  130. }
  131. if (lastScannedImage) {
  132. CGImageRetain(lastScannedImage);
  133. }
  134. _lastScannedImage = lastScannedImage;
  135. }
  136. - (void)setMirror:(BOOL)mirror {
  137. if (_mirror != mirror) {
  138. _mirror = mirror;
  139. if (self.layer) {
  140. CGAffineTransform transform = self.transform;
  141. transform.a = - transform.a;
  142. self.transform = transform;
  143. [self.layer setAffineTransform:self.transform];
  144. }
  145. }
  146. }
  147. - (void)setTorch:(BOOL)torch {
  148. _torch = torch;
  149. [self.input.device lockForConfiguration:nil];
  150. self.input.device.torchMode = self.torch ? AVCaptureTorchModeOn : AVCaptureTorchModeOff;
  151. [self.input.device unlockForConfiguration];
  152. }
  153. - (void)setTransform:(CGAffineTransform)transform {
  154. _transform = transform;
  155. [self.layer setAffineTransform:transform];
  156. }
  157. #pragma mark - Back, Front, Torch
  158. - (int)back {
  159. return 1;
  160. }
  161. - (int)front {
  162. return 0;
  163. }
  164. - (BOOL)hasFront {
  165. NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
  166. return [devices count] > 1;
  167. }
  168. - (BOOL)hasBack {
  169. NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
  170. return [devices count] > 0;
  171. }
  172. - (BOOL)hasTorch {
  173. if ([self device]) {
  174. return [self device].hasTorch;
  175. } else {
  176. return NO;
  177. }
  178. }
  179. #pragma mark - Binary
  180. - (CALayer *)binary {
  181. return self.binaryLayer;
  182. }
  183. - (void)setBinary:(BOOL)on {
  184. if (on && !self.binaryLayer) {
  185. self.binaryLayer = [CALayer layer];
  186. } else if (!on && self.binaryLayer) {
  187. self.binaryLayer = nil;
  188. }
  189. }
  190. #pragma mark - Luminance
  191. - (CALayer *)luminance {
  192. return self.luminanceLayer;
  193. }
  194. - (void)setLuminance:(BOOL)on {
  195. if (on && !self.luminanceLayer) {
  196. self.luminanceLayer = [CALayer layer];
  197. } else if (!on && self.luminanceLayer) {
  198. self.luminanceLayer = nil;
  199. }
  200. }
  201. #pragma mark - Start, Stop
  202. - (void)hard_stop {
  203. self.hardStop = YES;
  204. if (self.running) {
  205. [self stop];
  206. }
  207. }
  208. - (void)order_skip {
  209. self.orderInSkip = 1;
  210. self.orderOutSkip = 1;
  211. }
  212. - (void)start {
  213. if (self.hardStop) {
  214. return;
  215. }
  216. if (self.delegate || self.luminanceLayer || self.binaryLayer) {
  217. (void)[self output];
  218. }
  219. if (!self.session.running) {
  220. static int i = 0;
  221. if (++i == -2) {
  222. abort();
  223. }
  224. [self.session startRunning];
  225. }
  226. self.running = YES;
  227. }
  228. - (void)stop {
  229. if (!self.running) {
  230. return;
  231. }
  232. if (self.session.running) {
  233. [self.session stopRunning];
  234. }
  235. self.running = NO;
  236. }
  237. #pragma mark - CAAction
  238. - (id<CAAction>)actionForLayer:(CALayer *)_layer forKey:(NSString *)event {
  239. [CATransaction setValue:[NSNumber numberWithFloat:0.0f] forKey:kCATransactionAnimationDuration];
  240. if ([event isEqualToString:kCAOnOrderIn] || [event isEqualToString:kCAOnOrderOut]) {
  241. return self;
  242. }
  243. return nil;
  244. }
  245. - (void)runActionForKey:(NSString *)key object:(id)anObject arguments:(NSDictionary *)dict {
  246. if ([key isEqualToString:kCAOnOrderIn]) {
  247. if (self.orderInSkip) {
  248. self.orderInSkip--;
  249. return;
  250. }
  251. self.onScreen = YES;
  252. [self startStop];
  253. } else if ([key isEqualToString:kCAOnOrderOut]) {
  254. if (self.orderOutSkip) {
  255. self.orderOutSkip--;
  256. return;
  257. }
  258. self.onScreen = NO;
  259. [self startStop];
  260. }
  261. }
  262. #pragma mark - AVCaptureVideoDataOutputSampleBufferDelegate
  263. - (void)captureOutput:(AVCaptureOutput *)captureOutput
  264. didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer
  265. fromConnection:(AVCaptureConnection *)connection {
  266. if (!self.running) return;
  267. @autoreleasepool {
  268. if (!self.cameraIsReady) {
  269. self.cameraIsReady = YES;
  270. if ([self.delegate respondsToSelector:@selector(captureCameraIsReady:)]) {
  271. dispatch_async(dispatch_get_main_queue(), ^{
  272. [self.delegate captureCameraIsReady:self];
  273. });
  274. }
  275. }
  276. if (!self.captureToFilename && !self.luminanceLayer && !self.binaryLayer && !self.delegate) {
  277. return;
  278. }
  279. CVImageBufferRef videoFrame = CMSampleBufferGetImageBuffer(sampleBuffer);
  280. CGImageRef videoFrameImage = [ZXCGImageLuminanceSource createImageFromBuffer:videoFrame];
  281. CGImageRef rotatedImage = [self createRotatedImage:videoFrameImage degrees:self.rotation];
  282. CGImageRelease(videoFrameImage);
  283. // If scanRect is set, crop the current image to include only the desired rect
  284. if (!CGRectIsEmpty(self.scanRect)) {
  285. CGImageRef croppedImage = CGImageCreateWithImageInRect(rotatedImage, self.scanRect);
  286. CFRelease(rotatedImage);
  287. rotatedImage = croppedImage;
  288. }
  289. self.lastScannedImage = rotatedImage;
  290. if (self.captureToFilename) {
  291. NSURL *url = [NSURL fileURLWithPath:self.captureToFilename];
  292. CGImageDestinationRef dest = CGImageDestinationCreateWithURL((__bridge CFURLRef)url, (__bridge CFStringRef)@"public.png", 1, nil);
  293. CGImageDestinationAddImage(dest, rotatedImage, nil);
  294. CGImageDestinationFinalize(dest);
  295. CFRelease(dest);
  296. self.captureToFilename = nil;
  297. }
  298. ZXCGImageLuminanceSource *source = [[ZXCGImageLuminanceSource alloc] initWithCGImage:rotatedImage];
  299. CGImageRelease(rotatedImage);
  300. if (self.luminanceLayer) {
  301. CGImageRef image = source.image;
  302. CGImageRetain(image);
  303. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0), dispatch_get_main_queue(), ^{
  304. self.luminanceLayer.contents = (__bridge id)image;
  305. CGImageRelease(image);
  306. });
  307. }
  308. if (self.binaryLayer || self.delegate) {
  309. ZXHybridBinarizer *binarizer = [[ZXHybridBinarizer alloc] initWithSource:self.invert ? [source invert] : source];
  310. if (self.binaryLayer) {
  311. CGImageRef image = [binarizer createImage];
  312. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0), dispatch_get_main_queue(), ^{
  313. self.binaryLayer.contents = (__bridge id)image;
  314. CGImageRelease(image);
  315. });
  316. }
  317. if (self.delegate) {
  318. ZXBinaryBitmap *bitmap = [[ZXBinaryBitmap alloc] initWithBinarizer:binarizer];
  319. NSError *error;
  320. ZXResult *result = [self.reader decode:bitmap hints:self.hints error:&error];
  321. if (result) {
  322. dispatch_async(dispatch_get_main_queue(), ^{
  323. [self.delegate captureResult:self result:result];
  324. });
  325. }
  326. }
  327. }
  328. }
  329. }
  330. #pragma mark - Private
  331. // Adapted from http://blog.coriolis.ch/2009/09/04/arbitrary-rotation-of-a-cgimage/ and https://github.com/JanX2/CreateRotateWriteCGImage
  332. - (CGImageRef)createRotatedImage:(CGImageRef)original degrees:(float)degrees CF_RETURNS_RETAINED {
  333. if (degrees == 0.0f) {
  334. CGImageRetain(original);
  335. return original;
  336. } else {
  337. double radians = degrees * M_PI / 180;
  338. #if TARGET_OS_EMBEDDED || TARGET_IPHONE_SIMULATOR
  339. radians = -1 * radians;
  340. #endif
  341. size_t _width = CGImageGetWidth(original);
  342. size_t _height = CGImageGetHeight(original);
  343. CGRect imgRect = CGRectMake(0, 0, _width, _height);
  344. CGAffineTransform __transform = CGAffineTransformMakeRotation(radians);
  345. CGRect rotatedRect = CGRectApplyAffineTransform(imgRect, __transform);
  346. CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
  347. CGContextRef context = CGBitmapContextCreate(NULL,
  348. rotatedRect.size.width,
  349. rotatedRect.size.height,
  350. CGImageGetBitsPerComponent(original),
  351. 0,
  352. colorSpace,
  353. kCGBitmapAlphaInfoMask & kCGImageAlphaPremultipliedFirst);
  354. CGContextSetAllowsAntialiasing(context, FALSE);
  355. CGContextSetInterpolationQuality(context, kCGInterpolationNone);
  356. CGColorSpaceRelease(colorSpace);
  357. CGContextTranslateCTM(context,
  358. +(rotatedRect.size.width/2),
  359. +(rotatedRect.size.height/2));
  360. CGContextRotateCTM(context, radians);
  361. CGContextDrawImage(context, CGRectMake(-imgRect.size.width/2,
  362. -imgRect.size.height/2,
  363. imgRect.size.width,
  364. imgRect.size.height),
  365. original);
  366. CGImageRef rotatedImage = CGBitmapContextCreateImage(context);
  367. CFRelease(context);
  368. return rotatedImage;
  369. }
  370. }
  371. - (AVCaptureDevice *)device {
  372. if (self.captureDevice) {
  373. return self.captureDevice;
  374. }
  375. AVCaptureDevice *zxd = nil;
  376. NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
  377. if ([devices count] > 0) {
  378. if (self.captureDeviceIndex == -1) {
  379. AVCaptureDevicePosition position = AVCaptureDevicePositionBack;
  380. if (self.camera == self.front) {
  381. position = AVCaptureDevicePositionFront;
  382. }
  383. for (unsigned int i = 0; i < [devices count]; ++i) {
  384. AVCaptureDevice *dev = [devices objectAtIndex:i];
  385. if (dev.position == position) {
  386. self.captureDeviceIndex = i;
  387. zxd = dev;
  388. break;
  389. }
  390. }
  391. }
  392. if (!zxd && self.captureDeviceIndex != -1) {
  393. zxd = [devices objectAtIndex:self.captureDeviceIndex];
  394. }
  395. }
  396. if (!zxd) {
  397. zxd = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
  398. }
  399. self.captureDevice = zxd;
  400. return zxd;
  401. }
  402. - (void)replaceInput {
  403. [self.session beginConfiguration];
  404. if (self.session && self.input) {
  405. [self.session removeInput:self.input];
  406. self.input = nil;
  407. }
  408. AVCaptureDevice *zxd = [self device];
  409. if (zxd) {
  410. self.input = [AVCaptureDeviceInput deviceInputWithDevice:zxd error:nil];
  411. self.focusMode = self.focusMode;
  412. }
  413. if (self.input) {
  414. #if TARGET_OS_IPHONE
  415. if ([self.input.device supportsAVCaptureSessionPreset:AVCaptureSessionPreset1920x1080]) {
  416. _sessionPreset = AVCaptureSessionPreset1920x1080;
  417. } else {
  418. _sessionPreset = AVCaptureSessionPreset1280x720;
  419. }
  420. #else
  421. _sessionPreset = AVCaptureSessionPreset1280x720;
  422. #endif
  423. self.session.sessionPreset = self.sessionPreset;
  424. [self.session addInput:self.input];
  425. }
  426. [self.session commitConfiguration];
  427. }
  428. - (AVCaptureSession *)session {
  429. if (!_session) {
  430. _session = [[AVCaptureSession alloc] init];
  431. [self replaceInput];
  432. }
  433. return _session;
  434. }
  435. - (void)startStop {
  436. if ((!self.running && (self.delegate || self.onScreen)) ||
  437. (!self.output &&
  438. (self.delegate ||
  439. (self.onScreen && (self.luminanceLayer || self.binaryLayer))))) {
  440. [self start];
  441. }
  442. if (self.running && !self.delegate && !self.onScreen) {
  443. [self stop];
  444. }
  445. }
  446. @end