4

I am using AVCaptureVideoPreviewLayer to make a camera application. For some reason, I need to use just a part of the screen capturing in a square shape to show a display output. It is something similar to the early question: Cropping AVCaptureVideoPreviewLayer output to a square

However, I don't need to make the image taken to be a square shape. The only thing that I want is having a rectangle just show the part of the red area from the camera. I attached an example picture below.

enter image description here

Anyone can provide some example code in swift to show some hints how can I do that?

[updated] The code that I am using: * The code can also be downloaded from my bitbucket(https://bitbucket.org/fireares/swift-avcam_swiftwithcropfunction/src/7ca5aef02173e941e255623e230129cc2e304a7a/AVCamSwift-master-3?at=questionStackoverflow)

    override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    addTargetLayer()
    var session: AVCaptureSession = AVCaptureSession()
    self.session = session

    self.previewView.session = session

    self.checkDeviceAuthorizationStatus()

    var sessionQueue: dispatch_queue_t = dispatch_queue_create("session queue",DISPATCH_QUEUE_SERIAL)

    self.sessionQueue = sessionQueue
    dispatch_async(sessionQueue, {
        self.backgroundRecordId = UIBackgroundTaskInvalid

        var videoDevice: AVCaptureDevice! = ViewController.deviceWithMediaType(AVMediaTypeVideo, preferringPosition: AVCaptureDevicePosition.Back)
        var error: NSError? = nil

        var videoDeviceInput: AVCaptureDeviceInput? =  AVCaptureDeviceInput(device: videoDevice, error: &error)

        if (error != nil) {
            println(error)

        }

        if session.canAddInput(videoDeviceInput){
            session.addInput(videoDeviceInput)
            self.videoDeviceInput = videoDeviceInput

            dispatch_async(dispatch_get_main_queue(), {
                // Why are we dispatching this to the main queue?
                // Because AVCaptureVideoPreviewLayer is the backing layer for AVCamPreviewView and UIView can only be manipulated on main thread.
                // Note: As an exception to the above rule, it is not necessary to serialize video orientation changes on the AVCaptureVideoPreviewLayer’s connection with other session manipulation.

                var orientation: AVCaptureVideoOrientation =  AVCaptureVideoOrientation(rawValue: self.interfaceOrientation.rawValue)!
                (self.previewView.layer as! AVCaptureVideoPreviewLayer).connection.videoOrientation = orientation

            })

        }

                    if error != nil{
            println(error)
        }

        var movieFileOutput: AVCaptureMovieFileOutput = AVCaptureMovieFileOutput()
        if session.canAddOutput(movieFileOutput){
            session.addOutput(movieFileOutput)


            var connection: AVCaptureConnection? = movieFileOutput.connectionWithMediaType(AVMediaTypeVideo)
            let stab = connection?.supportsVideoStabilization
            if (stab != nil) {
                connection!.enablesVideoStabilizationWhenAvailable = true
            }

        //   self.movieFileOutput = movieFileOutput

        }

        var stillImageOutput: AVCaptureStillImageOutput = AVCaptureStillImageOutput()
        if session.canAddOutput(stillImageOutput){
            stillImageOutput.outputSettings = [AVVideoCodecKey: AVVideoCodecJPEG]
            session.addOutput(stillImageOutput)

            self.stillImageOutput = stillImageOutput
        }


    })


}
override func viewWillAppear(animated: Bool) {
    dispatch_async(self.sessionQueue!, {
        self.addObserver(self, forKeyPath: "stillImageOutput.capturingStillImage", options: NSKeyValueObservingOptions.Old | NSKeyValueObservingOptions.New, context: &CapturingStillImageContext)


        NSNotificationCenter.defaultCenter().addObserver(self, selector: "subjectAreaDidChange:", name: AVCaptureDeviceSubjectAreaDidChangeNotification, object: self.videoDeviceInput?.device)


        weak var weakSelf = self

        self.runtimeErrorHandlingObserver = NSNotificationCenter.defaultCenter().addObserverForName(AVCaptureSessionRuntimeErrorNotification, object: self.session, queue: nil, usingBlock: {
            (note: NSNotification?) in
            var strongSelf: ViewController = weakSelf!
            dispatch_async(strongSelf.sessionQueue!, {
            if let sess = strongSelf.session{
                    sess.startRunning()
                }

            })

        })

        self.session?.startRunning()

    })
}
Community
  • 1
  • 1
Ares Li
  • 603
  • 1
  • 7
  • 19
  • 1
    Will the red square consistently be in the same area? Can you add your code to your question. – Daniel Storm Aug 08 '15 at 12:50
  • Yes, the red square will be at the same position all the time. I just want the camera shows the preview of the red square area. I have already added my code on the post, as well as my bitbucket project link. Thanks @DanielStorm – Ares Li Aug 08 '15 at 16:46

0 Answers0