【问题标题】:AVCapturePhotoOutput not returning proper imageAVCapturePhotoOutput 没有返回正确的图像
【发布时间】:2017-12-26 07:02:25
【问题描述】:

我在应用程序中的要求是在不预览UIImagePickerController 的情况下捕获图像所以我使用以下代码来捕获图像而不呈现相同的图像。

- (void)clickImage
{
    AVCaptureDevice *rearCamera = [self checkIfRearCameraAvailable];

    if (rearCamera != nil)
    {
        photoSession = [[AVCaptureSession alloc] init];

        NSError *error;
        AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:rearCamera error:&error];

        if (!error && [photoSession canAddInput:input])
        {
            [photoSession addInput:input];

            AVCapturePhotoOutput *output = [[AVCapturePhotoOutput alloc] init];


            if ([photoSession canAddOutput:output])
            {
                [photoSession addOutput:output];

                AVCaptureConnection *videoConnection = nil;

                for (AVCaptureConnection *connection in output.connections)
                {
                    for (AVCaptureInputPort *port in [connection inputPorts])
                    {
                        if ([[port mediaType] isEqual:AVMediaTypeVideo])
                        {
                            videoConnection = connection;
                            break;
                        }
                    }
                    if (videoConnection)
                    {
                        break;
                    }
                }

                if (videoConnection)
                {
                    [photoSession startRunning];

                    [output capturePhotoWithSettings:[AVCapturePhotoSettings photoSettings] delegate:self];
                }
            }
        }
    }
}

- (AVCaptureDevice *)checkIfRearCameraAvailable
{
    AVCaptureDevice *rearCamera;

    AVCaptureDeviceDiscoverySession *captureDeviceDiscoverySession =
    [AVCaptureDeviceDiscoverySession discoverySessionWithDeviceTypes:@[AVCaptureDeviceTypeBuiltInWideAngleCamera]
                                                           mediaType:AVMediaTypeVideo
                                                            position:AVCaptureDevicePositionBack];

    NSArray *allCameras = [captureDeviceDiscoverySession devices];


    for (int i = 0; i < allCameras.count; i++)
    {
        AVCaptureDevice *camera = [allCameras objectAtIndex:i];

        if (camera.position == AVCaptureDevicePositionBack)
        {
            rearCamera = camera;
        }
    }

    return rearCamera;
}

- (void)captureOutput:(AVCapturePhotoOutput *)output didFinishProcessingPhotoSampleBuffer:(CMSampleBufferRef)photoSampleBuffer previewPhotoSampleBuffer:(CMSampleBufferRef)previewPhotoSampleBuffer resolvedSettings:(AVCaptureResolvedPhotoSettings *)resolvedSettings bracketSettings:(AVCaptureBracketedStillImageSettings *)bracketSettings error:(NSError *)error
{
    if (error)
    {
        NSLog(@"error : %@", error.localizedDescription);
    }

    if (photoSampleBuffer)
    {
        NSData *data = [AVCapturePhotoOutput JPEGPhotoDataRepresentationForJPEGSampleBuffer:photoSampleBuffer
                                                                   previewPhotoSampleBuffer:previewPhotoSampleBuffer];
        UIImage *image = [UIImage imageWithData:data];

        _imgView.image = image;
    }
}

我已经使用上面的代码来捕获图像但是输出图像返回看起来像在夜间模式或像负图像。

您能检查一下代码并纠正我吗?

【问题讨论】:

    标签: objective-c avfoundation avcapturedevice


    【解决方案1】:

    我从Apple Developer's site 找到了以下示例代码:

    https://developer.apple.com/library/content/samplecode/AVCam/Introduction/Intro.html#//apple_ref/doc/uid/DTS40010112

    以上示例代码有助于获得正确的图像。我也添加了这个答案以寻求其他人的帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-05-30
      • 2021-01-25
      • 2014-11-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多