目前在Objective-C中,我使用Zbar(http://zbar.sourceforge.net/)来生成&阅读QR码。现在,我想继续开发Swift开发,是否有任何'如何'或关于在Swift中生成和读取QR码的库?
答案 0 :(得分:8)
这是我写的用于在swift中生成QRcode的代码。
//MARK:- generate QR code
func generateQRImage(stringQR:NSString, withSizeRate rate:CGFloat) -> UIImage
{
var filter:CIFilter = CIFilter(name:"CIQRCodeGenerator")
filter.setDefaults()
var data:NSData = stringQR.dataUsingEncoding(NSUTF8StringEncoding)!
filter.setValue(data, forKey: "inputMessage")
var outputImg:CIImage = filter.outputImage
var context:CIContext = CIContext(options: nil)
var cgimg:CGImageRef = context.createCGImage(outputImg, fromRect: outputImg.extent())
var img:UIImage = UIImage(CGImage: cgimg, scale: 1.0, orientation: UIImageOrientation.Up)!
var width = img.size.width * rate
var height = img.size.height * rate
UIGraphicsBeginImageContext(CGSizeMake(width, height))
var cgContxt:CGContextRef = UIGraphicsGetCurrentContext()
CGContextSetInterpolationQuality(cgContxt, kCGInterpolationNone)
img.drawInRect(CGRectMake(0, 0, width, height))
img = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return img
}
答案 1 :(得分:6)
从iOS 7开始,iOS设备可以读取QR码。但是,没有内置的方法来生成QR码。这是一个快速而肮脏的QR码阅读器。
首先,import AVFoundation
并添加AVCaptureMetadataOutputObjectsDelegate
接下来,设置捕获会话:
func captureQRCode() {
let session = AVCaptureSession()
let device = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo)
let input = AVCaptureDeviceInput.deviceInputWithDevice(device, error: nil) as AVCaptureDeviceInput
session.addInput(input)
let output = AVCaptureMetadataOutput()
output.setMetadataObjectsDelegate(self, queue: dispatch_get_main_queue())
session.addOutput(output)
output.metadataObjectTypes = [AVMetadataObjectTypeQRCode]
let previewLayer = AVCaptureVideoPreviewLayer(session: session)
let bounds = self.view.layer.bounds
previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill
previewLayer.bounds = bounds
previewLayer.position = CGPointMake(CGRectGetMidX(bounds), CGRectGetMidY(bounds))
self.view.layer.addSublayer(previewLayer)
session.startRunning()
}
最后,处理委托
func captureOutput(captureOutput: AVCaptureOutput!, didOutputMetadataObjects metadataObjects: [AnyObject]!, fromConnection connection: AVCaptureConnection!) {
for item in metadataObjects {
if let metadataObject = item as? AVMetadataMachineReadableCodeObject {
if metadataObject.type == AVMetadataObjectTypeQRCode {
println("QR Code: \(metadataObject.stringValue)")
}
}
}
}
答案 2 :(得分:2)
以下是我在Objective-C中生成QRcode的代码:
#pragma mark - qrimage
-(UIImage *)generateQRimg:(NSString *)a withsize:(CGFloat)b
{
CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
[filter setDefaults];
NSData *data = [a dataUsingEncoding:NSUTF8StringEncoding];
[filter setValue:data forKey:@"inputMessage"];
CIImage *outputImage = [filter outputImage];
CIContext *context = [CIContext contextWithOptions:nil];
CGImageRef cgImage = [context createCGImage:outputImage
fromRect:[outputImage extent]];
UIImage *image = [UIImage imageWithCGImage:cgImage
scale:1.
orientation:UIImageOrientationUp];
// Resize without interpolating
UIImage *resized = [self resizeImage:image
withQuality:kCGInterpolationNone
rate:b];
CGImageRelease(cgImage);
return resized;
}
- (UIImage *)resizeImage:(UIImage *)image withQuality (CGInterpolationQuality)quality rate:(CGFloat)rate
{
UIImage *resized = nil;
CGFloat width = image.size.width * rate;
CGFloat height = image.size.height * rate;
UIGraphicsBeginImageContext(CGSizeMake(width, height));
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetInterpolationQuality(context, quality);
[image drawInRect:CGRectMake(0, 0, width, height)];
resized = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return resized;
}
答案 3 :(得分:2)
这个GitHub项目提供了一些如何生成QR码的代码:
https://github.com/aschuch/QRCode
// NSData
let data = "http://schuch.me".dataUsingEncoding(NSISOLatin1StringEncoding)
let qrCode = QRCode(data)
qrCode.image
至于阅读,这里有另一个好的GitHub项目:
https://github.com/yannickl/QRCodeReader.swift
// Good practice: create the reader lazily to avoid cpu overload during the
// initialization and each time we need to scan a QRCode
lazy var reader = QRCodeReaderViewController(metadataObjectTypes: [AVMetadataObjectTypeQRCode])
@IBAction func scanAction(sender: AnyObject) {
// Retrieve the QRCode content
// By using the delegate pattern
reader.delegate = self
// Or by using the closure pattern
reader.completionBlock = { (result: String?) in
println(result)
}
// Presents the reader as modal form sheet
reader.modalPresentationStyle = .FormSheet
presentViewController(reader, animated: true, completion: nil)
}
// MARK: - QRCodeReader Delegate Methods
func reader(reader: QRCodeReader, didScanResult result: String) {
self.dismissViewControllerAnimated(true, completion: nil)
}
func readerDidCancel(reader: QRCodeReader) {
self.dismissViewControllerAnimated(true, completion: nil)
}
我建议您查看这两个项目并合并代码以实现您想要的行为。
答案 4 :(得分:2)
Swift中的QR码生成器:
viewDidLoad
中的:
var qrcodeImage: CIImage!
@IBOutlet var qrtext: UITextField!
@IBOutlet var imgQRCode: UIImageView!
@IBAction func displayQRcode(sender: AnyObject) {
if qrcodeImage == nil {
if qrtext.text == "" {
return
}
let data = qrtext.text!.dataUsingEncoding(NSISOLatin1StringEncoding, allowLossyConversion: false)
let filter = CIFilter(name: "CIQRCodeGenerator")
filter!.setValue(data, forKey: "inputMessage")
filter!.setValue("Q", forKey: "inputCorrectionLevel")
qrcodeImage = filter!.outputImage
qrtext.resignFirstResponder()
displayQRCodeImage()
}
else {
self.imgQRCode.image = nil
self.qrcodeImage = nil
}
}
func displayQRCodeImage() {
let scaleX = imgQRCode.frame.size.width / qrcodeImage.extent.size.width
let scaleY = imgQRCode.frame.size.height / qrcodeImage.extent.size.height
let transformedImage = qrcodeImage.imageByApplyingTransform(CGAffineTransformMakeScale(scaleX, scaleY))
imgQRCode.image = UIImage(CIImage: transformedImage)
}
适用于QRCode阅读器
答案 5 :(得分:1)
SWIFT 3: 二维码阅读器
第一步:只需在 info.plist 中添加一行,然后在新创建的行中输入隐私权 - CameraUsageDescription 并添加字符串即表示用于通知用户您的应用程序中需要访问相机的原因。
STEP SECOND:在ViewController类中使用此代码
class QrCodeScannerViewController: UIViewController, AVCaptureMetadataOutputObjectsDelegate {
var calssName:String = "QrCodeScannerViewController"
var captureSession:AVCaptureSession?
var videoPreviewLayer:AVCaptureVideoPreviewLayer?
var qrCodeFrameView:UIView?
@IBOutlet weak var messageLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
captureQRCode()
}
/* Open camera to capture QR CODE */
func captureQRCode() {
captureSession = AVCaptureSession()
let device = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo)
let input = try! AVCaptureDeviceInput(device: device) as AVCaptureDeviceInput
captureSession?.addInput(input)
let output = AVCaptureMetadataOutput()
output.setMetadataObjectsDelegate(self, queue: DispatchQueue.main)
captureSession?.addOutput(output)
output.metadataObjectTypes = [AVMetadataObjectTypeQRCode]
videoPreviewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
let bounds = self.view.layer.bounds
videoPreviewLayer?.videoGravity = AVLayerVideoGravityResizeAspectFill
videoPreviewLayer?.bounds = bounds
videoPreviewLayer?.position = CGPoint(x:bounds.midX, y:bounds.midY)
self.view.layer.addSublayer(videoPreviewLayer!)
captureSession?.startRunning()
}
/* onAtivityResult from App Delegate */
func captureOutput(_ captureOutput: AVCaptureOutput!,
didOutputMetadataObjects metadataObjects: [Any]!,
from connection: AVCaptureConnection!) {
for item in metadataObjects {
if let metadataObject = item as? AVMetadataMachineReadableCodeObject {
if metadataObject.type == AVMetadataObjectTypeQRCode {
print("QR Code: \(metadataObject.stringValue)")
hideCamera(voucherHashkey: metadataObject.stringValue)
}
}
}
}
/* Hide camera after getting result */
func hideCamera(voucherHashkey:String){
self.captureSession?.stopRunning()
self.videoPreviewLayer?.removeFromSuperlayer()
self.videoPreviewLayer = nil;
self.captureSession = nil;
// sendVocherDataToServer(voucherHashKey: voucherHashkey)
}
}
答案 6 :(得分:0)
考虑到startRunning()
{{1}} {{1}}是阻止呼叫和其他详细信息的事实,可以在此处找到快速而肮脏的快速实施QR码阅读器:
https://bitbucket.org/snippets/stefanpilger/gE98R/qr-code-reader-class-swift