最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

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

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

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

Github

具體實現(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

將其實例化后在需要的時候直接獲取其 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)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

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

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

    本文給大家分享ios開發(fā)之離線地圖核心代碼,代碼簡單易懂,非常實用,有需要的朋友參考下
    2016-04-04
  • Apple?Watch?App?Lifecycle應(yīng)用開發(fā)

    Apple?Watch?App?Lifecycle應(yīng)用開發(fā)

    這篇文章主要為大家介紹了Apple?Watch?App?Lifecycle應(yīng)用開發(fā)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-10-10
  • IOS中UIWebView、WKWebView之JS交互

    IOS中UIWebView、WKWebView之JS交互

    本篇文章主要介紹了IOS中UIWebView、WKWebView之JS交互,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-06-06
  • iOS的UI開發(fā)中UITabBarControlle的基本使用教程

    iOS的UI開發(fā)中UITabBarControlle的基本使用教程

    這篇文章主要介紹了iOS的UI開發(fā)中UITabBarControlle的基本使用教程,代碼基于傳統(tǒng)的Objective-C,需要的朋友可以參考下
    2015-12-12
  • iOS如何獲取手機的Mac地址

    iOS如何獲取手機的Mac地址

    這篇文章主要為大家詳細介紹了iOS獲取手機的Mac地址的多種方法,感興趣的小伙伴們可以參考一下
    2016-04-04
  • iOS WKWebView無法處理URL Scheme和App Store鏈接的問題解決

    iOS WKWebView無法處理URL Scheme和App Store鏈接的問題解決

    這篇文章主要給大家介紹了關(guān)于iOS WKWebView無法處理URL Scheme和App Store鏈接的問題解決的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧。
    2018-03-03
  • iOS實現(xiàn)底部彈出PopupWindow效果 iOS改變背景透明效果

    iOS實現(xiàn)底部彈出PopupWindow效果 iOS改變背景透明效果

    這篇文章主要為大家詳細介紹了iOS實現(xiàn)底部彈出PopupWindow效果,iOS改變背景透明效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-07-07
  • iOS如何獲取最頂層ViewController詳解

    iOS如何獲取最頂層ViewController詳解

    這篇文章主要給大家介紹了關(guān)于iOS如何獲取最頂層ViewController的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-09-09
  • 一篇文章讓你看懂IOS中的block為何再也不需要WeakSelf弱引用

    一篇文章讓你看懂IOS中的block為何再也不需要WeakSelf弱引用

    這篇文章主要給大家介紹了關(guān)于IOS中block為何再也不需要WeakSelf弱引用的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對各位iOS開發(fā)者們具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧。
    2018-01-01
  • 實例講解iOS中的CATransition轉(zhuǎn)場動畫使用

    實例講解iOS中的CATransition轉(zhuǎn)場動畫使用

    CATransition類為應(yīng)用程序的轉(zhuǎn)場動畫提供了很多可控制參數(shù),接下來我們就以幾個實例講解iOS中的CATransition轉(zhuǎn)場動畫使用,需要的朋友可以參考下
    2016-06-06

最新評論

固原市| 大荔县| 嵊泗县| 宜都市| 万源市| 冷水江市| 高淳县| 鄂托克前旗| 保康县| 泗洪县| 余干县| 河北省| 全南县| 平利县| 揭西县| 扎囊县| 宜兴市| 武宁县| 玉门市| 水富县| 元阳县| 玛纳斯县| 化德县| 怀远县| 永仁县| 沙坪坝区| 大港区| 施秉县| 托里县| 那坡县| 马山县| 望都县| 富蕴县| 宜春市| 时尚| 铜鼓县| 景宁| 深圳市| 白沙| 永嘉县| 彭州市|