IOS scans and generates QR codes, and ios scans

Source: Internet
Author: User

IOS scans and generates QR codes, and ios scans

1. QR code generation

Libqrencode: it is a library written in C language to parse two-dimensional code (QRCode). libqrencode scans two-dimensional code through the CCD camera of the mobile phone. Http://pan.baidu.com/s/1eQs1Epk

To import the libqrencode library, run the following code:

#import <Foundation/Foundation.h>@interface QRCodeGenerator : NSObject+ (UIImage *)qrImageForString:(NSString *)string imageSize:(CGFloat)size;+ (UIImage *) twoDimensionCodeImage:(UIImage *)twoDimensionCode withAvatarImage:(UIImage *)avatarImage;@end

# Import "QRCodeGenerator. h "# import" qrencode. h "enum {qr_margin = 3}; @ implementation QRCodeGenerator + (void) drawQRCode :( QRcode *) code context :( CGContextRef) ctx size :( CGFloat) size {unsigned char * data = 0; int width; data = code-> data; width = code-> width; float zoom = (double) size/(code-> width + 2.0 * qr_margin ); CGRect rectDraw = CGRectMake (0, 0, zoom, zoom); // drawCGContextSetFillColor (ctx, CGColorGe TComponents ([UIColor blackColor]. CGColor); for (int I = 0; I <width; ++ I) {for (int j = 0; j <width; ++ j) {if (* data & 1) {rectDraw. origin = CGPointMake (j + qr_margin) * zoom, (I + qr_margin) * zoom); CGContextAddRect (ctx, rectDraw) ;}+ + data ;}} CGContextFillPath (ctx);} + (UIImage *) qrImageForString :( NSString *) string imageSize :( CGFloat) size {if (! [String length]) {return nil;} QRcode * code = QRcode_encodeString ([string UTF8String], 0, QR_ECLEVEL_L, QR_MODE_8, 1); if (! Code) {return nil;} // create contextCGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB (); CGContextRef ctx = CGBitmapContextCreate (0, size, size, 8, size * 4, colorSpace, interval ); export translateTransform = transform (0,-size); CGAffineTransform scaleTransform = transform (1,-1); CGContextConcatCTM (ctx, transform (translateTransform, scaleTransform )); // draw QR on this context [QRCodeGenerator drawQRCode: code context: ctx size: size]; // get imageCGImageRef qrCGImage = ctx; UIImage * qrImage = [UIImage imageWithCGImage: qrCGImage]; // some values (ctx); CGImageRelease (qrCGImage); CGColorSpaceRelease (colorSpace); QRcode_free (code); return qrImage;} + (UIImage *) values :( UIImage *) twoDimensionCode withAvatarImage :( UIImage *) avatarImage {// two-dimension code QR code CGSize size = twoDimensionCode. size; CGSize size2 = CGSizeMake (1.0/5.5 * size. width, 1.0/5.5 * size. height); UIGraphicsBeginImageContext (size); [twoDimensionCode drawInRect: CGRectMake (0, 0, size. width, size. height)]; [[self avatarImage: avatarImage] drawInRect: CGRectMake (size. width-size2.width)/2.0, (size. height-size2.height)/2.0, size2.width, size2.height)]; UIImage * resultingImage = watermark (); UIGraphicsEndImageContext (); return resultingImage;} + (UIImage *) avatarImage :( UIImage *) avatarImage {UIImage * avatarBackgroudImage = [UIImage imageNamed: @ "icon.png"]; CGSize size = avatarBackgroudImage. size; UIGraphicsBeginImageContext (size); [avatarBackgroudImage drawInRect: CGRectMake (0, 0, size. width, size. height)]; [avatarImage drawInRect: CGRectMake (10, 10, size. width-20, size. height-20)]; UIImage * resultingImage = UIGraphicsGetImageFromCurrentImageContext (); UIGraphicsEndImageContext (); return resultingImage;} @ end

2. Scan

IOS 7 Apple launched its own scanning function. The following code is used:


-(Void) viewWillAppear :( BOOL) animated {[self setupCamera];}-(void) setupCamera {// Device _ device = [AVCaptureDevice defaultDeviceWithMediaType: AVMediaTypeVideo]; // Input _ input = [AVCaptureDeviceInput deviceInputWithDevice: self. device error: nil]; // Output _ output = [[AVCaptureMetadataOutput alloc] init]; [_ output setMetadataObjectsDelegate: self queue: dispatch_get_main_queue ()]; // Session _ session = [[AVCaptureSession alloc] init]; [_ session setSessionPreset: AVCaptureSessionPresetHigh]; if ([_ session canAddInput: self. input]) {[_ session addInput: self. input];} if ([_ session canAddOutput: self. output]) {[_ session addOutput: self. output];} // bar code AVMetadataObjectTypeQRCode _ output. metadataObjectTypes = @ [AVMetadataObjectTypeCode128Code, response, AVMetadataObjectTypeCode39Code, response, AVMetadataObjectTypeCode93Code, avmetadataobjecttype1_response code, AVMetadataObjectTypeQRCode, response, Response] // Preview _ preview = [AVCaptureVideoPreviewLayer layerWithSession: self. session]; _ preview. videoGravity = AVLayerVideoGravityResizeAspectFill; _ preview. frame = CGRectMake (20,110,280,280); [self. view. layer insertSublayer: self. preview atIndex: 0]; // Start [_ session startRunning];} # pragma mark upload-(void) captureOutput :( AVCaptureOutput *) captureOutput didOutputMetadataObjects :( NSArray *) metadataObjects fromConnection :( AVCaptureConnection *) connection {NSString * stringValue; if ([metadataObjects count]> 0) {Region * metadataObject = [metadataObjects objectAtIndex: 0]; stringValue = metadataObject. stringValue;} [_ session stopRunning]; UIAlertView * alert = [[UIAlertView alloc] initWithTitle: @ "prompt" message: [NSString stringWithFormat: @ "Result: % @", stringValue] delegate: self cancelButtonTitle: nil otherButtonTitles: @ "OK", @ "Rescan", nil]; [alert show];}-(void) alertView :( UIAlertView *) alertView clickedButtonAtIndex :( NSInteger) buttonIndex {if (buttonIndex = 0) {[self dismissViewControllerAnimated: YES completion: ^ {[timer invalidate] ;}];} else {[_ session startRunning];}


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.