Swift Objective-c 原生api 二维码条形码的扫描
来源:互联网 发布:苹果mac浏览器如何收藏 编辑:程序博客网 时间:2024/06/05 06:03
swift代码如下:
import UIKit
import AVFoundation
class QRCodeViewController: BaseViewController, AVCaptureMetadataOutputObjectsDelegate {
//用于处理采集信息的代理
private var titleLabel = UILabel()
private var captureSession: AVCaptureSession? //输入输出的中间桥梁
private var videoPreviewLayer: AVCaptureVideoPreviewLayer?
private var animationLineView = UIImageView()
private var timer: NSTimer?
override func viewDidLoad() {
super.viewDidLoad()
buildNavigationItem()
buildInputAVCaptureDevice()
buildFrameImageView()
buildTitleLabel()
buildAnimationLineView()
}
override func viewWillDisappear(animated: Bool) {
super.viewWillDisappear(animated)
if timer != nil {
timer!.invalidate()
timer = nil
}
}
// MARK: - Build UI
private func buildNavigationItem() {
navigationItem.title = "店铺二维码"
navigationController?.navigationBar.barTintColor = LFBNavigationBarWhiteBackgroundColor
}
private func buildTitleLabel() {
titleLabel.textColor = UIColor.whiteColor()
titleLabel.font = UIFont.systemFontOfSize(16)
titleLabel.frame = CGRectMake(0, 340, ScreenWidth, 30)
titleLabel.textAlignment = NSTextAlignment.Center
view.addSubview(titleLabel)
}
private func buildInputAVCaptureDevice() {
//获取摄像设备
let captureDevice = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo)
titleLabel.text = "将店铺二维码对准方块内"
//创建输入流
let input = try? AVCaptureDeviceInput(device: captureDevice)
if input == nil {
titleLabel.text = "没有摄像头换真机试试"
return
}
let captureMetadataOutput = AVCaptureMetadataOutput()
//初始化链接对象
captureSession = AVCaptureSession()
captureSession?.addInput(input!)
captureSession?.addOutput(captureMetadataOutput)
let dispatchQueue = dispatch_queue_create("myQueue", nil)
//设置代理 在主线程里刷新
captureMetadataOutput.setMetadataObjectsDelegate(self, queue: dispatchQueue)
captureMetadataOutput.metadataObjectTypes = [AVMetadataObjectTypeQRCode, AVMetadataObjectTypeAztecCode]
//设置扫码支持的编码格式
videoPreviewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
videoPreviewLayer?.videoGravity = AVLayerVideoGravityResizeAspectFill
videoPreviewLayer?.frame = view.layer.frame
view.layer.addSublayer(videoPreviewLayer!)
captureMetadataOutput.rectOfInterest = CGRectMake(0, 0, 1, 1)
//开始捕获
captureSession?.startRunning()
}
private func buildFrameImageView() {
let lineT = [CGRectMake(0, 0, ScreenWidth, 100),
CGRectMake(0, 100, ScreenWidth * 0.2, ScreenWidth * 0.6),
CGRectMake(0, 100 + ScreenWidth * 0.6, ScreenWidth, ScreenHeight - 100 - ScreenWidth * 0.6),
CGRectMake(ScreenWidth * 0.8, 100, ScreenWidth * 0.2, ScreenWidth * 0.6)]
for lineTFrame in lineT {
buildTransparentView(lineTFrame)
}
let lineR = [CGRectMake(ScreenWidth * 0.2, 100, ScreenWidth * 0.6, 2),
CGRectMake(ScreenWidth * 0.2, 100, 2, ScreenWidth * 0.6),
CGRectMake(ScreenWidth * 0.8 - 2, 100, 2, ScreenWidth * 0.6),
CGRectMake(ScreenWidth * 0.2, 100 + ScreenWidth * 0.6, ScreenWidth * 0.6, 2)]
for lineFrame in lineR {
buildLineView(lineFrame)
}
let yellowHeight: CGFloat = 4
let yellowWidth: CGFloat = 30
let yellowX: CGFloat = ScreenWidth * 0.2
let bottomY: CGFloat = 100 + ScreenWidth * 0.6
let lineY = [CGRectMake(yellowX, 100, yellowWidth, yellowHeight),
CGRectMake(yellowX, 100, yellowHeight, yellowWidth),
CGRectMake(ScreenWidth * 0.8 - yellowHeight, 100, yellowHeight, yellowWidth),
CGRectMake(ScreenWidth * 0.8 - yellowWidth, 100, yellowWidth, yellowHeight),
CGRectMake(yellowX, bottomY - yellowHeight + 2, yellowWidth, yellowHeight),
CGRectMake(ScreenWidth * 0.8 - yellowWidth, bottomY - yellowHeight + 2, yellowWidth, yellowHeight),
CGRectMake(yellowX, bottomY - yellowWidth, yellowHeight, yellowWidth),
CGRectMake(ScreenWidth * 0.8 - yellowHeight, bottomY - yellowWidth, yellowHeight, yellowWidth)]
for yellowRect in lineY {
buildYellowLineView(yellowRect)
}
}
private func buildLineView(frame: CGRect) {
let view1 = UIView(frame: frame)
view1.backgroundColor = UIColor.colorWithCustom(230, g: 230, b: 230)
view.addSubview(view1)
}
private func buildYellowLineView(frame: CGRect) {
let yellowView = UIView(frame: frame)
yellowView.backgroundColor = LFBNavigationYellowColor
view.addSubview(yellowView)
}
private func buildTransparentView(frame: CGRect) {
let tView = UIView(frame: frame)
tView.backgroundColor = UIColor.blackColor()
tView.alpha = 0.5
view.addSubview(tView)
}
private func buildAnimationLineView() {
animationLineView.image = UIImage(named: "yellowlight")
view.addSubview(animationLineView)
timer = NSTimer(timeInterval: 2.5, target: self, selector: "startYellowViewAnimation", userInfo: nil, repeats: true)
let runloop = NSRunLoop.currentRunLoop()
runloop.addTimer(timer!, forMode: NSRunLoopCommonModes)
timer!.fire()
}
func startYellowViewAnimation() {
weak var weakSelf = self
animationLineView.frame = CGRectMake(ScreenWidth * 0.2 + ScreenWidth * 0.1 * 0.5, 100, ScreenWidth * 0.5, 20)
UIView.animateWithDuration(2.5) { () -> Void in
weakSelf!.animationLineView.frame.origin.y += ScreenWidth * 0.55
}
}
}
oc代码如下:@interface ViewController ()<AVCaptureMetadataOutputObjectsDelegate>//用于处理采集信息的代理{ AVCaptureSession * session;//输入输出的中间桥梁}@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. //获取摄像设备 AVCaptureDevice * device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; //创建输入流 AVCaptureDeviceInput * input = [AVCaptureDeviceInput deviceInputWithDevice:device error:nil]; //创建输出流 AVCaptureMetadataOutput * output = [[AVCaptureMetadataOutput alloc]init]; //设置代理 在主线程里刷新 [output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()]; //初始化链接对象 session = [[AVCaptureSession alloc]init]; //高质量采集率 [session setSessionPreset:AVCaptureSessionPresetHigh]; [session addInput:input]; [session addOutput:output]; //设置扫码支持的编码格式(如下设置条形码和二维码兼容) output.metadataObjectTypes=@[AVMetadataObjectTypeQRCode,AVMetadataObjectTypeEAN13Code, AVMetadataObjectTypeEAN8Code, AVMetadataObjectTypeCode128Code]; AVCaptureVideoPreviewLayer * layer = [AVCaptureVideoPreviewLayer layerWithSession:session]; layer.videoGravity=AVLayerVideoGravityResizeAspectFill; layer.frame=self.view.layer.bounds; [self.view.layer insertSublayer:layer atIndex:0]; //开始捕获 [session startRunning];}之后我们的UI上已经可以看到摄像头捕获的内容,只要实现代理中的方法,就可以完成二维码条形码的扫描:-(void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection{ if (metadataObjects.count>0) { //[session stopRunning]; AVMetadataMachineReadableCodeObject * metadataObject = [metadataObjects objectAtIndex : 0 ]; //输出扫描字符串 NSLog(@"%@",metadataObject.stringValue); }}
- Swift Objective-c 原生api 二维码条形码的扫描
- 使用IOS7原生API进行二维码条形码的扫描
- 使用IOS7原生API进行二维码条形码的扫描
- 使用IOS7原生API进行二维码条形码的扫描
- IOS7使用原生API进行二维码和条形码的扫描
- 使用IOS7原生API进行二维码条形码的扫描
- 使用IOS7原生API进行二维码条形码的扫描
- IOS7原生API进行二维码条形码的扫描
- IOS7使用原生API进行二维码和条形码的扫描
- 使用IOS7原生API进行二维码条形码的扫描
- 使用苹果原生API进行二维码和条形码的扫描
- 使用苹果原生API进行二维码和条形码的扫描
- 使用IOS7原生API进行二维码条形码的扫描
- 使用 IOS7 原生API进行二维码条形码的扫描
- iOS 原生的二维码/条形码扫描
- iOS原生二维码生成与扫描以及条形码的扫描
- 【iOS】AVFoundation架构下的原生二维码和条形码扫描
- 二维码、条形码的扫描
- 返回顶部小火箭
- PagerAdapter滑动页面,onpagechangelistener监听滑动
- 获取文件类型和文件名
- opencv 人脸识别 java版------2
- 创建一个规范的iOS工程(xcode8新特性)
- Swift Objective-c 原生api 二维码条形码的扫描
- javascript函数节流 keyup
- 文献阅读(7)
- Velocity China 2016 Web 性能与运维大会相约北京
- 关于摄像头的一些基础知识
- iOS中Autolayout-Autoresizing与Autolayout
- mysql分布式数据库中间件对比
- 使用GPU计算骨骼动画(二)我的实践总结
- ANDROID PAGERADAPTER的使用方法_安卓实现左右滑动指引效果