让ChatGPT给我写了一个简易相机demo,支持手动对焦,真香

//

//  CameraVC.m

//  AWOpenDemo

//

//  Created by yikunHuang on 2022/12/7.

//

#import "CameraVC.h"

#import <AVFoundation/AVFoundation.h>

@interface CameraVC ()

@property(strong, nonatomic)AVCaptureDevice *device;

@property(strong, nonatomic)AVCaptureSession *session;

@end

@implementation CameraVC

- (void)viewDidLoad {

    [super viewDidLoad];

    

    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(100, 100, 50, 50)];

    [btn addTarget:self action:@selector(capture) forControlEvents:UIControlEventTouchUpInside ];

    btn.backgroundColor = [UIColor whiteColor];

    btn.titleLabel.text = @"拍照";

    

    // 获取默认的摄像头设备

    AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];

    self.device = device;

    // 支持手动对焦

    if ([device isFocusModeSupported:AVCaptureFocusModeLocked]) {

        NSError *error;

        if ([device lockForConfiguration:&error]) {

            // 成功获取配置锁,可以进行摄像头设备的配置操作

            // 设置对焦模式为手动对焦

            device.focusMode = AVCaptureFocusModeLocked;

            

            // 释放配置锁

            [device unlockForConfiguration];

        } else {

            // 获取配置锁失败,可能是其他应用或程序正在使用摄像头设备

        }

        

    }

    // 创建会话

    AVCaptureSession *session = [[AVCaptureSession alloc] init];

    self.session = session;

    // 创建摄像头输入

    AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error:nil];

    // 创建摄像头输出

    AVCaptureStillImageOutput *output = [[AVCaptureStillImageOutput alloc] init];

    // 添加摄像头输入

    if ([session canAddInput:input]) {

        [session addInput:input];

    }

    // 添加摄像头输出

    if ([session canAddOutput:output]) {

        [session addOutput:output];

    }

    // 创建预览层

    AVCaptureVideoPreviewLayer *previewLayer = [AVCaptureVideoPreviewLayer layerWithSession:session];

    previewLayer.frame = self.view.bounds;

    [self.view.layer addSublayer:previewLayer];

    // 启动会话

    [session startRunning];

    

    [self.view addSubview:btn];

}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {

    // 获取触摸点

    UITouch *touch = touches.anyObject;

    // 获取触摸点在屏幕中的坐标

    CGPoint point = [touch locationInView:self.view];

    // 判断摄像头设备是否支持手动对焦

    if ([self.device isFocusPointOfInterestSupported] && [self.device isFocusModeSupported:AVCaptureFocusModeAutoFocus]) {

        // 获取配置锁

        NSError *error;

        if ([self.device lockForConfiguration:&error]) {

            // 设置对焦模式为手动对焦

            self.device.focusMode = AVCaptureFocusModeAutoFocus;

            // 设置对焦点

            self.device.focusPointOfInterest = point;

            // 释放配置锁

            [self.device unlockForConfiguration];

        }

    }

}

#pragma mark:- action

- (void)capture {

    // 获取摄像头输出

       AVCaptureStillImageOutput *output = [self.session.outputs firstObject];

       // 获取摄像头输出的图像

       [output captureStillImageAsynchronouslyFromConnection:[output.connections firstObject] completionHandler:^(CMSampleBufferRef  _Nullable imageDataSampleBuffer, NSError * _Nullable error) {

           if (imageDataSampleBuffer == nil) {

               return;

           }

           // 获取图像数据

           NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer];

           // 创建图片

           UIImage *image = [UIImage imageWithData:imageData];

           // 在屏幕上显示图片

           UIImageView *imageView = [[UIImageView alloc] initWithImage:image];

           imageView.frame = self.view.bounds;

           [self.view addSubview:imageView];

       }];

}

@end

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐