用swift 写的相机手动对焦

//对焦十字

        focalView=UIView(frame: CGRectMake(10, 50, 200, 200))

       focalView.backgroundColor = UIColor.clearColor()

        self.overlayView.addSubview(focalView)

        //十字

        let line1 = UIView(frame:CGRectMake(0, 29.5, 60, 1))

        line1.backgroundColor = UIColor.whiteColor()

        focalView.addSubview(line1)

        let line2 = UIView(frame:CGRectMake(29.5, 0, 1, 60))

        line2.backgroundColor = UIColor.whiteColor()

        focalView.addSubview(line2)

        //默认隐藏

        focalView.hidden=true;

 

        //点击屏幕对焦的手势

        let tapGesture = UITapGestureRecognizer(target: self, action: "handleTapGesture:")

        [imagePickerController!.cameraOverlayView? .bringSubviewToFront(self.overlayView)];

        

        self.overlayView.addGestureRecognizer(tapGesture)

 

    }

    

    func threadInMainMethod(sender : AnyObject)

    {

        GPSLabel.text="GPS精度:中精度"

        directionPrecisionLabel.text = "方向精度:中精度"

        let appdelegate = UIApplication.sharedApplication().delegate as! AppDelegate

        latitudeLabel.text = NSString(format: "纬度:%f" , appdelegate.starpoint.x) as String

        longitudeLabel.text = NSString(format: "经度:%f" , appdelegate.starpoint.y) as String

        let receiver = VideoReceiver.getInstance()

        let wrapper = receiver.rotationWrapper

        let yaw = wrapper.yaw

        let roll = wrapper.roll

        let degree = (yaw - roll + 360)%360

        let direction = parseDirection(degree)

        directionLabel.text =  NSString(format: "方向:%@" , direction) as String

        

    }

         func handleTapGesture(sender: UITapGestureRecognizer){

           

            

        let location:CGPoint!  = sender.locationInView(self.overlayView)

                focusOnPoint(location, completionHandler: {

                        self.focalView.center = location

                        self.focalView.alpha = 0.0

                        self.focalView.hidden = false

                        UIView.animateWithDuration(1, animations: {

                           self.focalView.alpha = 1.0

                            }, completion: { (finished) in

                                self.focalView.alpha = 0.0

                        })

                    

                })

              

            }

            

     func focusOnPoint(_point: CGPoint, completionHandler: (Void) -> Void) {

        let device = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo)

        

        var pointOfInterest = CGPoint.zero

        let frameSize = self.overlayView.bounds.size

        pointOfInterest = CGPoint(x: _point.y / frameSize.height, y: 1 - (_point.x / frameSize.width))

        if device?.focusPointOfInterestSupported == true && device?.isFocusModeSupported(.AutoFocus) == true {

            do {

                try device?.lockForConfiguration()

                

                if device?.isWhiteBalanceModeSupported(.ContinuousAutoWhiteBalance) == true {

                    device?.whiteBalanceMode = .ContinuousAutoWhiteBalance

                }

                

                if device?.isFocusModeSupported(.ContinuousAutoFocus) == true {

                    device?.focusMode = .ContinuousAutoFocus

                    device?.focusPointOfInterest = pointOfInterest

                }

                

                if device?.exposurePointOfInterestSupported == true && device?.isExposureModeSupported(.ContinuousAutoExposure) == true {

                    device?.exposurePointOfInterest = pointOfInterest

                    device?.exposureMode = .ContinuousAutoExposure

                }

                

                device?.unlockForConfiguration()

                

                completionHandler()

            } catch {

               

            }

        } else {

            completionHandler()

        }

    }

    

   

 

posted @ 2017-08-08 10:20  lgx联盟  阅读(463)  评论(0编辑  收藏  举报