【问题标题】:Raw image data from camera来自相机的原始图像数据
【发布时间】:2012-05-22 08:49:11
【问题描述】:

我一直在搜索这个论坛,但我找不到我真正需要的东西。我想从相机获取原始图像数据。到目前为止,我试图从该方法 captureStillImageAsynchronouslyFromConnection:completionHandler: 中获取 imageDataSampleBuffer 中的数据并将其写入 NSData 对象,但这没有用。 也许我走错了路,或者我只是做错了。 我不想要以任何方式压缩图像。

简单的方法是使用AVCaptureStillImageOutput 中的jpegStillImageNSDataRepresentation:,但就像我说的那样,我不希望它被压缩。

谢谢!

【问题讨论】:

  • 参考this链接。

标签: iphone objective-c ios image camera


【解决方案1】:

这就是我的做法:

1:我首先使用以下方式打开相机:

- (void)openCamera
{
    imagePicker = [[UIImagePickerController alloc] init];
    imagePicker.delegate = self;

    if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
    {        
        imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
        imagePicker.mediaTypes = [NSArray arrayWithObjects:
                              (NSString *) kUTTypeImage,
                              (NSString *) kUTTypeMovie, nil];
        imagePicker.allowsEditing = NO;

        [self presentModalViewController:imagePicker animated:YES];
    }
    else {
        lblError.text = NSLocalizedStringFromTable(@"noCameraFound", @"Errors", @"");  
    }
}

拍照时调用此方法:

 -(void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    // Save and get the path of the image
    NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
    ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
    if([mediaType isEqualToString:(NSString *)kUTTypeImage]) 
    {
        // Save the image
        image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
        [library writeImageToSavedPhotosAlbum:[image CGImage] orientation:(ALAssetOrientation)[image imageOrientation] completionBlock:^(NSURL *assetURL, NSError *error){
            if(!error) {
               //Save path and location to database
               NSString *pathLocation = [[NSString alloc] initWithFormat:@"%@", assetURL];
            } else {
                NSLog(@"CameraViewController: Error on saving image : %@ {imagePickerController}", error);
            }
        }];

    }
    [imagePicker dismissModalViewControllerAnimated:YES];
}

然后通过该路径,我以全分辨率从库中获取图片(使用“1”):

 -(void)preparePicture: (NSString *) filePathPicture{
    ALAssetsLibraryAssetForURLResultBlock resultBlock = ^(ALAsset *myasset)
    {
        if(myasset != nil){
            ALAssetRepresentation *assetRep = [myasset defaultRepresentation];
            CGImageRef imageRef = [assetRep fullResolutionImage];
            if (imageRef) {
                NSData *imageData = UIImageJPEGRepresentation([UIImage imageWithCGImage:imageRef], 1);
            }
        }else {
            //error
        }
     };

    ALAssetsLibraryAccessFailureBlock failureBlock  = ^(NSError *error)
    {      
         NSString *errorString = [NSString stringWithFormat:@"can't get image, %@",[error localizedDescription]];
        NSLog(@"%@", errorString);
    };

     if(filePathPicture && [filePathPicture length])
     {
         NSURL *assetUrl = [NSURL URLWithString:filePathPicture];
         ALAssetsLibrary *assetslibrary = [[ALAssetsLibrary alloc] init];
        [assetslibrary assetForURL:assetUrl 
                   resultBlock:resultBlock
                  failureBlock:failureBlock];
    }
}

希望这对您有所帮助:-)。

【讨论】:

  • 当我使用值 1 时 UIImageJPEGRepresentation 是否没有压缩?那真是太棒了
  • 在我看来,不是。 1 是最高值,不会导致压缩。
  • 如果您阅读 Apple 文档中的“UIImageJPEGRepresentation”值 0.0 表示最大压缩(或最低质量),而值 1.0 表示最小压缩(或最佳质量)。 .是的,它被压缩了。
猜你喜欢
  • 2012-07-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-07
  • 1970-01-01
  • 1970-01-01
  • 2011-05-20
  • 1970-01-01
相关资源
最近更新 更多