Xamarin.Forms用相机拍摄照片显示错误的方向和后退按钮崩溃

时间:2014-10-10 10:06:44

标签: android camera xamarin.forms

我正在使用此处的Xamarin.Forms相机示例 - https://github.com/XForms/Xamarin-Forms-Labs-Samples/tree/master/XF.Labs.CameraSample我可以选择或拍照。

private async Task SelectPicture()
{
    mediaPicker = DependencyService.Get<IMediaPicker>();
    imageSource = null;
        var mediaFile = await mediaPicker.SelectPhotoAsync(new CameraMediaStorageOptions
            {
                DefaultCamera = CameraDevice.Front,
                MaxPixelDimension = 400
            });
        imageSource = ImageSource.FromStream(() => mediaFile.Source);
        img.Source  = imageSource;

}


private async Task TakePicture()
{
    mediaPicker = DependencyService.Get<IMediaPicker>();
    imageSource = null;
        var mediaFile = await mediaPicker.TakePhotoAsync(new CameraMediaStorageOptions
            {
                DefaultCamera = CameraDevice.Front,
                MaxPixelDimension = 400
            });
        imageSource = ImageSource.FromStream(() => mediaFile.Source);
        img.Source  = imageSource;

}

图像的代码就是

    img = new Image
    {
        BackgroundColor = Color.White,
        Aspect = Aspect.AspectFit
    };    

有几个问题:

第一个。您可以拍照或选择存储的照片然后将其显示在页面上。如果选择照片,则会正确显示照片,无论是纵向还是横向。拍摄照片时,它仅以横向模式显示,因此如果图像是以纵向拍摄的,则图像会显示在侧面。这不是灾难性的,但最好显示图像的拍摄方式。

第二个问题有点激烈,如果您在相机或图库中按下设备的后退按钮,则屏幕变为空白然后您会收到一条消息,指出该应用已停止响应。

到目前为止,我只在Android上试过这个。有没有人对如何解决上述问题有任何想法?

编辑:我已设法修复了后退按钮上的崩溃,但图片仍显示在Android的侧面,但正确显示iOS

2 个答案:

答案 0 :(得分:1)

我冒昧地猜测这里有几个问题。 Android上的一个Xamarin.Forms.Labs依赖注入处理程序是1)不检查所需的旋转,2)不检查外部存储或不处理onActivityCancelled。

解决问题的简单方法是使用Xamarin.Mobile Xamarin.Mobile我无法100%确认它会处理所有内容,但如果确实如此,这将是一个快速简便的解决方案。

更难以控制的选项是推出自己的平台特定实现。我不打算知道DI如何运作你知道或可以看到Accessing Native Features

以下是Android拍照的示例,其中包含用于确定存储是否在外部以及是否需要轮换的代码。

    protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
    {


        base.OnActivityResult(requestCode, resultCode, data);
        //FinishActivity(requestCode);
        try
        {

        if (resultCode == Result.Ok)
        {
            switch (requestCode)
            {
                case TAKE_PHOTO:
                    {
                        Java.IO.File photo = null;

                        if (isMounted)
                        {

                        photo = new Java.IO.File(Android.OS.Environment.ExternalStorageDirectory.ToString(), SharedLibrary.strPhotoLocation);

                        }
                        else
                        {
                        photo = new Java.IO.File(CacheDir, SharedLibrary.strPhotoLocation);
                        }


                        BitmapFactory.Options options = new BitmapFactory.Options();
                        options.InJustDecodeBounds = true;
                        options.InSampleSize = 4;
                        options.InPurgeable = true;
                        options.InInputShareable = true;                          

                        try
                        {
                            //Cleanup code... removed this in favor of using options.InJustDecodeBounds to get info about the bitmap
                            //instead of creating it twice in memory

                            //Bitmap imageBitmap = BitmapFactory.DecodeFile(photo.AbsolutePath, options);


                            //int w = imageBitmap.Width;
                            //int h = imageBitmap.Height;

                            BitmapFactory.DecodeFile(photo.AbsolutePath, options);
                            int w = options.OutWidth;
                            int h = options.OutHeight;

                            Matrix matrix = new Matrix();
                            matrix.SetRotate(getNeededRotation(photo.AbsolutePath));

                            options.InJustDecodeBounds = false;

                            //Bitmap imageBitmap = Bitmap.CreateBitmap(BitmapFactory.DecodeFile(photo.AbsolutePath, options), 0, 0, w, h, matrix, false);                                                       
                            Bitmap imageBitmap = Bitmap.CreateScaledBitmap(BitmapFactory.DecodeFile(photo.AbsolutePath, options), w, h, false);
                            //...

安装

     private System.Boolean isMounted
    {
        get
        {
            return       (System.Boolean)Android.OS.Environment.ExternalStorageState.Equals(Android.OS.Environment.MediaMounted);
        }
    }                               

<强> GetRotationNeeded

    private int getNeededRotation(string filepath)
    {
        ExifInterface exif = new ExifInterface(filepath);
        int orientation = exif.GetAttributeInt(ExifInterface.TagOrientation, -1);
        int rotate = 0;
        switch (orientation)
        {
            case 6:
                {
                    rotate = 90;
                    break;
                }
            case 3:
                {
                    rotate = 180;
                    break;
                }
            case 8:
                {
                    rotate = 270;
                    break;
                }
            default:
                {
                    rotate = 0;
                    break;
                }


        }
        exif.Dispose();
        return rotate;
    }

答案 1 :(得分:1)

您可以使用&#39; mediaFile.Exif.Orientation&#39;来检查方向。检查它是纵向还是横向。(&#39; TopLeft&#39; - &gt;纵向。)然后在保存前设置照片方向,

&#13;
&#13;
 mediaFile.Exif.Orientation = ExifLib.ExifOrientation.TopLeft;
&#13;
&#13;
&#13;