iOS development Swift uses avfoundation to implement barcode scanning (attach: zoom lens to improve reading)

Source: Internet
Author: User

1, barcode (one-dimensional code) of the scan read
Originally wrote an article on how to use the camera scan to read two-dimensional code: Swift-Two-D code QRCode read (from the image read, or through the camera scan) to read the barcode through the camera, only to the original two-dimensional code to read the code metadataobjecttypes The following modifications can be made:

Self.output.metadataObjectTypes = [Avmetadataobjecttypeean13code,
Avmetadataobjecttypeean8code, Avmetadataobjecttypecode128code,
Avmetadataobjecttypecode39code,avmetadataobjecttypecode93code]
2, zoom in and improve barcode reading effect

A netizen response, if the bar code is too small (such as iwatch on the production of Small bar code) will not be recognized.
The solution is: like Alipay, QQ, through the code to narrow the lens focus, enlarge the content area so that the machine better identification.
The left image below is the original size, and the right image magnifies the picture 1.5 times times:


Import Uikit
Import Avfoundation

Class Viewcontroller:uiviewcontroller, Avcapturemetadataoutputobjectsdelegate,
uialertviewdelegate{

var scanrectview:uiview!
var device:avcapturedevice!
var input:avcapturedeviceinput!
var output:avcapturemetadataoutput!
var session:avcapturesession!
var preview:avcapturevideopreviewlayer!

Override Func Viewdidload () {
Super.viewdidload ()

Fromcamera ()
}

Scanning through the camera
Func Fromcamera () {
do{
Self.device = Avcapturedevice.defaultdevicewithmediatype (Avmediatypevideo)

Self.input = Try Avcapturedeviceinput (device:device)

Self.output = Avcapturemetadataoutput ()
Output.setmetadataobjectsdelegate (self, queue:dispatch_get_main_queue ())

Self.session = Avcapturesession ()
If Uiscreen.mainscreen (). bounds.size.height<500 {
Self.session.sessionPreset = avcapturesessionpreset640x480
}else{
Self.session.sessionPreset = Avcapturesessionpresethigh
}

Self.session.addInput (Self.input)
Self.session.addOutput (Self.output)

Self.output.metadataObjectTypes = [Avmetadataobjecttypeean13code,
Avmetadataobjecttypeean8code, Avmetadataobjecttypecode128code,
Avmetadataobjecttypecode39code,avmetadataobjecttypecode93code]

To compute the intermediate detectable region
Let windowsize:cgsize = Uiscreen.mainscreen (). bounds.size;
Let scansize:cgsize = Cgsizemake (WINDOWSIZE.WIDTH*3/4,
WINDOWSIZE.WIDTH*3/4);
var scanrect:cgrect = CGRectMake (windowsize.width-scansize.width)/2,
(windowsize.height-scansize.height)/2, scansize.width, scansize.height);
Compute rectofinterest Note x,y swap position
Scanrect = CGRectMake (Scanrect.origin.y/windowsize.height,
Scanrect.origin.x/windowsize.width,
Scanrect.size.height/windowsize.height,
Scanrect.size.width/windowsize.width);
To set up a detectable area
Self.output.rectOfInterest = Scanrect

Self.preview = Avcapturevideopreviewlayer (session:self.session)
Self.preview.videoGravity = Avlayervideogravityresizeaspectfill
Self.preview.frame = Uiscreen.mainscreen (). Bounds
Self.view.layer.insertSublayer (Self.preview, atindex:0)

Add the middle Probe area Green box
Self.scanrectview = UIView ();
Self.view.addSubview (Self.scanrectview)
Self.scanRectView.frame = CGRectMake (0, 0, scansize.width, scansize.height);
Self.scanRectView.center = Cgpointmake (
Cgrectgetmidx (Uiscreen.mainscreen (). Bounds),
Cgrectgetmidy (Uiscreen.mainscreen (). bounds));
Self.scanRectView.layer.borderColor = Uicolor.greencolor (). Cgcolor
Self.scanRectView.layer.borderWidth = 1;

Start capturing
Self.session.startRunning ()

Amplification
do {
Try self.device!. Lockforconfiguration ()
Catch _ {
NSLog ("error:lockforconfiguration.");
}
self.device!. Videozoomfactor = 1.5
self.device!. Unlockforconfiguration ()

}catch _ as Nserror{
Print error messages
Let Erroralert = Uialertview (title: "Reminder",
Message: "Please allow this program to access your camera on the iphone's settings-privacy-camera \" option,
Delegate:self,
Cancelbuttontitle: "OK")
Erroralert.show ()
}
}

Camera capture
Func Captureoutput (captureoutput:avcaptureoutput!,
Didoutputmetadataobjects metadataobjects: [anyobject]!,
Fromconnection connection:avcaptureconnection!) {

var stringvalue:string?
If Metadataobjects.count > 0 {
Let MetadataObject = Metadataobjects[0]
as! Avmetadatamachinereadablecodeobject
StringValue = Metadataobject.stringvalue

If StringValue!= nil{
Self.session.stopRunning ()
}
}
Self.session.stopRunning ()
Output results
Let Alertview = Uialertview (title: "Two-dimensional Code", Message:stringvalue,
Delegate:self, Cancelbuttontitle: "OK")
Alertview.show ()
}

message box disappears after confirmation
Func Alertview (Alertview:uialertview, Willdismisswithbuttonindex buttonindex:int) {
Continue scanning
Self.session.startRunning ()
}

Override Func didreceivememorywarning () {
Super.didreceivememorywarning ()
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.