Use Libqrencode library to make QR code Electronic Business card--ios version

Source: Internet
Author: User
Tags vcard

1.:

Click the Generate button to display the QR code:

650) this.width=650; "Src=" https://s4.51cto.com/wyfs02/M01/8D/EB/wKiom1iu2vPBy0SdAABcpMVPeLQ880.png-wh_500x0-wm_ 3-wmp_4-s_3115143013.png "title=" 2635028-8bdc0dbabb6c19d7.png "alt=" wkiom1iu2vpby0sdaabcpmvpelq880.png-wh_50 "/ >

After using sweep, get the following interface:

650) this.width=650; "Src=" https://s4.51cto.com/wyfs02/M01/8D/EB/wKiom1iu2wjweiU7AABcpmDB3fY159.jpg-wh_500x0-wm_ 3-wmp_4-s_58627738.jpg "title=" after sweeping the effect. jpg "alt=" wkiom1iu2wjweiu7aabcpmdb3fy159.jpg-wh_50 "/>

2, Libqrencode Library:

Libqrencode is a cross-platform C language library written by a Japanese (Kentaro Fukuchi) to generate a QR code.
The official website is here, GitHub is here.
My favorite is this " written in C, cross-platform, functional single, easy to use " Library!
iOS in OBJC using C + + library Super Convenient, Android, a little trouble.

3, OBJC to Libqrencode Packaging:

The online search for the Qrcodegenerator class, implemented by Andrew Kopanev.
Very simply, just a method, given a string, returns a uiimage representation of a two-dimensional code image:

@interface qrcodegenerator:nsobject+ (UIImage *) qrimageforstring: (NSString *) string imageSize: (cgfloat) size; @end

Take a look at the key points of implementation:

+  (uiimage *) qrimageforstring: (nsstring *) String imagesize: (cgfloat) size {     if  (![ String length])  {        return nil;     }    //First step: Generate QRCode from UTF string, call the core function of Libqrencode     QRcode  *code = qrcode_encodestring ([String utf8string], 0, qr_eclevel_l, qr_mode_8,  1);    if  (!code)  {        return  nil;    }    //Second Step:create context     Cgcolorspaceref colorspace = cgcolorspacecreatedevicergb ();     cgcontextref  ctx = cgbitmapcontextcreate (0, size, size, 8, size * 4,  colorspace, kcgimagealphapremultipliedlast);     //Step Three: space transformation     Because the coordinate system of coregraphic is the right hand, the origin is     //in the lower left corner and the coordinate system in Uikit is the left hand system, the origin is in the upper left corner     // Therefore, after the following translation and scaling matrix operation, the UIImage is based on the left-hand corner of the left-handed line     CGAffineTransform translateTransform  = cgaffinetransformmaketranslation (0, -size);    cgaffinetransform  Scaletransform = cgaffinetransformmakescale (1, -1);     CGCONTEXTCONCATCTM ( Ctx, cgaffinetransformconcat (Translatetransform, scaletransform));     // Fourth step: Generate a two-dimensional code bitmap in the context of the image     // draw QR on this context         [QRCodeGenerator drawQRCode:code context:ctx size:size];     //Fifth Step:get image    cgimageref qrcgimage =  Cgbitmapcontextcreateimage (CTX);    uiimage * qrimage = [uiimage  imagewithcgimage:qrcgimage];     //Sixth step: Some releases    cgcontextrelease (CTX);     Cgimagerelease (Qrcgimage);     cgcolorspacerelease (colorspace);     Qrcode_free (code);     return qrimage;}

The main point is to pay attention to the third step, spatial transformation. transform the right-hand lower left corner to the upper left-hand corner of the system.

4. vcard Format:

Baidu Encyclopedia detailed description of the format of the vcard

Let's look at a common format using a function (see code comments ):

-(nsstring*)  getvcardstring{    nsmutablestring* ret = [[ Nsmutablestring alloc]init];    //vcard specification:     //1, beginning with Begin:vcard, End:vcard End     [ret appendstring:@ "begin:vcard \n"];    //2, Define a lot of keywords, please refer to Baidu Encyclopedia, this example used some of the more important keywords     //3, general format for  key : value, such as the following   fn: Aoyama minded Xianfeng line     [ret appendstring:@ "fn:"];    [ret  appendstring:@ "Castle Peak minded Xianfeng"];    [ret appendstring:@ "\ n"];    // 4, if there are sub-projects, use; separate, for example, the following Tel tel contains mobile phone cell and company phone work or home phone home, but also can be voice,fax and so on     [ret  appendstring:@ "TEL; CELL; VOICE: "];    [ret appendstring: @" 13900000000 "];    [ret  appendstring:@ "\ n"];    [ret appendstring:@ "TEL; work; VOICE: "];    [RET&Nbsp;appendstring:@ "021-12345678"];    [ret appendstring:@ "\ n"];     //Ibid, email also has sub-project     [ret appendstring:@ "email; Pref;internet: "];    [ret appendstring:@" [email protected] "];     [ret appendstring:@ "\ n"];    //5, a pit for a couple of hours. Note: When you see the code, you finally add the "\ n" character, right.     //    because   if the line is longer than 75 characters, you must have a branch     //     so   during insurance, each item adds a newline character     [ret appendstring:@ "URL:"];     [ret appendstring:@ "HTTPS://GITHUB.COM/JACKYBLF"];    [ret appendstring:@ " \ n "];    //6, custom keywords, start with x, and of course also compatible with your phone's phone store program, otherwise your mobile phone storage program does not recognize your customized content      /*    nsstring* qq = @ "47178234";    [ret  appendstring:@ "X-QQ:"];    [ret appendstring:qq];    [ret appendstring:@ "\ n"];    */    [ ret appendstring:@ "End:vcard"];    return ret;}
5, the creation of two-dimensional code in iOS Electronic Business card:
-(Ibaction) Forevent: (Uievent *) Event {//NOTE: Must be a square uiimageview, two-dimensional code is basically square bar self.imageView.image = [Qrcodegenerator Qrimageforstring:[self getvcardstring] imageSize:self.imageView.bounds.size.width];}


This article from "with the wind and the green shirt minded Xianfeng line" blog, reprint please contact the author!

Use Libqrencode library to make QR code Electronic Business card--ios version

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.