gpt4 book ai didi

android - 需要一个使用 MonoDroid 和 MVVMCross 拍照的例子

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:57:43 25 4
gpt4 key购买 nike

谁能给我指出一个使用 MVVMCross 拍摄照片并存储它的示例?

我一直在寻找,但只找到了这个:

谢谢!!!

Resolved! Thanks!
To Future References: (Using Master Branch)
Credits to Stuart, I just changed the code to work with my reality

using Cirrious.MvvmCross.ExtensionMethods;
using Cirrious.MvvmCross.Interfaces.Platform.Tasks;
using Cirrious.MvvmCross.Interfaces.ServiceProvider;
using SIGEP.DummyService;
using SIGEP.Mobile.Core.Interfaces;


namespace SIGEP.Mobile.Core.Models
{
public class PhotoService : IMvxServiceConsumer<IMvxPictureChooserTask>
{
private const int MaxPixelDimension = 1024;
private const int DefaultJpegQuality = 92;

public void GetNewPhoto()
{
this.GetService<IMvxPictureChooserTask>().TakePicture(
MaxPixelDimension,
DefaultJpegQuality,
HandlePhotoAvailable,
() => { /* cancel is ignored */ });

}

public event EventHandler<PhotoStreamEventArgs> PhotoStreamAvailable;

private void HandlePhotoAvailable(Stream pictureStream)
{
var handler = PhotoStreamAvailable;
if (handler != null)
{
handler(this, new PhotoStreamEventArgs() { PictureStream = pictureStream, OnSucessGettingPhotoFileName = OnSucessGettingPhotoFileName });
}
}



public static void TakePhoto(Action<string> successFileName, Action<Exception> error)
{
var service = new PhotoService();
service.OnSucessGettingPhotoFileName = successFileName;
service.OnError = error;
service.GetNewPhoto();
service.PhotoStreamAvailable += new EventHandler<PhotoStreamEventArgs>(service_PhotoStreamAvailable);
}

static void service_PhotoStreamAvailable(object sender, PhotoStreamEventArgs e)
{
//grava pra ficheiro!!!
var directory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
var filename = Path.Combine(directory, "photo.jpeg");
string saveTo = filename;
FileStream writeStream = new FileStream(saveTo, FileMode.Create, FileAccess.Write);
ReadWriteStream(e.PictureStream, writeStream);

e.OnSucessGettingPhotoFileName(filename);

}
private static void ReadWriteStream(Stream readStream, Stream writeStream)
{
int Length = 256;
Byte[] buffer = new Byte[Length];
int bytesRead = readStream.Read(buffer, 0, Length);
// write the required bytes
while (bytesRead > 0)
{
writeStream.Write(buffer, 0, bytesRead);
bytesRead = readStream.Read(buffer, 0, Length);
}
readStream.Close();
writeStream.Close();
}

public Action<string> OnSucessGettingPhotoFileName { get; set; }
public Action<Exception> OnError { get; set; }
}

[Serializable]
[ComVisible(true)]
public class PhotoStreamEventArgs : EventArgs
{
public Stream PictureStream { get; set; }

public Action<string> OnSucessGettingPhotoFileName { get; set; }
}
}

最佳答案

我通常使用内置的 IMvxPictureChooserTask 实现服务(如果使用 vNext,则在插件中):

using Cirrious.MvvmCross.ExtensionMethods;
using Cirrious.MvvmCross.Interfaces.Platform.Tasks;
using Cirrious.MvvmCross.Interfaces.ServiceProvider;

public class PhotoService
: IMvxServiceConsumer<IMvxPictureChooserTask>
, IPhotoService
{
private const int MaxPixelDimension = 1024;
private const int DefaultJpegQuality = 92;

public void GetNewPhoto()
{
Trace.Info("Get a new photo started.");

this.GetService<IMvxPictureChooserTask>().TakePicture(
MaxPixelDimension,
DefaultJpegQuality,
HandlePhotoAvailable,
() => { /* cancel is ignored */ });
}

public event EventHandler<PhotoStreamEventArgs> PhotoStreamAvailable;

private void HandlePhotoAvailable(Stream pictureStream)
{
Trace.Info("Picture available");
var handler = PhotoStreamAvailable;
if (handler != null)
{
handler(this, new PhotoStreamEventArgs() { PictureStream = pictureStream });
}
}
}

我通常在启动期间将此服务注册为单例,然后从 ViewModel ICommand 处理程序调用它。


使用此服务的一个应用程序是 Blooor sample - 请参阅 BaseEditProductViewModel.cs - 这与我无关,但我相信它带来了拍照和 ZXing - 两者都使用外部服务。


一个警告:在 MonoDroid 上,您可以看到一些奇怪/意外的 Activity/ViewModel 生命周期行为 - 基本上您可以看到您从中拍摄照片的 Activity 在拍照期间从内存中卸载/删除。如果您的应用出现这种情况,那么您可能需要开始研究以下问题:Saving Android Activity state using Save Instance State - 这不会在 MvvmCross 中自动处理(还)。

我相信 Blooor 样本可能会遇到这个问题 - 但用户是否会在正常的应用程序使用中看到它是值得商榷的。


作为 IMvxPictureChooserTask 服务的替代方案,您还可以查看使用 Xamarin.Mobile 中的一些跨平台 API - 请参阅 MvvmCross vnext : monodroid use a VideoView inside a plugin对于可能的起点 - 或者仅适用于 Android,您可以轻松实现自己的。

关于android - 需要一个使用 MonoDroid 和 MVVMCross 拍照的例子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13475896/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com