iOS7ã§è¿½å ããããã¼ã³ã¼ãèªã¿è¾¼ã¿ã試ã
iOSã§ãã¼ã³ã¼ããèªã¿åããã¨æãã¨ãZXingã¨ãããµã¼ããã¼ãã£è£½ã®ã©ã¤ãã©ãªãå©ç¨ããå¿
è¦ããã£ãã
iOS7ã§ã¯ãã¼ã³ã¼ãèªã¿åãç¨ã®ã©ã¤ãã©ãªãããã©ã«ãã§å
¥ã£ã¦ãããç°¡åã«å©ç¨ãããã¨ãã§ããã
ãã¼ã³ã¼ã以å¤ã«ãQRã³ã¼ããªã©ã®å©ç¨é »åº¦é«ãããªãã®ãï¼ã¤ããµã©ï¼è¿½å ããã¦ããã
ã³ã¼ãã®ç¨®é¡ | 宿° |
---|---|
UPC-E | AVMetadataObjectTypeUPCECode |
Code 39 | AVMetadataObjectTypeCode39Code |
Code 39 | mod 43 AVMetadataObjectTypeCode39Mod43Code |
EAN-13ï¼JANã³ã¼ãæ¨æºã¿ã¤ãï¼ | AVMetadataObjectTypeEAN13Code |
EAN-8ï¼JANã³ã¼ãç縮ã¿ã¤ãï¼ | AVMetadataObjectTypeEAN8Code |
Code 93 | AVMetadataObjectTypeCode93Code |
Code 128 | AVMetadataObjectTypeCode128Code |
PDF417 | AVMetadataObjectTypePDF417Code |
QRã³ã¼ã | AVMetadataObjectTypeQRCode |
Aztec code | AVMetadataObjectTypeAztecCode |
ï¼åèï¼äººã®é¡ | AVMetadataObjectTypeFace |
ä¸ã§ã人ã®é¡ãªããã¯è¤æ°äººãã¦ãã¡ããã¨èªã¿åãããããã»ã»ã»
ä»åã¯xibã§ãã¼ã³ã¼ãèªã¿åãæã®ã«ã¡ã©ã表示ããããã¥ã¼ãç¨æãã¦ããã¦ã ããã«ã«ã¡ã©ã®æ åãè²¼ãä»ããããæ¹ãæ¸ãã¦ãã¾ãã
#import <AVFoundation/AVFoundation.h> #import <AudioToolbox/AudioServices.h> @interface BarcodeViewController : UIViewController<AVCaptureMetadataOutputObjectsDelegate>{ AVCaptureSession *_session; AVCaptureDevice *_device; AVCaptureDeviceInput *_input; AVCaptureMetadataOutput *_output; AVCaptureVideoPreviewLayer *_prevLayer; UIView *_highlightView; IBOutlet UIView *barcodeView; NSString *getBarcodeString; } @property (weak, nonatomic) IBOutlet UIView *barcodeView;
viewdidloadã¯ç°¡åã«ã»ã»ã»
getBarcodeString = nil; [self barcodeCap];
ã¡ã¤ã³ã®é¨åã®å®è£
-(void)barcodeCap{ _highlightView = [[UIView alloc] init]; _highlightView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin|UIViewAutoresizingFlexibleBottomMargin; _highlightView.layer.borderColor = [UIColor redColor].CGColor; _highlightView.layer.borderWidth = 3; [self.barcodeView addSubview:_highlightView]; _session = [[AVCaptureSession alloc] init]; _device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; NSError *error = nil; _input = [AVCaptureDeviceInput deviceInputWithDevice:_device error:&error]; if (_input) { [_session addInput:_input]; } else { NSLog(@"Error: %@", error); } _output = [[AVCaptureMetadataOutput alloc] init]; [_output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()]; [_session addOutput:_output]; _output.metadataObjectTypes = [_output availableMetadataObjectTypes]; _prevLayer = [AVCaptureVideoPreviewLayer layerWithSession:_session]; _prevLayer.frame = self.barcodeView.bounds; _prevLayer.videoGravity = AVLayerVideoGravityResizeAspectFill; [self.barcodeView.layer addSublayer:_prevLayer]; AVCaptureConnection *con = _prevLayer.connection; con.videoOrientation = AVCaptureVideoOrientationLandscapeLeft; [_session startRunning]; [self.barcodeView bringSubviewToFront:_highlightView]; } - (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection{ CGRect highlightViewRect = CGRectZero; AVMetadataMachineReadableCodeObject *barCodeObject; NSString *detectionString = nil; NSArray *barCodeTypes = @[AVMetadataObjectTypeEAN13Code, AVMetadataObjectTypeEAN8Code]; for (AVMetadataObject *metadata in metadataObjects) { for (NSString *type in barCodeTypes) { if ([metadata.type isEqualToString:type]){ barCodeObject = (AVMetadataMachineReadableCodeObject *)[_prevLayer transformedMetadataObjectForMetadataObject:(AVMetadataMachineReadableCodeObject *)metadata]; highlightViewRect = barCodeObject.bounds; detectionString = [(AVMetadataMachineReadableCodeObject *)metadata stringValue]; break; } } if (detectionString != nil){ NSLog(@"èªã¿è¾¼ãã ã®ã¯%@",detectionString); AudioServicesPlaySystemSound(1000); getBarcodeString = detectionString; break; } } _highlightView.frame = highlightViewRect; }
AVCaptureConnection *con = _prevLayer.connection;
con.videoOrientation = AVCaptureVideoOrientationLandscapeLeft;
ã¯ç¡ãã¦ãOKã
横ç»é¢è¡¨ç¤ºãããã¨ãã«å¿
è¦ã£ã¦ã ãã
ããããã«ã¡ã©ã¢ããªãä½ãã¨ãã¨ä¼¼ã¦ãããããªããã¨æãã¾ãã
NSArray *barCodeTypes = @[AVMetadataObjectTypeEAN13Code, AVMetadataObjectTypeEAN8Code];
ãã®é
åã«èªèããããã¿ã¤ããè¨è¼ãã¦ããã ãã§ãã
ä»åã¯ãã¼ã³ã¼ããªã®ã§ãï¼ã¤å
¥ãã¦ãã¾ãããQRã³ã¼ãã ãã®å ´åã¯AVMetadataObjectTypeQRCode
ãå
¥ãããã¨ã«ãªãã¾ãã
以ä¸ï¼