Swift_二维码、条形码的生成
2017-07-13 10:52
387 查看
Object-C对应功能实现
二维码的生成
二维码中心添加图片
条形码的生成
识别本地二维码图片
代码地址:
https://github.com/FlyingKuiKui/BarCodeAndORCode_Swift.git
二维码的生成
class func generateQRCode(messgae:NSString,width:CGFloat,height:CGFloat) -> UIImage { var returnImage:UIImage? if (messgae.length > 0 && width > 0 && height > 0){ let inputData = messgae.data(using: String.Encoding.utf8.rawValue)! as NSData // CIQRCodeGenerator let filter = CIFilter.init(name: "CIQRCodeGenerator")! filter.setValue(inputData, forKey: "inputMessage") var ciImage = filter.outputImage! let min = width > height ? height :width; let scaleX = min/ciImage.extent.size.width let scaleY = min/ciImage.extent.size.height ciImage = ciImage.applying(CGAffineTransform.init(scaleX: scaleX, y: scaleY)) returnImage = UIImage.init(ciImage: ciImage) }else { returnImage = nil; } return returnImage! }
二维码中心添加图片
class func generateQRCodeWithCenterImage(messgae:NSString,width:CGFloat,height:CGFloat,centerImage:UIImage) -> UIImage { let backImage = generateQRCode(messgae: messgae, width: width, height: height) UIGraphicsBeginImageContext(backImage.size); backImage.draw(in: CGRect.init(x: 0, y: 0, width: backImage.size.width, height: backImage.size.height)) let centerImageWH:CGFloat = backImage.size.width > backImage.size.height ? backImage.size.height * 0.2 : backImage.size.width*0.2 centerImage.draw(in: CGRect.init(x: (backImage.size.width - centerImageWH)*0.5, y: (backImage.size.height - centerImageWH)*0.5, width: centerImageWH, height: centerImageWH)) let returnImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return returnImage! }
条形码的生成
class func generateBarCode(messgae:NSString,width:CGFloat,height:CGFloat) -> UIImage { var returnImage:UIImage? if (messgae.length > 0 && width > 0 && height > 0){ let inputData:NSData? = messgae.data(using: String.Encoding.utf8.rawValue)! as NSData // CICode128BarcodeGenerator let filter = CIFilter.init(name: "CICode128BarcodeGenerator")! filter.setValue(inputData, forKey: "inputMessage") var ciImage = filter.outputImage! let scaleX = width/ciImage.extent.size.width let scaleY = height/ciImage.extent.size.height ciImage = ciImage.applying(CGAffineTransform.init(scaleX: scaleX, y: scaleY)) returnImage = UIImage.init(ciImage: ciImage) }else { returnImage = nil; } return returnImage! }
识别本地二维码图片
class func decodeQRCode(QRCodeImage:UIImage) -> NSString { var outputString:NSString? let detector:CIDetector = CIDetector.init(ofType: CIDetectorTypeQRCode, context: nil, options: [CIDetectorAccuracy:CIDetectorAccuracyHigh])! let image = CIImage.init(image: QRCodeImage)! let features = detector.features(in: image) as NSArray for feature in features { outputString = (feature as! CIQRCodeFeature).messageString! as NSString; } return outputString! }
代码地址:
https://github.com/FlyingKuiKui/BarCodeAndORCode_Swift.git
相关文章推荐
- swift之生成条形码、二维码、识别二维码、长按识别二维码
- C#利用ZXing.Net生成条形码和二维码
- android二维码条形码生成
- asp.net 生成、解析条形码和二维码
- Android 生成二维码 带LOGO的二维码 条形码
- Android基于Zxing实现二维码,条形码扫描和生成二维码
- 二维码、条形码生成
- iOS 花式二维码生成和二维码识别< Swift 版>
- C# 利用BarcodeLib.dll生成条形码(一维,zxing,QrCodeNet/dll二维码)
- ZXing生成条形码、二维码、带logo二维码
- C# 利用BarcodeLib.dll生成条形码(一维,zxing,QrCodeNet/dll二维码)
- zxing生成二维码或者条形码
- Android ZXing二维码、条形码的生成和解析
- java 生成条形码 或二维码
- iText生成条形码与二维码
- iOS开发中使用CIFilter生成二维码和条形码
- C# .Net 使用zxing.dll生成二维码,条形码
- Android基于Google Zxing实现二维码/条形码扫描、生成二维码/条形码
- java生成二维码、条形码和二维码、条形码的解码
- JavaScript生成条形码和二维码