简体   繁体   中英

Xamarin iOS camera and photos

I am taking a picture with the iOS camera and trying to extract metadata from the image. This is my code:-

partial void BtnCamera_TouchUpInside(UIButton sender)
        {
            UIImagePickerController imagePicker = new UIImagePickerController();
            imagePicker.PrefersStatusBarHidden();
            imagePicker.SourceType = UIImagePickerControllerSourceType.Camera;

            // handle saving picture and extracting meta-data from picture //
            imagePicker.FinishedPickingMedia += Handle_FinishedPickingMedia;

            // present //
            PresentViewController(imagePicker, true, () => { });         
        }


protected void Handle_FinishedPickingMedia(object sender, UIImagePickerMediaPickedEventArgs e)
        {
            try
            {
                // determine what was selected, video or image
                bool isImage = false;
                switch (e.Info[UIImagePickerController.MediaType].ToString())
                {
                    case "public.image":
                        isImage = true;
                        break;
                }

                // get common info 
                NSUrl referenceURL = e.Info[new NSString("UIImagePickerControllerReferenceURL")] as NSUrl;
                if (referenceURL != null)
                    Console.WriteLine("Url:" + referenceURL.ToString());

I am able to initiate the camera, take the picture and then however when I click 'use photo'... The referenceURL comes back as NULL... How can I get the url, such that to extract GPS coordinates of the photo and such other attributes ?

I had a tremendous amount of trouble with the URL. It can be a file, it can be a web url and it acts different on every device. My app crashed and burned so many times with my test group. I finally found a way to get the Metadata from the data. There are multiple ways to get the DateTaken, Width and Height as well as GPS Coordinates. In addition, I needed the Camera MFG and the Model.

string dateTaken = string.Empty;
string lat = string.Empty;
string lon = string.Empty;
string width = string.Empty;
string height = string.Empty;
string mfg = string.Empty;
string model = string.Empty;

PHImageManager.DefaultManager.RequestImageData(asset, options, (data, dataUti, orientation, info) => {

    dateTaken = asset.CreationDate.ToString();

    // GPS Coordinates
    var coord = asset.Location?.Coordinate;
    if (coord != null)
    {
        lat = asset.Location?.Coordinate.Latitude.ToString();
        lon = asset.Location?.Coordinate.Longitude.ToString();
    }

    UIImage img = UIImage.LoadFromData(data);
    if (img.CGImage != null)
    {
        width = img.CGImage?.Width.ToString();
        height = img.CGImage?.Height.ToString();
    }
    using (CGImageSource imageSource = CGImageSource.FromData(data, null))
    {
        if (imageSource != null)
        {
            var ns = new NSDictionary();
            var imageProperties = imageSource.CopyProperties(ns, 0);
            if (imageProperties != null)
            {
                width = ReturnStringIfNull(imageProperties[CGImageProperties.PixelWidth]);
                height = ReturnStringIfNull(imageProperties[CGImageProperties.PixelHeight]);

                var tiff = imageProperties.ObjectForKey(CGImageProperties.TIFFDictionary) as NSDictionary;
                if (tiff != null)
                {
                    mfg = ReturnStringIfNull(tiff[CGImageProperties.TIFFMake]);
                    model = ReturnStringIfNull(tiff[CGImageProperties.TIFFModel]);
                    //dateTaken = ReturnStringIfNull(tiff[CGImageProperties.TIFFDateTime]);
                }
            }
        }
    }
}

}

The little helper function

private string ReturnStringIfNull(NSObject inObj)
{
    if (inObj == null) return String.Empty;
    return inObj.ToString();
}

You can request a PHAsset from the reference Url and that will contain some metadata. You can request the image data to obtain more.

Note: If you need full EXIF, you need to check to ensure the image on on the device (could be iCloud-based), download it if needed, and then load the image data with the ImageIO framework (lots of SO postings cover this).

public void ImagePicker_FinishedPickingMedia(object sender, UIImagePickerMediaPickedEventArgs e)
{
    void ImageData(PHAsset asset)
    {
        if (asset == null) throw new Exception("PHAsset is null");
        PHImageManager.DefaultManager.RequestImageData(asset, null, (data, dataUti, orientation, info) =>
        {
            Console.WriteLine(data);
            Console.WriteLine(info);
        });
    }

    PHAsset phAsset;
    if (e.ReferenceUrl == null)
    {
        e.OriginalImage?.SaveToPhotosAlbum((image, error) =>
        {
            if (error == null)
            {
                var options = new PHFetchOptions
                {
                    FetchLimit = 1,
                    SortDescriptors = new[] { new NSSortDescriptor("creationDate", true) }
                };
                phAsset = PHAsset.FetchAssets(options).FirstOrDefault() as PHAsset;
                ImageData(phAsset);
            }
        });
    }
    else
    {
        phAsset = PHAsset.FetchAssets(new[] { e.ReferenceUrl }, null).FirstOrDefault() as PHAsset;
        ImageData(phAsset);
    }
}

Note : Make sure you have request runtime photo library authorization PHPhotoLibrary.RequestAuthorization ) and have set the Privacy - Photo Library Usage Description string in your info.plist to avoid a nasty privacy crash

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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