【问题标题】:AVCam save full screen captured imageAVCam 保存全屏捕获的图像
【发布时间】:2016-05-06 03:05:39
【问题描述】:

我正在使用 Apple 制造的 AVCam 用于我的自定义相机视图。老实说,如果您第一次看到 AVCamViewController 课堂上发生的事情,这并不容易理解。

现在我对他们如何设置捕获图像的框架感兴趣。我试图找到一些成名者或类似的东西,但我没有找到。

我在谷歌搜索并在这里找到答案AVCam not in fullscreen

但是当我实现该解决方案时,我才意识到它只是制作了与我的视图大小相同的实时相机预览层,但是当应用程序以方法- (IBAction)snapStillImage:(id)sender 保存图像时,图库中的图像仍然是从左侧开始的 2 个条纹和正确的。

我的问题是如何删除这些条纹或苹果在源代码的哪一行设置了这些东西?

还有一个额外的子问题,我如何设置类型只创建照片,因为应用程序要求我“麦克风设置”,我不需要它,只需要制作照片就可以了。

来自苹果的这段代码将图像保存到照片库。

- (IBAction)snapStillImage:(id)sender
{
    dispatch_async([self sessionQueue], ^{
        // Update the orientation on the still image output video connection before capturing.
        [[[self stillImageOutput] connectionWithMediaType:AVMediaTypeVideo] setVideoOrientation:[[(AVCaptureVideoPreviewLayer *)[[self previewView] layer] connection] videoOrientation]];

        // Flash set to Auto for Still Capture
        [AVCamViewController setFlashMode:AVCaptureFlashModeAuto forDevice:[[self videoDeviceInput] device]];

        // Capture a still image.
        [[self stillImageOutput] captureStillImageAsynchronouslyFromConnection:[[self stillImageOutput] connectionWithMediaType:AVMediaTypeVideo] completionHandler:^(CMSampleBufferRef imageDataSampleBuffer, NSError *error) {

            if (imageDataSampleBuffer)
            {
                NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer];

                UIImage *image = [[UIImage alloc] initWithData:imageData];

                UIImage *proccessingImage = [SAPAdjustImageHelper adjustImage:image];

                NSNumber *email_id = [SAPCoreDataEmailHelper currentEmailId];

                [SAPFileManagerHelper addImage:proccessingImage toFolderWithEmailId:email_id];
            }
        }];
    });
}

【问题讨论】:

  • 请添加代码或截图预览...
  • 根据我的经验,它在 iPhone5 上可以正常工作,但问题只在 iPhone 4 上,对吧???
  • 你可以在这里下载代码developer.apple.com/library/ios/samplecode/AVCam/Introduction/… 如上所述。我没有修改任何东西
  • 你在 iPhone 5 和 4 上测试过吗?
  • @hmdeep 我在 iPad 上测试它我不知道。它工作得很好,但有条纹,所以这意味着有 2 个黑色区域,当我保存捕获的图像时按下静止按钮,它有 2 个黑色阶梯,就像在电影院中一样))我不知道他们为什么不使用全帧,因为它提供相机,但将帧缩小到另一个尺寸

标签: ios objective-c avcapturesession avcam


【解决方案1】:

你在设置会话预设吗?

您可以将会话与AVCaptureSessionPresetPhoto 中设置的会话预设一起使用。

对于另一个子问题:您只需添加AVCaptureStillImageOutput 输出。

如何设置会话预设?

[session setSessionPreset:AVCaptureSessionPresetPhoto];

如何将会话配置为仅使用 StillImageOutput 来拍照和?

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Create the AVCaptureSession
    AVCaptureSession *session = [[AVCaptureSession alloc] init];
    [self setSession:session];

    // Setup the preview view
    [[self previewView] setSession:session];

    // Setup the session Preset          
    [session setSessionPreset:AVCaptureSessionPresetPhoto];

    // Check for device authorization
    [self checkDeviceAuthorizationStatus];

    dispatch_queue_t sessionQueue = dispatch_queue_create("session queue", DISPATCH_QUEUE_SERIAL);
    [self setSessionQueue:sessionQueue];

    dispatch_async(sessionQueue, ^{
        //[self setBackgroundRecordingID:UIBackgroundTaskInvalid];

        NSError *error = nil;

        AVCaptureDevice *videoDevice = [AVCamViewController deviceWithMediaType:AVMediaTypeVideo preferringPosition:AVCaptureDevicePositionBack];
        AVCaptureDeviceInput *videoDeviceInput = [AVCaptureDeviceInput deviceInputWithDevice:videoDevice error:&error];

        if (error)
        {
            NSLog(@"%@", error);
        }

        if ([session canAddInput:videoDeviceInput])
        {
            [session addInput:videoDeviceInput];
            [self setVideoDeviceInput:videoDeviceInput];

            dispatch_async(dispatch_get_main_queue(), ^{
                // Why are we dispatching this to the main queue?
                // Because AVCaptureVideoPreviewLayer is the backing layer for AVCamPreviewView and UIView can only be manipulated on main thread.
                // Note: As an exception to the above rule, it is not necessary to serialize video orientation changes on the AVCaptureVideoPreviewLayer’s connection with other session manipulation.

                [[(AVCaptureVideoPreviewLayer *)[[self previewView] layer] connection] setVideoOrientation:(AVCaptureVideoOrientation)[self interfaceOrientation]];
            });
        }

AVCaptureStillImageOutput *stillImageOutput = [[AVCaptureStillImageOutput alloc] init];
        if ([session canAddOutput:stillImageOutput])
        {
            [stillImageOutput setOutputSettings:@{AVVideoCodecKey : AVVideoCodecJPEG}];
            [session addOutput:stillImageOutput];
            [self setStillImageOutput:stillImageOutput];
        }
    });
}

【讨论】:

  • 好的,我今天会检查你的答案,非常感谢)我会将其标记为已接受的问题,让我测试一下)
  • 在我拍摄 2 或 1 张照片后由于某种原因应用程序崩溃
  • 所以我只是添加了一行 [session setSessionPreset:AVCaptureSessionPresetPhoto];它工作得很好,但是当应用程序开始调用 snapStillImage 应用程序崩溃时。删除这条线app拍照但有黑条
  • 我刚刚发现stackoverflow.com/questions/19179070/… 似乎也解决了崩溃问题
  • 抱歉耽搁了。是的,您还需要配置预览层。 :) 我不知道为什么代码会导致崩溃。也许我的剪切代码中有一个错误。
【解决方案2】:

需要黑色字段来保持纵横比,如果要避免它们,您应该更改 AVCaptureVideoPreviewLayer 中的 videoGravityUIImageView 中的 contentMode 以显示捕获的图像。
这是预期行为,不了解您的担忧。

【讨论】:

  • 好的,当我使用 UIIMagePickerController 时,我变得很棒!图像,没有任何条纹。这是我关心的问题,但是当运行苹果资源时,我得到了一半的图像而不是 100 大小。这是一个问题。视频重力还可以,但仅用于预览。在任何情况下,画廊中保存的图像都将是 iPad 屏幕的一半大小。我不使用任何 UIImageView。
  • 我添加了苹果用于将图像保存到库的代码,苹果提供的 AVCam 示例中是否有 UIImageView。你说的是哪个 contentMode?​​span>
  • 我还找到了这个链接stackoverflow.com/questions/20497575/avcam-not-in-fullscreen,但正如我所说,它只是为现场录制组件制作全屏。使用此方法将图像保存到库后 - (IBAction)snapStillImage:(id)sender 图像将具有 iPad 屏幕大小的一半。
  • 使用 AVCaptureConnection 捕获的图像可能具有不同的分辨率,具体取决于已设置为捕获会话的质量。我建议您导出图像并检查图像的分辨率。我可以肯定地告诉你,普通的 API 不会画背条纹
  • 好吧,让我们跳过条纹。我只想说,当我使用 UIImagePicker 时,我在 iPad 上的照片库中有全屏图像。或者如果在 iPad 上使用相机应用程序,它是内部应用程序。所有这些情况下的照片都比我通过 iPad 上的 AVCam 获得的最大。而且这段代码中根本没有UIImageView,大家可以下载确认一下。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-02
  • 1970-01-01
相关资源
最近更新 更多