CaptureViewController.m 4.6 KB
Newer Older
杨时权 已提交
1
#import <Foundation/Foundation.h>
杨时权 已提交
2
#import <AVFoundation/AVFoundation.h>
杨时权 已提交
3
#import "CaptureViewController.h"
杨时权 已提交
4 5
#import "LWCheckBoxView.h"
#import "LWVideoCaptureSession.h"
杨时权 已提交
6

杨时权 已提交
7 8 9 10 11 12
@interface CaptureViewController () <LWVideoCaptureSessionDelegate, LWCheckBoxViewDelegate>
@property (strong, nonatomic) UIView* displayView;
@property (strong, nonatomic) LWCheckBoxView* checkBoxView;
@property (strong, nonatomic) UILabel* captureTypeLabel;
@property (strong, nonatomic) UIButton* startBtn;
@property (strong, nonatomic) UIButton* stopBtn;
杨时权 已提交
13

杨时权 已提交
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
@property (strong, nonatomic) LWVideoCaptureConfig* captureSessionConfig;
@property (strong, nonatomic) LWVideoCaptureSession* captureSession;
@end

@implementation CaptureViewController

- (void) viewDidLoad {
    int displayViewX = 0;
    int displayViewY = 20;
    int displayViewW = [UIScreen mainScreen].bounds.size.width;
    int displayViewH = displayViewW / 16 * 9;
    self.displayView = [[UIView alloc] initWithFrame:CGRectMake(displayViewX, displayViewY, displayViewW, displayViewH)];
    self.displayView.backgroundColor = [UIColor blackColor];
    [self.view addSubview:self.displayView];
    
    int checkBoxViewX = 100;
    int checkBoxViewY = displayViewH + displayViewY + 10;
    int checkBoxViewW = 270;
    int checkBoxViewH = 30;
    LWCheckBoxItem* item1 = [[LWCheckBoxItem alloc] initWithName:@"前置"];
    LWCheckBoxItem* item2 = [[LWCheckBoxItem alloc] initWithName:@"后置"];
    NSArray<LWCheckBoxItem*>* array = [NSArray arrayWithObjects:item1, item2, nil];
    self.checkBoxView = [[LWCheckBoxView alloc] initWithItems:array];
    self.checkBoxView.frame = CGRectMake(checkBoxViewX, checkBoxViewY, checkBoxViewW, checkBoxViewH);
    self.checkBoxView.displayType = kLWCheckBoxViewDisplayType_LR;
杨时权 已提交
39
    self.checkBoxView.delegate = self;
杨时权 已提交
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
    [self.view addSubview:self.checkBoxView];
    
    self.captureTypeLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, checkBoxViewY, 100, 30)];
    self.captureTypeLabel.text = @"摄像头选择";
    self.captureTypeLabel.backgroundColor = [UIColor whiteColor];
    self.captureTypeLabel.textColor = [UIColor blackColor];
    [self.view addSubview:self.captureTypeLabel];
    
    self.startBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, checkBoxViewY + checkBoxViewH + 10, [UIScreen mainScreen].bounds.size.width / 2, 30)];
//    self.startBtn.enabled = YES;
    [self.startBtn setTitle:@"start" forState:UIControlStateNormal];
    [self.startBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.startBtn setBackgroundColor:[UIColor whiteColor]];
    [self.startBtn addTarget:self action:@selector(startSender:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.startBtn];
    
    self.stopBtn = [[UIButton alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width / 2, checkBoxViewY + checkBoxViewH + 10, [UIScreen mainScreen].bounds.size.width / 2, 30)];
//    self.startBtn.enabled = YES;
    [self.stopBtn setTitle:@"stop" forState:UIControlStateNormal];
    [self.stopBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.stopBtn setBackgroundColor:[UIColor whiteColor]];
    [self.stopBtn addTarget:self action:@selector(stopSender:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.stopBtn];
}

- (void)viewDidDisappear:(BOOL)animated {
    
}

- (IBAction)startSender:(id)sender {
杨时权 已提交
70 71 72
    self.captureSessionConfig = [[LWVideoCaptureConfig alloc] init];
    self.captureSession = [[LWVideoCaptureSession alloc] initWithConfig:self.captureSessionConfig];
    
杨时权 已提交
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
    [self.captureSession startCapture];
    
    AVCaptureVideoPreviewLayer* layer = [self.captureSession layer];
    layer.frame = CGRectMake(0, 0, self.displayView.frame.size.width, self.displayView.frame.size.height);
    layer.videoGravity = AVLayerVideoGravityResizeAspectFill;
    [self.displayView.layer addSublayer:layer];
}

- (IBAction)stopSender:(id)sender {
    AVCaptureVideoPreviewLayer* layer = [self.captureSession layer];
    layer.frame = CGRectMake(0, 0, self.displayView.frame.size.width, self.displayView.frame.size.height);
    layer.videoGravity = AVLayerVideoGravityResizeAspectFill;
    [layer removeFromSuperlayer];
    
    [self.captureSession stopCapture];
杨时权 已提交
88
    self.captureSession = nil;
杨时权 已提交
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
}

#pragma mark LWVideoCaptureSessionDelegate
- (void)onVideoWithSampleBuffer:(CMSampleBufferRef)sampleBuffer {
    
}

#pragma mark LWCheckBoxViewDelegate
- (void)onSelect:(int)index item:(LWCheckBoxItem*)item {
    if ([item.checkBoxName isEqual:@"前置"]) {
        self.captureSessionConfig.position = AVCaptureDevicePositionFront;
    } else {
        self.captureSessionConfig.position = AVCaptureDevicePositionBack;
    }
}

@end