欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

iOS實(shí)現(xiàn)攝像頭實(shí)時(shí)采集圖像

 更新時(shí)間:2021年04月22日 14:22:32   作者:survivorsfyh  
這篇文章主要為大家詳細(xì)介紹了iOS實(shí)現(xiàn)攝像頭實(shí)時(shí)采集圖像,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了iOS實(shí)現(xiàn)攝像頭實(shí)時(shí)采集圖像的具體代碼,供大家參考,具體內(nèi)容如下

新接到一個(gè)實(shí)時(shí)獲取攝像頭當(dāng)前照片的需求,在設(shè)定的時(shí)間內(nèi)需要保持?jǐn)z像頭處在開啟狀態(tài)并可以實(shí)時(shí)回調(diào)到當(dāng)前的圖片數(shù)據(jù)信息;

此次結(jié)合 AVCaptureDevice、AVCaptureSession、AVCaptureVideoPreviewLayer 將其與 UIView、UIImageView 和 UIImage 相結(jié)合;

Github

具體實(shí)現(xiàn) code 如下:

#import <UIKit/UIKit.h>
#import <CoreVideo/CoreVideo.h>
#import <CoreMedia/CoreMedia.h>
#import <AVFoundation/AVFoundation.h>
 
NS_ASSUME_NONNULL_BEGIN
 
@interface YHCameraView : UIView <AVCaptureVideoDataOutputSampleBufferDelegate>
 
@property (nonatomic, weak) UIImageView *cameraImageView;
@property (strong, nonatomic) AVCaptureDevice* device;
@property (strong, nonatomic) AVCaptureSession* captureSession;
@property (strong, nonatomic) AVCaptureVideoPreviewLayer* previewLayer;
@property (strong, nonatomic) UIImage* cameraImage;
 
@end
 
NS_ASSUME_NONNULL_END
#import "YHCameraView.h"
 
@implementation YHCameraView
 
- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor lightGrayColor];
        [self createUI];
    }
    return self;
}
 
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
 
- (void)createUI {
    NSArray* devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
    for(AVCaptureDevice *device in devices)
    {
        if([device position] == AVCaptureDevicePositionFront) // 前置攝像頭
            self.device = device;
    }
    
    AVCaptureDeviceInput* input = [AVCaptureDeviceInput deviceInputWithDevice:self.device error:nil];
    AVCaptureVideoDataOutput* output = [[AVCaptureVideoDataOutput alloc] init];
    output.alwaysDiscardsLateVideoFrames = YES;
 
    dispatch_queue_t queue;
    queue = dispatch_queue_create("cameraQueue", NULL);
    [output setSampleBufferDelegate:self queue:queue];
 
    NSString* key = (NSString *) kCVPixelBufferPixelFormatTypeKey;
    NSNumber* value = [NSNumber numberWithUnsignedInt:kCVPixelFormatType_32BGRA];
    NSDictionary* videoSettings = [NSDictionary dictionaryWithObject:value forKey:key];
    [output setVideoSettings:videoSettings];
 
    self.captureSession = [[AVCaptureSession alloc] init];
    [self.captureSession addInput:input];
    [self.captureSession addOutput:output];
    [self.captureSession setSessionPreset:AVCaptureSessionPresetPhoto];
 
    self.previewLayer = [AVCaptureVideoPreviewLayer layerWithSession:self.captureSession];
    self.previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
 
    // CHECK FOR YOUR APP
    NSInteger screenWidth = self.frame.size.width;
    NSInteger screenHeitht = self.frame.size.height;
    self.previewLayer.frame = self.bounds;
    self.previewLayer.orientation = AVCaptureVideoOrientationPortrait;
    // CHECK FOR YOUR APP
 
//    [self.layer insertSublayer:self.previewLayer atIndex:0];   // Comment-out to hide preview layer
 
    [self.captureSession startRunning];
}
 
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection
{
    CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
    CVPixelBufferLockBaseAddress(imageBuffer, 0);
    uint8_t *baseAddress = (uint8_t *)CVPixelBufferGetBaseAddress(imageBuffer);
    size_t bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer);
    size_t width = CVPixelBufferGetWidth(imageBuffer);
    size_t height = CVPixelBufferGetHeight(imageBuffer);
 
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGContextRef newContext = CGBitmapContextCreate(baseAddress, width, height, 8, bytesPerRow, colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst);
    CGImageRef newImage = CGBitmapContextCreateImage(newContext);
 
    CGContextRelease(newContext);
    CGColorSpaceRelease(colorSpace);
 
    self.cameraImage = [UIImage imageWithCGImage:newImage scale:1.0f orientation:UIImageOrientationLeftMirrored]; // UIImageOrientationDownMirrored
    self.cameraImageView.image = [UIImage imageWithCGImage:newImage scale:1.0f orientation:UIImageOrientationLeftMirrored];
 
    CGImageRelease(newImage);
 
    CVPixelBufferUnlockBaseAddress(imageBuffer, 0);
}
 
@end

將其實(shí)例化后在需要的時(shí)候直接獲取其 cameraView 的 cameraImage 即可;

#pragma mark - 快照采集
/// 快照采集
- (YHCameraView *)cameraView {
    if (!_cameraView) {
        YHCameraView *view = [[YHCameraView alloc] init];
        view.frame = CGRectMake(1, 1, 1, 1);
        view.cameraImageView.image = view.cameraImage;
        _cameraView = view;
    }
    return _cameraView;
}
 
NSString *strImg = [YHCameraManager imageBase64EncodedWithImage:self.cameraView.cameraImage
                                                       AndImageType:@"JPEG"]; // 獲取照片信息
/**
 圖片轉(zhuǎn) Base64
 
 @param img     原圖片
 @param type    圖片類型(PNG 或 JPEG)
 @return        處理結(jié)果
 */
+ (NSString *)imageBase64EncodedWithImage:(UIImage *)img AndImageType:(NSString *)type {
    NSString *callBack = nil;
    if ([img isKindOfClass:[UIImage class]]) {
        NSData *data = [NSData data];
        if ([type isEqualToString:@"PNG"]) {
            data = UIImagePNGRepresentation(img);
        } else {
            data = UIImageJPEGRepresentation(img, 1.0f);
        }
        
        NSString *encodedImgStr = [data base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
        
        NSLog(@"YHCameraManager\nencodedImgStr: %@", encodedImgStr);
        return encodedImgStr;
    } else {
        return callBack;
    }
}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • iOS APP中保存圖片到相冊時(shí)崩潰的解決方法

    iOS APP中保存圖片到相冊時(shí)崩潰的解決方法

    下面小編就為大家分享一篇iOS APP中保存圖片到相冊時(shí)崩潰的解決方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2017-12-12
  • iOS開發(fā)之離線地圖核心代碼

    iOS開發(fā)之離線地圖核心代碼

    本文給大家分享ios開發(fā)之離線地圖核心代碼,代碼簡單易懂,非常實(shí)用,有需要的朋友參考下
    2016-04-04
  • 詳解iOS本地推送與遠(yuǎn)程推送

    詳解iOS本地推送與遠(yuǎn)程推送

    這篇文章主要為大家詳細(xì)介紹了iOS本地推送與遠(yuǎn)程推送,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-09-09
  • iOS基于CATransition實(shí)現(xiàn)翻頁、旋轉(zhuǎn)等動(dòng)畫效果

    iOS基于CATransition實(shí)現(xiàn)翻頁、旋轉(zhuǎn)等動(dòng)畫效果

    這篇文章主要為大家詳細(xì)介紹了iOS基于CATransition實(shí)現(xiàn)翻頁、旋轉(zhuǎn)等動(dòng)畫效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-04-04
  • Objective-C const常量的優(yōu)雅使用方法

    Objective-C const常量的優(yōu)雅使用方法

    這篇文章主要為大家介紹了Objective-C const常量的優(yōu)雅使用方法示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-08-08
  • 30分鐘快速帶你理解iOS中的謂詞NSPredicate

    30分鐘快速帶你理解iOS中的謂詞NSPredicate

    NSPredicate類是用來定義邏輯條件約束的獲取或內(nèi)存中的過濾搜索。下面這篇文章將通過30分鐘快速帶大家理解iOS中的謂詞NSPredicate類,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。
    2018-03-03
  • IOS檢測指定路徑的文件是否存在

    IOS檢測指定路徑的文件是否存在

    本文給大家分享的是在IOS開發(fā)中檢測指定文件是否存在的方法,給大家匯總了4種,十分實(shí)用,小伙伴們根據(jù)自己的需求自由選擇吧。
    2015-05-05
  • iOS實(shí)現(xiàn)左右拖動(dòng)抽屜效果

    iOS實(shí)現(xiàn)左右拖動(dòng)抽屜效果

    這篇文章主要介紹了iOS實(shí)現(xiàn)左右拖動(dòng)抽屜效果,理解ios平臺類似于QQ主頁面,利用觸摸事件滑動(dòng)touchesMoved實(shí)現(xiàn)的效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-02-02
  • 詳解iOS的深淺拷貝

    詳解iOS的深淺拷貝

    本文詳細(xì)介紹了IOS中的三種拷貝方式,對iOS的深淺拷貝有疑問的朋友們可以參考下本文。
    2016-08-08
  • 淺談iOS解析HTMl標(biāo)簽以及開發(fā)中的一些坑

    淺談iOS解析HTMl標(biāo)簽以及開發(fā)中的一些坑

    這篇文章主要介紹了淺談iOS解析HTMl標(biāo)簽以及開發(fā)中的一些坑,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-12-12

最新評論