1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
| #pragma mark-> 二维码生成 -(void)create{ UIImage *image=[UIImage imageNamed:@"6824500_006_thumb.jpg"]; NSString*tempStr; if(self.textField.text.length==0){ tempStr=@"ddddddddd"; }else{ tempStr=self.textField.text; } UIImage*tempImage=[QRCodeGenerator qrImageForString:tempStr imageSize:360 Topimg:image withColor:RandomColor]; _outImageView.image=tempImage; } +(UIImage*)qrImageForString:(NSString *)string imageSize:(CGFloat)size Topimg:(UIImage *)topimg withColor:(UIColor*)color{ if (![string length]) { return nil; } QRcode *code = QRcode_encodeString([string UTF8String], 0, QR_ECLEVEL_L, QR_MODE_8, 1); if (!code) { return nil; } CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); CGContextRef ctx = CGBitmapContextCreate(0, size, size, 8, size * 4, colorSpace, kCGImageAlphaPremultipliedLast); CGAffineTransform translateTransform = CGAffineTransformMakeTranslation(0, -size); CGAffineTransform scaleTransform = CGAffineTransformMakeScale(1, -1); CGContextConcatCTM(ctx, CGAffineTransformConcat(translateTransform, scaleTransform)); [QRCodeGenerator drawQRCode:code context:ctx size:size withPointType:0 withPositionType:0 withColor:color]; CGImageRef qrCGImage = CGBitmapContextCreateImage(ctx); UIImage * qrImage = [UIImage imageWithCGImage:qrCGImage]; if(topimg) { UIGraphicsBeginImageContext(qrImage.size); [qrImage drawInRect:CGRectMake(0, 0, qrImage.size.width, qrImage.size.height)]; float r=qrImage.size.width*35/240; [topimg drawInRect:CGRectMake((qrImage.size.width-r)/2, (qrImage.size.height-r)/2 ,r, r)];
qrImage=UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); }
CGContextRelease(ctx); CGImageRelease(qrCGImage); CGColorSpaceRelease(colorSpace); QRcode_free(code); return qrImage; } + (void)drawQRCode:(QRcode *)code context:(CGContextRef)ctx size:(CGFloat)size withPointType:(QRPointType)pointType withPositionType:(QRPositionType)positionType withColor:(UIColor *)color { 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); const CGFloat *components; if (color) { components = CGColorGetComponents(color.CGColor); }else { components = CGColorGetComponents([UIColor blackColor].CGColor); } CGContextSetRGBFillColor(ctx, components[0], components[1], components[2], 1.0); NSLog(@"aad :%f bbd :%f ccd:%f",components[0],components[1],components[2]); 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); if (positionType == QRPositionNormal) { switch (pointType) { case QRPointRect: CGContextAddRect(ctx, rectDraw); break; case QRPointRound: CGContextAddEllipseInRect(ctx, rectDraw); break; default: break; } }else if(positionType == QRPositionRound) { switch (pointType) { case QRPointRect: CGContextAddRect(ctx, rectDraw); break; case QRPointRound: if ((i>=0 && i<=6 && j>=0 && j<=6) || (i>=0 && i<=6 && j>=width-7-1 && j<=width-1) || (i>=width-7-1 && i<=width-1 && j>=0 && j<=6)) { CGContextAddRect(ctx, rectDraw); }else { CGContextAddEllipseInRect(ctx, rectDraw); } break; default: break; } } } ++data; } } CGContextFillPath(ctx); }
|