Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to take a picture using the proximity sensor?

I am having trouble getting the device to take an image using the rear view camera when the proximity sensor is enabled. I don't want the camera preview to show up, just want the device to take the photo and present it in the imageView. I have the proximity sensor working, and I am using imagePicker.takePicture() to take the image when the proximity sensor is enabled, but that doesn't seem to work. What is the method/function that I can use to programmatically take the picture without the user input.

This is my code so far:

class ViewController: UIViewController, UINavigationControllerDelegate, UIImagePickerControllerDelegate {

@IBOutlet var imageView: UIImageView!

var imagePicker: UIImagePickerController!

//*The function in question*  
func proximityChanged(notification: NSNotification) {
    let device = notification.object as? UIDevice
    if device?.proximityState == true {
        print("\(device) detected!")
like image 951
D.Khan Avatar asked Dec 13 '15 22:12

D.Khan


People also ask

What is a proximity camera?

Introduction: Proximity Triggered Car Security Camera This is a car security system that will start a camera recording when someone touches your vehicle. It can be run off it's own 12v power supply or you can wire it to the car battery and use a switch or relay to turn it on.

How do I check proximity sensor?

One way to test the sensor is by lifting your phone up to your face to see if it lights up. Another way is to get on a call and place your phone near your ear to see if it dims the screen. If these don't work, you can download a free third-party app like Sensor Test to perform a proximity sensor test.


1 Answers

If you have troubles capturing photos with UIImagePickerController, I suggest using AVFoundation.

Below is a working example. Photo capture is triggered by the proximity sensor.

You can add a preview if you need it.

import UIKit
import AVFoundation

final class CaptureViewController: UIViewController {

    @IBOutlet weak var imageView: UIImageView!

    private static let captureSessionPreset = AVCaptureSessionPresetPhoto
    private var captureSession: AVCaptureSession!
    private var photoOutput: AVCaptureStillImageOutput!
    private var initialized = false

    override func viewDidLoad() {
        super.viewDidLoad()
        initialized = setupCaptureSession()
    }

    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        if initialized {
            captureSession.startRunning()
            UIDevice.currentDevice().proximityMonitoringEnabled = true
            NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(proximityStateDidChange), name: UIDeviceProximityStateDidChangeNotification, object: nil)
        }
    }

    override func viewDidDisappear(animated: Bool) {
        super.viewDidDisappear(animated)
        if initialized {
            NSNotificationCenter.defaultCenter().removeObserver(self, name: UIDeviceProximityStateDidChangeNotification, object: nil)
            UIDevice.currentDevice().proximityMonitoringEnabled = false
            captureSession.stopRunning()
        }
    }

    dynamic func proximityStateDidChange(notification: NSNotification) {
        if UIDevice.currentDevice().proximityState {
            captureImage()
        }
    }

    // MARK: - Capture Image

    private func captureImage() {
        if let c = findConnection() {
            photoOutput.captureStillImageAsynchronouslyFromConnection(c) { sampleBuffer, error in
                if let jpeg  = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(sampleBuffer),
                   let image = UIImage(data: jpeg)
                {
                    dispatch_async(dispatch_get_main_queue()) { [weak self] in
                        self?.imageView.image = image
                    }
                }
            }
        }
    }

    private func findConnection() -> AVCaptureConnection? {
        for c in photoOutput.connections {
            let c = c as? AVCaptureConnection
            for p in c?.inputPorts ?? [] {
                if p.mediaType == AVMediaTypeVideo {
                    return c
                }
            }
        }
        return nil
    }

    // MARK: - Setup Capture Session

    private func setupCaptureSession() -> Bool {
        captureSession = AVCaptureSession()
        if  captureSession.canSetSessionPreset(CaptureViewController.captureSessionPreset) {
            captureSession.sessionPreset = CaptureViewController.captureSessionPreset
            if setupCaptureSessionInput() && setupCaptureSessionOutput() {
                return true
            }
        }
        return false
    }

    private func setupCaptureSessionInput() -> Bool {
        if let captureDevice = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo),
           let captureDeviceInput = try? AVCaptureDeviceInput.init(device: captureDevice)
        {
            if  captureSession.canAddInput(captureDeviceInput) {
                captureSession.addInput(captureDeviceInput)
                return true
            }
        }
        return false
    }

    private func setupCaptureSessionOutput() -> Bool {
        photoOutput = AVCaptureStillImageOutput()
        photoOutput.outputSettings = [AVVideoCodecKey: AVVideoCodecJPEG]
        if  captureSession.canAddOutput(photoOutput) {
            captureSession.addOutput(photoOutput)
            return true
        }
        return false
    }

}
like image 73
werediver Avatar answered Oct 16 '22 09:10

werediver