From e21b1c797955a231f2bcf71818e0259fbb6aeba1 Mon Sep 17 00:00:00 2001 From: Runt <qingingrunt2010@qq.com> Date: Fri, 27 Jun 2025 15:57:25 +0000 Subject: [PATCH] 相机权限 --- LiveProject/controller/CameraCapture.swift | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 47 insertions(+), 0 deletions(-) diff --git a/LiveProject/controller/CameraCapture.swift b/LiveProject/controller/CameraCapture.swift index e69de29..455b88a 100644 --- a/LiveProject/controller/CameraCapture.swift +++ b/LiveProject/controller/CameraCapture.swift @@ -0,0 +1,47 @@ +// +// CameraCapture.swift +// LiveProject +// +// Created by 倪路朋 on 6/27/25. +// +import AVFoundation + +class CameraCapture: NSObject, AVCaptureVideoDataOutputSampleBufferDelegate { + private let session = AVCaptureSession() + var onFrame: ((CVPixelBuffer) -> Void)? + + func start() { + guard let device = AVCaptureDevice.default(for: .video), + let input = try? AVCaptureDeviceInput(device: device) else { + print("❌ 相机设备无法创建") + return + } + + session.beginConfiguration() + session.sessionPreset = .high + + if session.canAddInput(input) { + session.addInput(input) + } + + let output = AVCaptureVideoDataOutput() + output.videoSettings = [ + kCVPixelBufferPixelFormatTypeKey as String: kCVPixelFormatType_32BGRA + ] + output.setSampleBufferDelegate(self, queue: DispatchQueue(label: "camera.queue")) + + if session.canAddOutput(output) { + session.addOutput(output) + } + + session.commitConfiguration() + session.startRunning() + } + + func captureOutput(_ output: AVCaptureOutput, + didOutput sampleBuffer: CMSampleBuffer, + from connection: AVCaptureConnection) { + guard let buffer = CMSampleBufferGetImageBuffer(sampleBuffer) else { return } + onFrame?(buffer) + } +} -- Gitblit v1.9.1