一個(gè)Objective-C的github 系統(tǒng)自帶二維碼條形碼的掃描和生成及圖片解析二維碼(上線App Store的開源項(xiàng)目)
- 可以定制掃描框和字體圖標(biāo)的顏色
- 可以定制生成二維碼的前景色和背景色哦
- 另外還支持橫豎屏切換哦样眠,是不是很激爽
- 其實(shí)還是支持中文素跺,英文逮壁,繁體中文的哦
- 支持創(chuàng)建各種二維碼
- 3D-Touch的icon及圖片和列表按壓支持
- 隨時(shí)隨地的系統(tǒng)分享功能
- 每次生成掃描都記錄拗军,隨時(shí)刪除單個(gè)或全部
- storyboard的界面方式政冻,可供新手學(xué)習(xí)牢硅,autolayout和舊版UIViewAutoresizing的約束方法
- 除了FMDB蒙谓,沒有接入任何第三方庫。
功能介紹
1.二維碼條形碼掃描 (一個(gè)block搞定是不是很酸爽)
FanQRCodeScanViewController *qrCoreVC=[[FanQRCodeScanViewController alloc]initWithQRBlock:^(NSString *resultSrt,NSString *type, BOOL isSuccess) {
if (isSuccess) {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self fan_showAlertWithTitle:@"掃描結(jié)果" message:resultSrt];
});
NSLog(@"%@",resultSrt);
}else{
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self fan_showAlertWithTitle:@"掃描失敗" message:resultSrt];
});
NSLog(@"關(guān)閉或失敗:%@",resultSrt);
}
}];
qrCoreVC.themColor=[UIColor redColor];
qrCoreVC.scanColor=[UIColor yellowColor];
[self presentViewController:qrCoreVC animated:YES completion:nil];
2.二維碼生成
//二維碼調(diào)用
UIImage *image=[FanQRCodeScanViewController fan_qrCodeImageWithText:textField.text size:CGSizeMake(300, 300)];
#pragma mark - 二維碼生成
+(UIImage *)fan_qrCodeImageWithText:(NSString *)text size:(CGSize)size{
return [[self class] fan_qrCodeImageWithText:text size:size color:[UIColor blackColor] bgColor:[UIColor whiteColor]];
}
+(UIImage *)fan_qrCodeImageWithText:(NSString *)text size:(CGSize)size color:(UIColor *)color bgColor:(UIColor *)bgColor{
NSData *stringData = [text dataUsingEncoding: NSUTF8StringEncoding];
//生成
CIFilter *qrFilter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
[qrFilter setValue:stringData forKey:@"inputMessage"];
[qrFilter setValue:@"M" forKey:@"inputCorrectionLevel"];
UIColor *onColor = color;
UIColor *offColor = bgColor;
//上色
CIFilter *colorFilter = [CIFilter filterWithName:@"CIFalseColor"
keysAndValues:
@"inputImage",qrFilter.outputImage,
@"inputColor0",[CIColor colorWithCGColor:onColor.CGColor],
@"inputColor1",[CIColor colorWithCGColor:offColor.CGColor],
nil];
CIImage *qrImage = colorFilter.outputImage;
//繪制
CGImageRef cgImage = [[CIContext contextWithOptions:nil] createCGImage:qrImage fromRect:qrImage.extent];
UIGraphicsBeginImageContext(size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetInterpolationQuality(context, kCGInterpolationNone);
CGContextScaleCTM(context, 1.0, -1.0);
CGContextDrawImage(context, CGContextGetClipBoundingBox(context), cgImage);
if (cgImage==nil) {
return nil;
}
UIImage *codeImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
CGImageRelease(cgImage);
return codeImage;
}
3.條形碼生成
//條形碼調(diào)用
UIImage *image=[FanQRCodeScanViewController fan_generateBarImageWithCode:textField.text size:CGSizeMake(300, 160)];
//條形碼生成
+ (UIImage *)fan_generateBarImageWithCode:(NSString *)code size:(CGSize)size{
//生成條形碼
CIImage *barcodeImage;
//NSISOLatin1StringEncoding
NSData *data = [code dataUsingEncoding:NSISOLatin1StringEncoding allowLossyConversion:false];
CIFilter *filter = [CIFilter filterWithName:@"CICode128BarcodeGenerator"];
[filter setValue:data forKey:@"inputMessage"];
barcodeImage = [filter outputImage];
//消除模糊(此種方法,得到的圖片不能保存到相冊)
// CGFloat scaleX = size.width / barcodeImage.extent.size.width;// extent 返回圖片的frame
// CGFloat scaleY = size.height / barcodeImage.extent.size.height;
// CIImage *transformedImage = [barcodeImage imageByApplyingTransform:CGAffineTransformScale(CGAffineTransformIdentity, scaleX, scaleY)];
// return [UIImage imageWithCIImage:transformedImage];
//用繪制方法(可以保存到相冊)不知道什么原因
CGImageRef cgImage = [[CIContext contextWithOptions:nil] createCGImage:barcodeImage fromRect:barcodeImage.extent];
UIGraphicsBeginImageContext(size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetInterpolationQuality(context, kCGInterpolationNone);
CGContextScaleCTM(context, 1.0, -1.0);
CGContextDrawImage(context, CGContextGetClipBoundingBox(context), cgImage);
if (cgImage==nil) {
return nil;
}
UIImage *codeImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
CGImageRelease(cgImage);
return codeImage;
}
Like(喜歡)
有問題請直接在文章下面留言,喜歡就給個(gè)Star(小星星)吧!
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者