0

我需要使用便携式 xamarin.forms 从我的相机捕获图像,并获得对图像字节 [] 数据的访问权限以进行图像处理。

如何才能做到这一点?

我有捕获图像并简单地显示它的工作代码,使用 xlabs

public async Task<MediaFile> TakePicture()
    {
        Setup ();

        ImageSource = null;

        return await _Mediapicker.TakePhotoAsync (new CameraMediaStorageOptions {
            DefaultCamera = CameraDevice.Front, MaxPixelDimension = 400
        }).ContinueWith (t => {
            if (t.IsFaulted)
            {
                Status = t.Exception.InnerException.ToString();
            }
            else if (t.IsCanceled)
            {
                Status = "Canceled";
            }
            else
            {
                MediaFile mediaFile = t.Result;
                ImageSource = ImageSource.FromStream(() => mediaFile.Source);

                return mediaFile;
            }

            return null;
        }, _scheduler);
    }

    private async void buttonTakePicture_Clicked() {
        await cameraViewModel.TakePicture();
        imageView.Source = cameraViewModel.ImageSource;
    }

单击按钮启动cameraViewModel.TakePicture(),然后使用 xlabs 在设备上实际拍摄照片。

如何更改代码以同时提供图像原始数据(或完全使用不同的代码)?

谢谢

4

2 回答 2

0

这将帮助您在 PCL 中获取 byte[ ] :

public async Task<MediaFile> TakePicture()
{
    Setup ();
    ImageSource = null;
    return await _Mediapicker.TakePhotoAsync (new CameraMediaStorageOptions {
        DefaultCamera = CameraDevice.Front, MaxPixelDimension = 400
    }).ContinueWith (t => {
        if (t.IsFaulted)
        {
            Status = t.Exception.InnerException.ToString();
        }
        else if (t.IsCanceled)
        {
            Status = "Canceled";
        }
        else
        {
            MediaFile mediaFile = t.Result;
            using (MemoryStream ms = new MemoryStream())
            {
                mediaFile.Source.CopyTo(ms);
                byte[] attchmentbytes = ms.ToArray();
            }
            ImageSource = ImageSource.FromStream(() => mediaFile.Source);
            return mediaFile;
        }
        return null;
    }, _scheduler);
}
于 2016-12-27T05:49:43.677 回答
0

您可以mediaFile.Path将其作为 DependencyService 的参数传递:

//For Android
public byte[] ByteArrayFromStream(string path)
{
    var bitmap = BitmapFactory.DecodeFile(path);

    byte[] bitmapData;
    using(stream = new MemoryStream())
    {
        bitmap.Compress(Bitmap.CompressFormat.Png, 100, stream);
        bitmapData = stream.ToArray();
    }

    return bitmapData;
}

//For iOS
public byte[] ByteArrayFromStream(string path)
{
    UIImage originalImage = UIImage.FromFile(path);
    return originalImage.AsPNG().ToArray();
}

要了解有关 DependencyService 的更多信息,您可以访问以下位置的文档: https ://developer.xamarin.com/guides/xamarin-forms/dependency-service/

于 2016-12-26T23:40:59.727 回答