Two-dimensional code

Source: Internet
Author: User


#import "ViewController.h"
#import <CoreImage/CoreImage.h>
#import <AVFoundation/AVFoundation.h>
#import "CustomView.h"
#define WIDTH Self.view.frame.size.width
#define Height Self.view.frame.size.height
@interface Viewcontroller () <AVCaptureMetadataOutputObjectsDelegate>

{
BOOL Torchison;
}
@property (Weak, nonatomic) Iboutlet Uiimageview *imageview;
@property (Nonatomic,strong) avcapturesession * session;
@property (nonatomic,strong) avcapturevideopreviewlayer * preview;
@end

@implementation Viewcontroller
-(Ibaction) button: (ID) Sender {


[Self Creamt];
}

-(void) Viewdidload {
[Super Viewdidload];

1 instantiation of the QR Code filter
Cifilter * Filter=[cifilter filterwithname:@ "Ciqrcodegenerator"];
2. Restore the default properties of the filter
[Filter SetDefaults];
3 Converting a string to NSDate

NSData * data=[@ "http://www.baidu.com" datausingencoding:nsutf8stringencoding];
4 Pass the KVO settings filter to the data in the future filter will know to notify the passed in to generate two-dimensional code
[Filter Setvalue:data forkey:@ "InputMessage"];

5 generating two-dimensional code
Ciimage * Outputimage=[filter Outputimage];
Self.imageview.image=[uiimage Imagewithciimage:outputimage];
Additional setup after loading the view, typically from a nib.
}

-(void) didreceivememorywarning {
[Super didreceivememorywarning];
Dispose of any resources the can be recreated.
}
-(void) Creamt
{
1. Instance session shooting device
Avcapturedevice * Device=[avcapturedevice Defaultdevicewithmediatype:avmediatypevideo];
2. Setting the input device
Avcapturedeviceinput *input=[avcapturedeviceinput Deviceinputwithdevice:device Error:nil];
3 Setting the number of meta outputs
3.1 Power session shooting meta-data output
avcapturemetadataoutput* Output=[[avcapturemetadataoutput Alloc]init];
Setting the output data agent
[Output Setmetadataobjectsdelegate:self queue:dispatch_get_main_queue ()];
4 adding a shooting session
4.1 Power conversation Shooting session
Avcapturesession * Session=[[avcapturesession Alloc]init];
4.2 Adding session Input
[Session Addinput:input];
4.3 Adding session output
[Session Addoutput:output];
Self.session=session;

4.4 Set the output data type, you need to add the original data output to the session before you can specify the metadata type, or you will get an error
[Output SETMETADATAOBJECTTYPES:@[AVMETADATAOBJECTTYPEQRCODE]];

5
Avcapturevideopreviewlayer * Preview=[[avcapturevideopreviewlayer alloc]initwithsession:_session];
Preview.videogravity =avlayervideogravityresizeaspectfill;
Preview.frame=self.view.bounds;
5.2 Inserting a layer into the current view
[Self.view.layer Insertsublayer:preview atindex:100];
Self.preview=preview;
6. Start the session
[_session startrunning];

UIView * Line =[[uiview alloc]initwithframe:cgrectmake (0, 0, 280, 1)];
Line.backgroundcolor=[uicolor Orangecolor];

CustomView * L=[[customview alloc]initwithframe:cgrectmake (width/2-140, 100, 280, 280)];

[L Addsubview:line];
[Self runloop:line];
L.backgroundcolor=[uicolor Clearcolor];
[Self.view addsubview:l];


}
-(ibaction) Open: (ID) Sender {
if (Torchison) {
[Self turntorchon:no];
}else{
[Self turntorchon:yes];
}

}

-(void) Runloop: (uiview*) line
{
[UIView Animatewithduration:3 animations:^{
Line.frame=cgrectmake (0, 280, 280, 1);
}completion:^ (BOOL finished) {
Line.frame=cgrectmake (0, 0, 280, 1);
[Self runloop:line];
}];


}
-(void) Turntorchon: (bool) on {

Class Capturedeviceclass = nsclassfromstring (@ "Avcapturedevice");
if (capturedeviceclass! = nil) {
Avcapturedevice *device = [Avcapturedevice defaultdevicewithmediatype:avmediatypevideo];
if ([device hastorch] && [device Hasflash]) {

[Device Lockforconfiguration:nil];
if (ON) {
[Device Settorchmode:avcapturetorchmodeon];
[Device Setflashmode:avcaptureflashmodeon];
Torchison = YES;
} else {
[Device Settorchmode:avcapturetorchmodeoff];
[Device Setflashmode:avcaptureflashmodeoff];
Torchison = NO;
}
[Device unlockforconfiguration];
}
}
}
-(void) Captureoutput: (Avcaptureoutput *) captureoutput didoutputmetadataobjects: (Nsarray *) metadataobjects Fromconnection: (avcaptureconnection *) connection
{
Frequent scans, invoking proxy methods
1 if the scan is complete, stop the session
[Self.session stoprunning];
2 Deleting a preview layer
[Self.preview Removefromsuperlayer];
3 Setting interface to display scan results
Avmetadatamachinereadablecodeobject * obj =metadataobjects[0];
NSLog (@ "%@", Obj.stringvalue);

}
@end


#import "CustomView.h"
#define WIDTH Self.frame.size.width
#define Height Self.frame.size.height
@implementation CustomView


Only override Drawrect:if perform custom drawing.
An empty implementation adversely affects performance during animation.
-(void) DrawRect: (cgrect) Rect {


Get context
Cgcontextref context =uigraphicsgetcurrentcontext ();
Set Line Style
Cgcontextsetlinecap (context, kcglinecapround);
Set Line thickness width
Cgcontextsetlinewidth (context, 1.0);


Cgcontextdrawpath (context, kcgpathstroke);
Self.backgroundcolor=[uicolor Clearcolor];
Set color
Cgcontextsetrgbstrokecolor (Context, 1.0, 0.0, 0.0, 1.0);
Set a start path
Cgcontextbeginpath (context);
The starting point is set to (0,0): Note that this is the relative coordinate of the corresponding area in the context
Cgcontextmovetopoint (context, 0, 0);
Set the next coordinate point
Cgcontextaddlinetopoint (context, width, 0);
Set the next coordinate point
Cgcontextaddlinetopoint (context, width, height);
Set the next coordinate point
Cgcontextaddlinetopoint (context, 0, height);
Set the next coordinate point
Cgcontextaddlinetopoint (context, 0, 0);
Connect the coordinate points defined above
Cgcontextstrokepath (context);


Get context
Cgcontextref con =uigraphicsgetcurrentcontext ();
Set Line Style
Cgcontextsetlinecap (Con, kcglinecapround);
Set Line thickness width
Cgcontextsetlinewidth (context, 3.0);
Cgcontextdrawpath (Con, kcgpathstroke);
Set color
Cgcontextsetrgbstrokecolor (Con, 1.0, 1.0, 0.0, 1.0);
Set a start path
Cgcontextbeginpath (con);
The starting point is set to (0,0): Note that this is the relative coordinate of the corresponding area in the context
Cgcontextmovetopoint (Con, 0, 20);
Set the next coordinate point
Cgcontextaddlinetopoint (Con, 0, 0);
Set the next coordinate point
Cgcontextaddlinetopoint (Con, 20, 0);
Cgcontextstrokepath (con);


Get context
Cgcontextref Con2 =uigraphicsgetcurrentcontext ();
Set Line Style
Cgcontextsetlinecap (Con2, Kcglinecapround);
Set Line thickness width
Cgcontextsetlinewidth (context, 3.0);
Cgcontextdrawpath (Con2, Kcgpathstroke);
Set color
Cgcontextsetrgbstrokecolor (Con2, 1.0, 1.0, 0.0, 1.0);
Set a start path
Cgcontextbeginpath (con);
The starting point is set to (0,0): Note that this is the relative coordinate of the corresponding area in the context
Cgcontextmovetopoint (Con2, width-20, 0);
Set the next coordinate point
Cgcontextaddlinetopoint (con2, width, 0);
Set the next coordinate point
Cgcontextaddlinetopoint (Con2, width, 20);
Cgcontextstrokepath (Con2);


Lower left corner

Get context
Cgcontextref Con1 =uigraphicsgetcurrentcontext ();
Set Line Style
Cgcontextsetlinecap (Con1, Kcglinecapround);
Set Line thickness width
Cgcontextsetlinewidth (Con1, 3.0);
Cgcontextdrawpath (Con1, Kcgpathstroke);
Set color
Cgcontextsetrgbstrokecolor (Con1, 1.0, 0, 1.0, 1.0);
Set a start path
Cgcontextbeginpath (Con1);
The starting point is set to (0,0): Note that this is the relative coordinate of the corresponding area in the context
Cgcontextmovetopoint (con1, 0, height-20);
Set the next coordinate point
Cgcontextaddlinetopoint (con1, 0, height);
Set the next coordinate point
Cgcontextaddlinetopoint (Con1, max, height);
Cgcontextstrokepath (Con1);


Lower right corner

Get context
Cgcontextref Con3 =uigraphicsgetcurrentcontext ();
Set Line Style
Cgcontextsetlinecap (Con3, Kcglinecapround);
Set Line thickness width
Cgcontextsetlinewidth (Con3, 3.0);
Cgcontextdrawpath (Con3, Kcgpathstroke);
Set color
Cgcontextsetrgbstrokecolor (Con3, 1.0, 1.0, 0.0, 1.0);
Set a start path
Cgcontextbeginpath (Con3);
The starting point is set to (0,0): Note that this is the relative coordinate of the corresponding area in the context
Cgcontextmovetopoint (Con3, width-20, height);
Set the next coordinate point
Cgcontextaddlinetopoint (con3, width, height);
Set the next coordinate point
Cgcontextaddlinetopoint (Con3, Width, height-20);
Cgcontextstrokepath (Con3);

}

Two-dimensional code

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.