简体   繁体   English

我如何检测库图像是来自前置摄像头还是后置摄像头

[英]How can i detect the library image is from front camera or back camera

I know its not a good question to be ask but i am stuck. 我知道这不是一个好问题,但我被困住了。 How can i detect when user pick image from library not from camera and this library image saved via front camera or back camera? 我如何检测用户何时从库中而不是从相机中选择图像,并且该库图像是通过前置摄像头或后置摄像头保存的? Like 喜欢

if (library image from front camera)
{
  // Do something here
}  
else {
 // Do something here
} 

Your code checks for available cameras on the device. 您的代码检查设备上是否有可用的摄像头。 What you need to do is read the metadata for the image after you have taken the picture, that will include info on the camera. 您需要做的是在拍照读取图像的元数据,其中将包含相机上的信息。

Use this solution to read the Exif data that comes with the image to find out which camera obtained it: Exif Data from Image 使用此解决方案可以读取图像随附的Exif数据,以找出是哪个相机获得了它: 来自图像的Exif数据

You can check the image EXIF data in the info dictionary UIImagePicker passes in it's callback. 您可以在信息字典UIImagePicker的回调中检查图像EXIF数据。

- (IBAction) handleTakePhoto:(UIButton *)sender {

    UIImagePickerController* picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    picker.sourceType = UIImagePickerControllerSourceTypeCamera;

    [self presentViewController:picker animated:YES completion:nil];

}

-(void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {

    __block NSDictionary* metadata = [info objectForKey:UIImagePickerControllerMediaMetadata];

    dispatch_async(dispatch_get_main_queue(), ^{

        NSLog(@"%@", [metadata valueForKeyPath:@"{Exif}.LensModel"]);

        [picker dismissViewControllerAnimated:YES completion:nil];

    });

}

The above snippet outputs 上面的代码片段输出

iPhone 6 Plus back camera 4.15mm f/2.2 iPhone 6 Plus后置镜头4.15mm f / 2.2

You would have to parse out the "front" or "back" parts of the string. 您将不得不解析字符串的“前”或“后”部分。

Relying on parsing something that is parsed out of a string raises some red flags -- there is probably a better and more stable way of doing it. 依靠解析从字符串中解析出的内容会引发一些危险信号-可能有一种更好,更稳定的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM