簡體   English   中英

如果使用 UIImagePickerControllerSourceTypeCamera 從相機拍攝圖像,則不會檢測到圖像

[英]Image is not detected if taken from camera Using UIImagePickerControllerSourceTypeCamera

我正在從相機中獲取圖像(使用UIImagePickerController )並將其保存到文檔目錄中。

然后我使用CIDetector APICIfacefeature API在不同的視圖 controller 中獲取這些圖像以獲取面部部分。

問題是雖然我能夠正確獲取圖像,但它根本沒有檢測到面部。 如果我將相同的圖像存儲在它檢測到的主包中。

不知道問題出在哪里??。 我已經嘗試了一切。 問題可能出在UIImage上,或者可能是圖像保存在文檔目錄或相機中的格式。

請幫忙。 我會很感激你的。

- (void) imagePickerController:(UIImagePickerController *)picker       didFinishPickingMediaWithInfo:(NSDictionary *)info
{
   UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
   NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 
                                                         NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString* path = [documentsDirectory stringByAppendingPathComponent: 
                      [NSString stringWithString: @"SampleImage.jpg"] ];
    NSData* data = UIImageJPEGRepresentation(image, 0);
    [data writeToFile:path atomically:YES];
    [picker  dismissModalViewControllerAnimated:YES];
    FCVC *fcvc = [[FCVC alloc] initwithImage:image];

    [self.navigationController pushViewController:fcvc animated:YES];
}

在 FCVC 的 ViewDidLoad 中,我通過以下方式調用 function:

-(void)markFaces:(UIImage *)pic 
{
    CIImage* image = [CIImage imageWithCGImage:pic.CGImage];
    CGImageRef masterFaceImage;

    CIDetector* detector = [CIDetector detectorOfType: CIDetectorTypeFace
                                          context:nil options:[NSDictionary dictionaryWithObject:CIDetectorAccuracyHigh forKey:CIDetectorAccuracy]];

// create an array containing all the detected faces from the detector    
   NSArray* features = [detector featuresInImage:image];
   for(CIFaceFeature* faceFeature in features)
   {       
        masterFaceImage  =  CGImageCreateWithImageInRect(facePicture.CGImage,CGRectMake(faceFeature.bounds.origin.x,faceFeature.bounds.origin.y, faceFeature.bounds.size.width,faceFeature.bounds.size.height));
   }
   self.masterExtractedFace = [UIImage imageWithCGImage:masterFaceImage];
}

提前致謝。

一個簡單的解決方法是,如果您始終以縱向使用相機,則添加以下小片段:

NSDictionary* imageOptions = [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:6] forKey:CIDetectorImageOrientation];
NSArray* features = [detector featuresInImage:image options:imageOptions];

如果您需要它來動態確定您的方向,請確定您所處的方向。

檢查kCGImagePropertyOrientation

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM