简体   繁体   English

如何检测照片是否来自前置摄像头

[英]How to detect if photo taken from front camera

I want to change orientation of photo taken from front camera vertically with following code:我想使用以下代码垂直更改从前置摄像头拍摄的照片的方向:

let reversedImage = UIImage(CGImage: image.CGImage!, scale: 1.0, orientation: .LeftMirrored)

But, how can I detect if photo taken from front camera?但是,如何检测照片是否来自前置摄像头? I tried following code but it didn't work:我尝试了以下代码,但没有用:

     let availableCameraDevices = AVCaptureDevice.devicesWithMediaType(AVMediaTypeVideo)
                for device in availableCameraDevices as! [AVCaptureDevice] {
                    if device.position == .Back {
                        let reversedImage = UIImage(CGImage: image.CGImage!, scale: 1.0, orientation: .LeftMirrored)

                        sp.pickedPhoto = reversedImage

                    }
                    else if device.position == .Front {
                        sp.pickedPhoto = image


                    }
                }

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 Data from Image

In Swift, you can do it this way:在 Swift 中,你可以这样做:

func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage!, editingInfo: [NSObject : AnyObject]!) {

    self.dismissViewControllerAnimated(false, completion: { () -> Void in

    })

    if picker.cameraDevice == .Front
    {
        print("Image taken from front camera")
    }
    else if picker.cameraDevice == .Rear
    {
        print("Image taken from back camera")
    }
}

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.依赖于解析从字符串中解析出来的东西会引发一些危险信号——可能有更好、更稳定的方法来做这件事。

Extract the EXIF data from the image从图像中提取 EXIF 数据

Facetime Camera: Facetime相机:

"{Exif}" =     {
        ColorSpace = 65535;
        PixelXDimension = 3088;
        PixelYDimension = 2320;
    };

Main Camera:主摄像头:

"{Exif}" =     {
        ColorSpace = 65535;
        PixelXDimension = 4032;
        PixelYDimension = 3024;
    };

Use this helper for extracting EXIF data使用此助手提取 EXIF 数据

extension UIImage {

    func getExifData() -> CFDictionary? {
        var exifData: CFDictionary? = nil
        if let data = self.jpegData(compressionQuality: 1.0) {
            data.withUnsafeBytes {
                let bytes = $0.baseAddress?.assumingMemoryBound(to: UInt8.self)
                if let cfData = CFDataCreate(kCFAllocatorDefault, bytes, data.count),
                    let source = CGImageSourceCreateWithData(cfData, nil) {
                    exifData = CGImageSourceCopyPropertiesAtIndex(source, 0, nil)
                }
            }
        }
        return exifData
    }
}

Source: https://stackoverflow.com/a/54230367/6576315来源: https ://stackoverflow.com/a/54230367/6576315

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

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