ios swift3.0實現(xiàn)二維碼掃描、生成、識別示例代碼
基于swift3.0
1.掃描二維碼

設(shè)置掃描會話,圖層和輸入輸出
//設(shè)置捕捉設(shè)備
let device = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo)
do
{
//設(shè)置設(shè)備輸入輸出
let input = try AVCaptureDeviceInput(device: device)
let output = AVCaptureMetadataOutput()
output.setMetadataObjectsDelegate(self, queue: DispatchQueue.main)
//設(shè)置會話
let scanSession = AVCaptureSession()
scanSession.canSetSessionPreset(AVCaptureSessionPresetHigh)
if scanSession.canAddInput(input)
{
scanSession.addInput(input)
}
if scanSession.canAddOutput(output)
{
scanSession.addOutput(output)
}
//設(shè)置掃描類型(二維碼和條形碼)
output.metadataObjectTypes = [
AVMetadataObjectTypeQRCode,
AVMetadataObjectTypeCode39Code,
AVMetadataObjectTypeCode128Code,
AVMetadataObjectTypeCode39Mod43Code,
AVMetadataObjectTypeEAN13Code,
AVMetadataObjectTypeEAN8Code,
AVMetadataObjectTypeCode93Code]
//預(yù)覽圖層
let scanPreviewLayer = AVCaptureVideoPreviewLayer(session:scanSession)
scanPreviewLayer?.videoGravity = AVLayerVideoGravityResizeAspectFill
scanPreviewLayer?.frame = view.layer.bounds
view.layer.insertSublayer(scanPreviewLayer!, at: 0)
//自動對焦
if (device?.isFocusModeSupported(.autoFocus))!
{
do { try input.device.lockForConfiguration() } catch{ }
input.device.focusMode = .autoFocus
input.device.unlockForConfiguration()
}
//設(shè)置掃描區(qū)域
NotificationCenter.default.addObserver(forName: NSNotification.Name.AVCaptureInputPortFormatDescriptionDidChange, object: nil, queue: nil, using: {[weak self] (noti) in
output.rectOfInterest = (scanPreviewLayer?.metadataOutputRectOfInterest(for: self!.scanPane.frame))!
})
//保存會話
self.scanSession = scanSession
}
catch
{
//攝像頭不可用
Tool.confirm(title: "溫馨提示", message: "攝像頭不可用", controller: self)
return
}
開始掃描
if !scanSession.isRunning
{
scanSession.startRunning()
}
掃描結(jié)果在代理方法中
//掃描捕捉完成
extension ScanCodeViewController : AVCaptureMetadataOutputObjectsDelegate
{
func captureOutput(_ captureOutput: AVCaptureOutput!, didOutputMetadataObjects metadataObjects: [Any]!, from connection: AVCaptureConnection!)
{
//停止掃描
self.scanLine.layer.removeAllAnimations()
self.scanSession!.stopRunning()
//播放聲音
Tool.playAlertSound(sound: "noticeMusic.caf")
//掃完完成
if metadataObjects.count > 0
{
if let resultObj = metadataObjects.first as? AVMetadataMachineReadableCodeObject
{
Tool.confirm(title: "掃描結(jié)果", message: resultObj.stringValue, controller: self,handler: { (_) in
//繼續(xù)掃描
self.startScan()
})
}
}
}
}
2.二維碼生成

通過濾鏡生成CGImage
//2.二維碼濾鏡
let contentData = self.data(using: String.Encoding.utf8)
let fileter = CIFilter(name: "CIQRCodeGenerator")
fileter?.setValue(contentData, forKey: "inputMessage")
fileter?.setValue("H", forKey: "inputCorrectionLevel")
let ciImage = fileter?.outputImage
//3.顏色濾鏡
let colorFilter = CIFilter(name: "CIFalseColor")
colorFilter?.setValue(ciImage, forKey: "inputImage")
colorFilter?.setValue(CIColor(cgColor: QRCodeColor.cgColor), forKey: "inputColor0")// 二維碼顏色
colorFilter?.setValue(CIColor(cgColor: QRCodeBgColor.cgColor), forKey: "inputColor1")// 背景色
//4.生成處理
let outImage = colorFilter!.outputImage
let scale = QRCodeSize / outImage!.extent.size.width;
let transform = CGAffineTransform(scaleX: scale, y: scale)
let transformImage = colorFilter!.outputImage!.applying(transform)
通過CGImage生成UIImage
let image = UIImage(ciImage: ciImage)
繪制Logo和邊框
// 繪制logo
UIGraphicsBeginImageContextWithOptions(image.size, false, UIScreen.main.scale)
image.draw(in: CGRect(x: 0, y: 0, width: image.size.width, height: image.size.height))
//線框
let logoBorderLineImagae = QRCodeLogo.getRoundRectImage(size: logoWidth, radius: radius, borderWidth: borderLineWidth, borderColor: borderLineColor)
//邊框
let logoBorderImagae = logoBorderLineImagae.getRoundRectImage(size: logoWidth, radius: radius, borderWidth: boderWidth, borderColor: borderColor)
logoBorderImagae.draw(in: logoFrame)
let QRCodeImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
封裝接口:
/**
1.生成二維碼
- returns: 黑白普通二維碼(大小為300)
*/
func generateQRCode() -> UIImage
/**
2.生成二維碼
- parameter size: 大小
- returns: 生成帶大小參數(shù)的黑白普通二維碼
*/
func generateQRCodeWithSize(size:CGFloat?) -> UIImage
/**
3.生成二維碼
- parameter logo: 圖標(biāo)
- returns: 生成帶Logo二維碼(大小:300)
*/
func generateQRCodeWithLogo(logo:UIImage?) -> UIImage
/**
4.生成二維碼
- parameter size: 大小
- parameter logo: 圖標(biāo)
- returns: 生成大小和Logo的二維碼
*/
func generateQRCode(size:CGFloat?,logo:UIImage?) -> UIImage
/**
5.生成二維碼
- parameter size: 大小
- parameter color: 顏色
- parameter bgColor: 背景顏色
- parameter logo: 圖標(biāo)
- returns: 帶Logo、顏色二維碼
*/
func generateQRCode(size:CGFloat?,color:UIColor?,bgColor:UIColor?,logo:UIImage?) -> UIImage
/**
6.生成二維碼
- parameter size: 大小
- parameter color: 顏色
- parameter bgColor: 背景顏色
- parameter logo: 圖標(biāo)
- parameter radius: 圓角
- parameter borderLineWidth: 線寬
- parameter borderLineColor: 線顏色
- parameter boderWidth: 帶寬
- parameter borderColor: 帶顏色
- returns: 自定義二維碼
*/
func generateQRCode(size:CGFloat?,color:UIColor?,bgColor:UIColor?,logo:UIImage?,radius:CGFloat,borderLineWidth:CGFloat?,borderLineColor:UIColor?,boderWidth:CGFloat?,borderColor:UIColor?) -> UIImage
使用
DispatchQueue.global().async {
let image = content.generateQRCodeWithLogo(logo: self.logoImageView.image)
DispatchQueue.main.async(execute: {
self.QRCodeImageView.image = image
})
}
3.識別二維碼

通過CIDetector識別二維碼
CIDetector用于分析CIImage,以得到CIFeature,每個CIDetector都要用一個探測器類型(NSString)來初始化。這個類型用于告訴探測器要找什么特征
1.識別圖片二維碼
func recognizeQRCode() -> String?
{
let detector = CIDetector(ofType: CIDetectorTypeQRCode, context: nil, options: [CIDetectorAccuracy : CIDetectorAccuracyHigh])
let features = detector?.features(in: CoreImage.CIImage(cgImage: self.cgImage!))
guard (features?.count)! > 0 else { return nil }
let feature = features?.first as? CIQRCodeFeature
return feature?.messageString
}
使用實例
DispatchQueue.global().async {
let recognizeResult = self.sourceImage?.recognizeQRCode()
let result = recognizeResult?.characters.count > 0 ? recognizeResult : "無法識別"
DispatchQueue.main.async {
Tool.confirm(title: "掃描結(jié)果", message: result, controller: self)
self.activityIndicatoryView.stopAnimating()
}
}
本文Demo地址:QRCode_jb51.rar
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
OC runtime學(xué)習(xí)筆記之關(guān)聯(lián)對象
這篇文章主要介紹了OC runtime學(xué)習(xí)筆記之關(guān)聯(lián)對象的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-09-09
iOS開發(fā)中使用UIScrollView實現(xiàn)無限循環(huán)的圖片瀏覽器
這篇文章主要介紹了iOS開發(fā)中使用UIScrollView實現(xiàn)無限循環(huán)的圖片瀏覽器的方法,感興趣的小伙伴們可以參考一下2016-03-03
iOS CoreTelephony 實現(xiàn)監(jiān)聽通話狀態(tài)
這篇文章主要介紹了iOS CoreTelephony 實現(xiàn)監(jiān)聽通話狀態(tài) 的相關(guān)資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-07-07
iOS頁面跳轉(zhuǎn)及數(shù)據(jù)傳遞(三種)
本文主要介紹了iOS頁面跳轉(zhuǎn)的三種方法及數(shù)據(jù)傳遞的方法。具有很好的參考價值。下面跟著小編一起來看下吧2017-03-03
Flutter之PageView頁面緩存與KeepAlive
這篇文章主要為大家介紹了Flutter之PageView頁面緩存與KeepAlive示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10
iOS中監(jiān)聽UITextField值改變事件的方法實例
UITextField 是一個用來處理文本輸入和現(xiàn)實的控件,在我們的開發(fā)當(dāng)中也是經(jīng)常被用到。下面這篇文章主要給大家介紹了關(guān)于iOS中監(jiān)聽UITextField值改變事件的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2018-07-07
IOS 基礎(chǔ)之設(shè)置 tableview 的分割線
這篇文章主要介紹了IOS 基礎(chǔ)之設(shè)置 tableview 的分割線的相關(guān)資料,需要的朋友可以參考下2017-03-03

