iOS Swift条形码阅读

我的条形码扫描工作在我的应用程序中.检测到条形码后,我停止捕获会话以允许处理条形码.但是,在处理完条码后,我希望扫描控制器保持原状并扫描下一个条形码.我曾假设开始捕获会话(startRunning())会这样做,但图像保持冻结状态.如何再次启动捕获会话?

解决方法

要停止会话,请使用此代码
self.session.stopRunning()

要开始使用agian,请使用此代码

self.session.startRunning()

以下是实现条形码扫描器的所有代码……

import UIKit
import AVFoundation


class ViewController: UIViewController,AVCaptureMetadataOutputObjectsDelegate {

    let session         : AVCaptureSession = AVCaptureSession()
    var previewLayer    : AVCaptureVideoPreviewLayer!
    var highlightView   : UIView = UIView()

    override func viewDidLoad() {
        super.viewDidLoad()

        // Allow the view to resize freely
        self.highlightView.autoresizingMask =   UIViewAutoresizing.FlexibletopMargin |
            UIViewAutoresizing.FlexibleBottomMargin |
            UIViewAutoresizing.FlexibleLeftMargin |
            UIViewAutoresizing.FlexibleRightMargin

        // Select the color you want for the completed scan reticle
        self.highlightView.layer.borderColor = UIColor.greenColor().CGColor
        self.highlightView.layer.borderWidth = 3

        // Add it to our controller's view as a subview.
        self.view.addSubview(self.highlightView)


        // For the sake of discussion this is the camera
        let device = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo)

        // Create a nilable NSError to hand off to the next method.
        // Make sure to use the "var" keyword and not "let"
        var error : NSError? = nil


        let input : AVCaptureDeviceInput? = AVCaptureDeviceInput.deviceInputWithDevice(device,error: &error) as? AVCaptureDeviceInput

        // If our input is not nil then add it to the session,otherwise we're kind of done!
        if input != nil {
            session.addInput(input)
        }
        else {
            // This is fine for a demo,do something real with this in your app. :)
            println(error)
        }

        let output = AVCaptureMetadataOutput()
        output.setMetadataObjectsDelegate(self,queue: dispatch_get_main_queue())
        session.addOutput(output)
        output.MetadataObjectTypes = output.availableMetadataObjectTypes


        previewLayer = AVCaptureVideoPreviewLayer.layerWithSession(session) as! AVCaptureVideoPreviewLayer
        previewLayer.frame = self.view.bounds
        previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill
        self.view.layer.addSublayer(previewLayer)

        // Start the scanner. You'll have to end it yourself later.
        session.startRunning()

    }

    // This is called when we find a kNown barcode type with the camera.
    func captureOutput(captureOutput: AVCaptureOutput!,didOutputMetadataObjects MetadataObjects: [AnyObject]!,fromConnection connection: AVCaptureConnection!) {

        var highlightViewRect = CGRectZero

        var barCodeObject : AVMetadataObject!

        var detectionString : String!

        let barCodeTypes = [AVMetadataObjectTypeUPCECode,AVMetadataObjectTypeCode39Code,AVMetadataObjectTypeCode39Mod43Code,AVMetadataObjectTypeEAN13Code,AVMetadataObjectTypeEAN8Code,AVMetadataObjectTypeCode93Code,AVMetadataObjectTypeCode128Code,AVMetadataObjectTypePDF417Code,AVMetadataObjectTypeQRCode,AVMetadataObjectTypeAztecCode
        ]


        // The scanner is capable of capturing multiple 2-dimensional barcodes in one scan.
        for Metadata in MetadataObjects {

            for barcodeType in barCodeTypes {

                if Metadata.type == barcodeType {
                    barCodeObject = self.previewLayer.transformedMetadataObjectForMetadataObject(Metadata as! AVMetadataMachineReadableCodeObject)

                    highlightViewRect = barCodeObject.bounds

                    detectionString = (Metadata as! AVMetadataMachineReadableCodeObject).stringValue

                    self.session.stopRunning()

self.alert(detectionString)
                    break
                }

            }
        }

        println(detectionString)
        self.highlightView.frame = highlightViewRect
        self.view.bringSubviewToFront(self.highlightView)

    }



    func alert(Code: String){
        let actionSheet:UIAlertController = UIAlertController(title: "Barcode",message: "\(Code)",preferredStyle: UIAlertControllerStyle.Alert)

        // for alert add .Alert instead of .Action Sheet


        // start copy

        let firstAlertAction:UIAlertAction = UIAlertAction(title: "OK",style: UIAlertActionStyle.Default,handler:


            {
                (alertAction:UIAlertAction!) in


                // action when pressed

                self.session.startRunning()




        })

        actionSheet.addAction(firstAlertAction)

        // end copy






        self.presentViewController(actionSheet,animated: true,completion: nil)

    }



}

相关文章

当我们远离最新的 iOS 16 更新版本时,我们听到了困扰 Apple...
欧版/美版 特别说一下,美版选错了 可能会永久丧失4G,不过只...
一般在接外包的时候, 通常第三方需要安装你的app进行测...
前言为了让更多的人永远记住12月13日,各大厂都在这一天将应...