Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to turn on/offf camera flash light with ARKit?

Tags:

ios

swift

arkit

I am using a ARSCNView for displaying AR experiences that augment the camera view with 3D SceneKit content.

ARSession shared object that manages the device camera, but in ARSession i can't found any option for flash light.

Is there any way to turn on/off camera flash light?

How to manage camera with ARKit?

How to change camera position front and back?

like image 781
Harshal Valanda Avatar asked Dec 11 '25 04:12

Harshal Valanda


2 Answers

func toggleTorch(on: Bool) {
        guard let device = AVCaptureDevice.default(for: AVMediaType.video) 
        else {return}

        if device.hasTorch {
            do {
                try device.lockForConfiguration()

                if on == true {
                    device.torchMode = .on // set on
                } else {
                    device.torchMode = .off // set off
                }

                device.unlockForConfiguration()
            } catch {
                print("Torch could not be used")
            }
        } else {
            print("Torch is not available")
        }
    }

Call this as

toggleTorch(on: true) of toggleTorch(on: false)

ref: Hacking with Swift

like image 179
Vipin Avatar answered Dec 13 '25 18:12

Vipin


The other answer covers the torch, so as to your camera position question...

There’s no way to select a camera for ARKit; that is, ARKit selects the camera for you.

  • When you use world tracking, ARKit always selects the back-facing, wide-angle camera (not the telephoto or dual camera on devices so equipped).
  • When you use face tracking (iPhone X only), ARKit always uses the front-facing TrueDepth camera.
like image 22
rickster Avatar answered Dec 13 '25 19:12

rickster



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!