Xamarin Forms Labs相机-永久保存图像并调用它们 [英] Xamarin Forms Labs Camera - Permanently Saving Images and Calling Them

查看:196
本文介绍了Xamarin Forms Labs相机-永久保存图像并调用它们的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我启用了摄像头功能,它也像我要求的那样在页面上显示图像.但是,是否有一种方法可以将图像永久保存在您的手机或其他地方,然后再调用它?

I got the camera function to work and it displays the image on the page like i asked it too. But is there a way to permanently save the image on your phone or somewhere else and then call it?

非常感谢您

推荐答案

以下是一些对我有用的代码.

Here's some code that works for me.

IFileAccess是我对System.IO.File函数的包装,例如文件打开,写入,检查是否存在.如果您要创建自己的文件服务,请查找Xamarin.Forms.Labs.Resolver以及如何使用它;如果使用共享的Forms项目类型,则可以直接从Forms项目访问System.IO.File.假设很清楚,以下

IFileAccess is my wrapper around System.IO.File functions such as file open, write, check if exsists. If you're making your own file service look up Xamarin.Forms.Labs.Resolver and how to use it; if you're using shared Forms project type you can access System.IO.File directly from the Forms project. Assuming that's clear, the following

var fileAccess = Resolver.Resolve<IFileAccess> (); 
mediaPicker.SelectPhotoAsync (new CameraMediaStorageOptions{ MaxPixelDimension = 1024 })
.ContinueWith(t=>{
  if (!t.IsFaulted && !t.IsCanceled) { 
    var mediaFile = t.Result;  

    var fileAccess = Resolver.Resolve<IFileAccess> ();
    string imageName = "IMG_" + DateTime.Now.ToString ("yy-MM-dd_HH-mm-ss") + ".jpg";

 // save the media stream to a file 
    fileAccess.WriteStream (imageName, mediaFile.Source);

 // use the stored file for ImageSource
    ImageSource imgSource = ImageSource.FromFile (fileAccess.FullPath (imageName)); 

    imgInXAML.Source = imgSource;
  }
});


有关IFileAccess的更多详细信息.


Further detail on IFileAccess.

在您的Forms项目中,创建一个如下所示的界面:

In your Forms project create an interface like this:

public interface IFileAccess
{
    bool Exists (string filename);
    string FullPath(string filename); 
    void WriteStream (string filename, Stream streamIn);
}

在您的iOS或Android或共享项目中,添加一个实现IFileAccess的类FileAccess:

In your iOS or Android or Shared project add a class FileAccess that implements IFileAccess:

public class FileAccess : IFileAccess
{ 
    public bool Exists (string filename)
    {
        var filePath = GetFilePath (filename);

        if (File.Exists (filePath)) {
            FileInfo finf = new FileInfo (filePath);
            return finf.Length > 0;
        } else
            return false;
    }

    public string FullPath (string filename)
    {
        var filePath = GetFilePath (filename);
        return filePath;
    }

    static string GetFilePath (string filename)
    {
        var documentsPath = Environment.GetFolderPath (Environment.SpecialFolder.Personal);
        var filePath = Path.Combine (documentsPath, filename);
        return filePath;
    }

    public void WriteStream (string filename, Stream streamIn)
    {
        var filePath = GetFilePath (filename);
        using (var fs = File.Create (filePath)) {
            streamIn.CopyTo (fs); 
        }
    }
}

如果您已经在使用Xamarin.Forms.Labs.Resolver,则仅添加用于注册服务的行,否则,在您的iOS或Android项目中找到对Forms.Init()的调用,并在添加之前进行

If you're already using Xamarin.Forms.Labs.Resolver then add only the line to register the service, otherwise in your iOS or Android project find a call to Forms.Init() and right before it add

var resolverContainer = new SimpleContainer ();
resolverContainer.Register<IFileAccess> (t => new FileAccess ()); // maybe just this line
Resolver.SetResolver (resolverContainer.GetResolver ());

这篇关于Xamarin Forms Labs相机-永久保存图像并调用它们的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆