代码之家  ›  专栏  ›  技术社区  ›  Philo

Xamarin iOS摄像头和照片

  •  0
  • Philo  · 技术社区  · 7 年前

    我正在用iOS摄像头拍照,并试图从图像中提取元数据。这是我的代码:-

    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());
    

    我可以启动相机,拍照,然后单击“使用照片”。。。referenceURL返回为NULL。。。如何获取url,以便提取照片的GPS坐标和其他属性?

    2 回复  |  直到 7 年前
        1
  •  1
  •   Linda Rawson    4 年前

    我在URL上遇到了很多麻烦。它可以是一个文件,也可以是一个web url,并且在每个设备上的行为都不同。我的应用程序在我的测试组中崩溃并烧毁了很多次。我终于找到了从数据中获取元数据的方法。有多种方法可以获取拍摄日期、宽度和高度以及GPS坐标。此外,我还需要相机制造商和型号。

    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]);
                    }
                }
            }
        }
    }
    

    }

    小助手函数

    private string ReturnStringIfNull(NSObject inObj)
    {
        if (inObj == null) return String.Empty;
        return inObj.ToString();
    }
    
        2
  •  0
  •   SushiHangover    7 年前

    您可以请求 PHAsset 来自引用Url,并且将包含一些元数据。您可以请求图像数据以获取更多信息。

    注意:如果需要完全EXIF,则需要检查以确保设备上的图像(可以是基于iCloud的),如果需要,请下载图像,然后使用 ImageIO 框架(许多SO帖子都涵盖了这一点)。

    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);
        }
    }
    

    笔记 :确保您具有请求运行时照片库授权 PHPhotoLibrary.RequestAuthorization )并设置了 Privacy - Photo Library Usage Description 输入您的信息。plist可避免严重的隐私崩溃

    推荐文章