Giter Site home page Giter Site logo

customcamera's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

customcamera's Issues

Swift 4 has runtime errors with code

This is a great tutorial! I'm stuck on swift 4 syntax changes and I'm hoping you can help.

This line of code in ViewController prepareCamera function

if let availableDevices = AVCaptureDevice.DiscoverySession(deviceTypes:  [.builtInWideAngleCamera], mediaType: AVMediaType.video, position: .back).devices

Has a Buildtime error:

ViewController.swift:34:12: Initializer for conditional binding must have Optional type, not '[AVCaptureDevice]'

I've tried several things and this converted it from an Error to a Warning:

if let availableDevices = try?
            AVCaptureDevice.DiscoverySession(deviceTypes:  [.builtInWideAngleCamera], mediaType: AVMediaType.video, position: .back).devices
ViewController.swift:53:13: No calls to throwing functions occur within 'try' expression

This also happens with if let previewLayer in the beginSession function

Preview not matching superview frame?

Hey Brian! Curious as to why setting the layers frame doesn't make it match the full screen parent view bounds? Trying to get a full screen camera preview. Any help would be great!!

Fix the issue that "viewWillAppear" not called when PhotoVC dissmissed.

Ref : https://stackoverflow.com/a/60295024/2151275

Need to set the modalPresentationStyle of PhotoVC to full screen.

Full code of ViewController.swift

import UIKit
import AVFoundation

class ViewController: UIViewController, AVCaptureVideoDataOutputSampleBufferDelegate {
    
    let captureSession = AVCaptureSession()
    var previewLayer:CALayer!
    var captrueDevice:AVCaptureDevice!
    
    var takePhoto = false

    override func viewDidLoad() {
        super.viewDidLoad()
        prepareCamera()
    }
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        self.captureSession.startRunning()
    }

    func prepareCamera() {
        captureSession.sessionPreset = AVCaptureSession.Preset.photo
        
        let availableDeviceSession = AVCaptureDevice.DiscoverySession(deviceTypes: [.builtInWideAngleCamera], mediaType: .video, position: .back)
        
        captrueDevice = availableDeviceSession.devices.first
        beginSession()
    }
    
    func beginSession() {
        do {
            let captureDeviceInput = try AVCaptureDeviceInput(device: captrueDevice)
            captureSession.addInput(captureDeviceInput)
        } catch {
            print(error.localizedDescription)
        }
        
        let previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
        self.previewLayer = previewLayer
        self.view.layer.addSublayer(self.previewLayer)
        self.previewLayer.frame = self.view.layer.frame
        captureSession.startRunning()
        
        let dataOutput = AVCaptureVideoDataOutput()
        dataOutput.videoSettings = [(kCVPixelBufferPixelFormatTypeKey as String): NSNumber(value: kCVPixelFormatType_32BGRA)]
        captureSession.addOutput(dataOutput)
        
        captureSession.commitConfiguration()
        
        
        let queue = DispatchQueue(label: "com.czk.captureQueue")
        dataOutput.setSampleBufferDelegate(self, queue: queue)

        
    }
    
    @IBAction func takePhoto(_ sender: Any) {
        takePhoto = true
    }
    
    func captureOutput(_ output: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) {
        if takePhoto {
            takePhoto = false
            if let image = getImageFromSampleBuffer(buffer: sampleBuffer) {
                
                
                
                DispatchQueue.main.async {
                    let photoVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(identifier: "PhotoVC") as! PhotoViewController
                    photoVC.takenPhoto = image
                    photoVC.modalPresentationStyle = .fullScreen
                    self.present(photoVC, animated: true, completion: {
                        self.captureSession.stopRunning()
                    })
                    
                }
            }
        }
    }
    
    func getImageFromSampleBuffer(buffer:CMSampleBuffer) -> UIImage? {
        if let pixelBuffer = CMSampleBufferGetImageBuffer(buffer) {
            let ciImage = CIImage(cvPixelBuffer: pixelBuffer)
            
            let context = CIContext()
            let imageRect = CGRect(x: 0, y: 0, width: CVPixelBufferGetWidth(pixelBuffer), height: CVPixelBufferGetHeight(pixelBuffer))
            
            if let image = context.createCGImage(ciImage, from: imageRect) {
                return UIImage(cgImage: image, scale: UIScreen.main.scale, orientation: .right)
            }
        }
        
        return nil
    }

}

Problem with photoVC

in the line
photoVC.takenPhoto = image
the error of "Value of type 'PhotoViewController' has no member 'takenPhoto'" shows up as photoVC is initialized as a UIStoryBoard. I'm not sure if this error only occurs in newer versions of swift or not but I can't seem to get past it

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.