谷歌浏览器获取后置摄像头

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了谷歌浏览器获取后置摄像头相关的知识,希望对你有一定的参考价值。

谷歌浏览器获取摄像头会报错,TypeError: MediaStreamTrack.getSources is not a function

参考技术A MediaStreamTrack.getSources已经弃用,现在用getMediaDevices.enumerateDevices().then(function(mediaDeviceInfos) ....}本回答被提问者采纳 参考技术B chrome30+forandroid已经实现了利用webcam调用后置摄像头,想看的话我可以把源码贴出。但暂时没法对焦,实用不大。

iOS 摄像头应用,从前置摄像头切换到后置摄像头时闪烁

【中文标题】iOS 摄像头应用,从前置摄像头切换到后置摄像头时闪烁【英文标题】:iOS camera application, flicker when switching from front to back camera 【发布时间】:2012-01-30 02:00:19 【问题描述】:

我正在尝试模仿相机应用程序从前置摄像头到后置摄像头的过渡。它使用 UIViewAnimationOptionTransitionFlipFromLeft..

我目前的方法是用前置摄像头拍照,切换到后置摄像头,用后置摄像头拍照,将它们加载到两个UIImageView中并使用图像视图执行过渡..(隐藏当前相机流在这个过程中)..

问题是当我调用从前置摄像头切换到后置摄像头时,cameraView(一个 AVCapturePreviewLayer)会在我隐藏它之前立即切换视图(这发生在最后一行的 FlipCameraToFront 中。[_captureSession commitConfiguration]) ...

希望这是有道理的,这里有一些我省略了的代码,如果你认为这对我有帮助,请告诉我,我会发布其余的。 任何帮助将不胜感激,谢谢

//class:RecordViewController
//iVars for this class..   
RecordVideoHandler *_recordHandler;
UIImage *_firstImageForFlip;
UIImage *_secondImageForFlip;
UIImageView *_firstImageViewForFlip;
BOOL isUsingFrontInput;
AVCaptureVideoPreviewLayer *_capturePreviewLayer;
UIView *_capturePreviewView;

- (void)doCameraFlip:(UIButton *)sender

    [_recordHandler addStillImageOutput];
    //This method calls imageReturned:(UIImage *)inImage;
    [_recordHandler captureAndReturnStillImage];


   
- (void)imageReturned:(UIImage *)inImage

    if(_firstImageForFlip == nil)
        [_capturePreviewLayer setFrame:CGRectZero];
        _firstImageForFlip = inImage;
        _firstImageViewForFlip = [[UIImageView alloc] initWithImage:_firstImageForFlip];


        if(isUsingFrontInput)
            [_firstImageViewForFlip setTransform:CGAffineTransformMakeScale(-1.0, 1.0)];


        [_firstImageViewForFlip setFrame:_capturePreviewView.bounds];
        [_capturePreviewView addSubview:_firstImageViewForFlip];


        if(isUsingFrontInput)
            [_recordHandler flipCameraToBack];
        else
            [_recordHandler flipCameraToFront];
        
        [_recordHandler captureAndReturnStillImage];
    else
        _secondImageForFlip = inImage;
        [_recordHandler removeStillImageOutput];
        [self finishCameraFlip];
    

- (void)finishCameraFlip
    UIImageView *secondImageView = [[UIImageView alloc] initWithImage:_secondImageForFlip];
    [secondImageView setFrame:_capturePreviewView.bounds];

    if(!isUsingFrontInput)
        [secondImageView setTransform:CGAffineTransformMakeScale(-1.0, 1.0)];

    [UIView transitionWithView:_capturePreviewView duration:3.3f
options:UIViewAnimationOptionTransitionFlipFromLeft animations:
     ^
         [_firstImageViewForFlip removeFromSuperview];
         [_capturePreviewLayer setFrame:_capturePreviewView.bounds];
         [_capturePreviewView addSubview:secondImageView];

      completion:
     ^(BOOL finished)
         [secondImageView removeFromSuperview];
     ];

    isUsingFrontInput = isUsingFrontInput ? NO : YES;

    _firstImageForFlip = nil;
    _secondImageForFlip = nil;
    

类:RecordVideoHandler

    //iVars for this class..    
    AVCaptureSession *_captureSession;

    - (void)flipCameraToFront
    
    [_captureSession beginConfiguration];
    for (AVCaptureInput *input in _captureSession.inputs) 
        if (input == _captureRearInput) 
            [_captureSession removeInput:_captureRearInput];
            [_captureSession addInput:_captureFrontInput];
        
    
    _currentCaptureDevice = _captureDeviceFrontFacing;
    [_captureSession commitConfiguration];

【问题讨论】:

【参考方案1】:

如果有人想知道,我想通了。我不得不使用 performSelectors 来等待下一个运行循环,因为相机立即翻转,而视图的东西在运行循环或其他东西之后等待......不确定细节,这里是代码。

- (void)doCameraFlip:(UIButton *)sender

    NSLog(@"doCameraFlip");
    [_recordHandler addStillImageOutput];    
    [_recordHandler captureAndReturnStillImage];    

- (void)imageReturned:(UIImage *)inImage
    [_recordHandler removeStillImageOutput];            
    _imageViewForFlip= [[UIImageView alloc] initWithImage:inImage];
    [_imageViewForFlip setFrame:_containerView.bounds];
    [_containerView addSubview:_imageViewForFlip];


    [self performSelector:@selector(cfe) withObject:nil afterDelay:0.0f];
    [self performSelector:@selector(cfe2) withObject:nil afterDelay:0.0f];


- (void)cfe
    if(isUsingFrontInput)
        [_recordHandler flipCameraToBack];
    else
        [_recordHandler flipCameraToFront];
    


- (void)cfe2
    [UIView transitionWithView:_containerView duration:3.0f options:UIViewAnimationOptionTransitionFlipFromRight animations:^
        [_imageViewForFlip removeFromSuperview]; 
        [_containerView addSubview:_capturePreviewView]; 
     completion:nil];

【讨论】:

效果很好!非常感谢

以上是关于谷歌浏览器获取后置摄像头的主要内容,如果未能解决你的问题,请参考以下文章

如何通过html5调用手机摄像头?

谷歌浏览器怎样用js获取本机ip

我用的谷歌浏览器,想要用javaScript获取MAC地址,这段javaScript代码应该怎么写?

vuepc电脑可以获取设备吗

安卓标杆旗舰首次曝光!谷歌Pixel 4预计10月份发布

谷歌模块化手机曝光:可自由组合模块!