Written in the first
/*
The advantage of the Avfoundation native framework is that scanning is particularly fast and efficient, but there may be a problem with not knowing how to limit the scan range.
QR code scan for excellent third-party libraries:----ZXing Google launches open source project-----Zbar SDK also has detailed documentation
*/
Introducing a header file into the framework required for streaming media
@import avfoundation;
Compliance Agreement Avcapturemetadataoutputobjectsdelegate
@property (Nonatomic,strong) avcapturesession *session;
@property (Nonatomic,strong) Avcapturevideopreviewlayer *layer;
-(void) Viewdidload {
[Super Viewdidload];
[Self startrunningsession];
}
-(void) startrunningsession
Get Avcapturedevice Instance
Nserror * ERROR;
Avcapturedevice *capturedevice = [Avcapturedevice defaultdevicewithmediatype:avmediatypevideo];
[Capturedevice Settorchmode:avcapturetorchmodeon]; Turn on Lighting
Initializing the input stream
Avcapturedeviceinput *input = [Avcapturedeviceinput deviceinputwithdevice:capturedevice error:&error];
if (!input) {
NSLog (@ "%@", [Error localizeddescription]);
}
Create a session
_session = [[Avcapturesession alloc] init];
if ([_session Canaddinput:input]) {
Add an input stream
[_session Addinput:input];
}
Initializing the output stream
Avcapturemetadataoutput *capturemetadataoutput = [[Avcapturemetadataoutput alloc] init];
if ([_session Canaddoutput:capturemetadataoutput]) {
Adding an output stream
[_session Addoutput:capturemetadataoutput];
}
[Capturemetadataoutput setmetadataobjectsdelegate:self Queue:dispatch_get_main_queue ()];
Set meta data type Avmetadataobjecttypeqrcode
[Capturemetadataoutput Setmetadataobjecttypes:@[avmetadataobjecttypeqrcode]];
Value CGRectMake (0, 0, 1, 1). Metadata objects whose bounds do no intersect with the rectofinterest won't be returned.
[Capturemetadataoutput Setrectofinterest:cgrectmake (124/667.0,100/375.0,160/667.0, 160/375.0)];
Creating an Output Object
_layer = [Avcapturevideopreviewlayer layerwithsession:_session];
[_layer Setvideogravity:avlayervideogravityresizeaspectfill];
[_layer Setframe:cgrectmake (50, 100, 280, 280)];
[Self.view.layer Addsublayer:_layer];
Start session
[_session startrunning];
}
#pragma-mark-avcapturemetadataoutputobjectsdelegate
-(void) Captureoutput: (Avcaptureoutput *) captureoutput didoutputmetadataobjects: (Nsarray *) metadataobjects Fromconnection: (avcaptureconnection *) connection{
NSLog (@ "-------");
if (metadataobjects && metadataobjects.count) {
Avmetadatamachinereadablecodeobject *metadataobj = [Metadataobjects objectatindex:0];
NSString *result;
if ([[[Metadataobj Type] isequaltostring:avmetadataobjecttypeqrcode]) {
result = Metadataobj.stringvalue;
NSLog (@ "result:%@", result);
} else {
NSLog (@ "Not two-dimensional code");
}
[_session stoprunning];
_session = nil;
Uialertview *alert = [[Uialertview alloc] initwithtitle:@ "Qrcodeinfo"
Message:result
Delegate:nil
cancelbuttontitle:@ "Cancel"
Otherbuttontitles:nil];
[Alert show];
}
}
Written in the last
Very small two-dimensional code, the edge of less than 1cm, and then modified sessionPreset
for 1080p, when the use of zxing, when the picture quality changes, also caused the decline in performance, the basic open scanning interface will be reported memorywarning, but also did solve the problem of small QR code scanning.
AVCaptureSession
You can set the sessionPreset
property, which determines the size of the image quality for each frame of the video input.
- avcapturesessionpreset320x240
- avcapturesessionpreset352x288
- avcapturesessionpreset640x480
- avcapturesessionpreset960x540
- avcapturesessionpreset1280x720
- avcapturesessionpreset1920x1080
The above lists some of the attribute values, representing the input image quality, in general, AVCaptureSessionPreset640x480
enough to use, but if you want to ensure that the smaller QR code image can be quickly scanned, it is better to set higher, such as AVCaptureSessionPreset1920x1080
(we often say 1080p).
The speed and performance of the scan is to set the scope of the resolution, in Zbar and Zxing scanCrop
, the AVFoundation
properties set in AVCaptureMetadataOutput
rectOfInterest
to configure the resolution scope.
Captureoutput.Rectofinterest=CGRectMake(Croprect.Origin.Y/size. Height, croprect. Origin. X/size. Width, croprect. Size. Height/size. Height, croprect. Size. Width/size. Width
rectOfInterest
are cropped based on the size of the image.
"" this blog post originates from each road summary.
The implementation of iOS scanning