ios简易版相机

本文介绍了一个简易相机Demo的实现,支持手动对焦功能。通过Objective-C编程,利用AVFoundation框架,实现了iOS平台上的相机应用。该应用可以调整对焦模式,并通过触摸屏幕改变对焦点。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

让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

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值