Two-dimensional code
- The generation and reading of two-dimensional code has been integrated from iOS7
Previously widely used ZBARSDK does not currently support 64-bit processors
Steps to generate a QR code:
- Pour coreimage frame
- Generate two-dimensional code with filter Cifilter
QR Code content (Traditional barcode function put numbers)
Generation of two-dimensional code
1. Creating filtersCifilter *filter = [Cifilter Filterwithname:@ "Ciqrcodegenerator"];2. Restore the default [filter SetDefaults];//3. Add data to filters (regular expressions/accounts and Passwords) nsstring *datastring = @ "http://www.520it.com"; nsdata *data = [datastring datausingencoding: Nsutf8stringencoding]; [Filter Setvalue:data Forkeypath:@ "InputMessage"]; //4. Gets the output of the QR code ciimage *outputimage = [Filter outputimage]; Span class= "Hljs-comment" >//because the generated two-dimensional code is blurred, so createnoninterpolateduiimageformciimage:outputimage to obtain a high-definition QR code image //5. Show QR code self.imageview.image = [self createnoninterpolated Uiimageformciimage:outputimage withsize:200];
- Implementation of Createnoninterpolateduiimageformciimage:outputimage method
/** * Generates uiimage of the specified size according to Ciimage * @param image ciimage * @param size picture width */-(UIImage *) createnoninterpolatedUiimageformciimage: (Ciimage *) Image withsize: (CGFloat) size{CGRect extent =Cgrectintegral (image. extent);CGFloat scale = MIN (size/Cgrectgetwidth (extent), size/Cgrectgetheight (extent));1. Create bitmap; size_t width =Cgrectgetwidth (extent) * scale; size_t height =Cgrectgetheight (extent) * scale;Cgcolorspaceref cs =Cgcolorspacecreatedevicegray ();Cgcontextref Bitmapref =Cgbitmapcontextcreate (Nil, width, height,8,0, CS, (Cgbitmapinfo) KCgimagealphanone);Cicontext *context = [Cicontext contextwithoptions:Nil]; cgimageref bitmapImage = [Context Createcgimage:image fromrect:extent]; cgcontextsetinterpolationquality (bitmapref, Kcginterpolationnone); CGCONTEXTSCALECTM (bitmapref, scale, scale); Cgcontextdrawimage (Bitmapref, extent, bitmapImage); //2. Save bitmap to Picture cgimageref scaledimage = cgbitmapcontextcreateimage (bitmapref); cgcontextrelease (BITMAPREF); cgimagerelease (bitmapImage); return [UIImage imagewithcgimage:scaledimage];}
Scanning of QR code
1. Create a capture sessionAvcapturesession *session = [[Avcapturesession alloc] init];Self. session = Session;2. Add input device (data from camera input)Avcapturedevice *device = [Avcapturedevice Defaultdevicewithmediatype:Avmediatypevideo];Avcapturedeviceinput *input = [Avcapturedeviceinput Deviceinputwithdevice:device Error:NIL]; [Session Addinput:input];3. Add Output data (example Object----class object-------the Meta-class object)Avcapturemetadataoutput *output = [[Avcapturemetadataoutput alloc] init]; [Output setmetadataobjectsdelegate:Self queue:dispatch_get_main_queue ()]; [Session Addoutput:output]; //3.1. Set the type of input metadata (type is the QR code data) [Output setmetadataobjecttypes:@[Avmetadataobjecttypeqrcode]]; //4. Add scan layer avcapturevideopreviewlayer *layer = [avcapturevideopreviewlayer layerwithsession:session]; Layer. frame = self. View. Bounds; [self. View. Layer Addsublayer:layer]; self. Layer = layer; //5. Start scanning [session startrunning];
- Methods that are called when a result is scanned
This method is executed when the data is scanned-(void) Captureoutput: (Avcaptureoutput *) Captureoutput didoutputmetadataobjects: (Nsarray *) metadataobjects fromconnection: (avcaptureconnection *) connection{ if (metadataobjects. Count > 0) { //Get scanned data, last time the latest scanned data Avmetadatamachinereadablecodeobject *object = [Metadataobjects lastobject]; NSLog (@ "%@", Object. stringvalue); //Stop scanning [self. session stoprunning]; //Remove the preview layer [self. Layer Removefromsuperlayer];} else { NSLog (@ "no data scanned");}}
Wen/superwx (author of Jane's book)
Original link: http://www.jianshu.com/p/d1675f536151
Copyright belongs to the author, please contact the author to obtain authorization, and Mark "book author".
iOS development-two-D code