android - Xamarin.Forms 使用相机拍照显示方向错误并在后退按钮上崩溃

标签 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 上正确显示

最佳答案

我敢猜测这里有几个问题。 Android 上的 Xamarin.Forms.Labs 依赖项注入(inject)处理程序之一是 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;
    }

关于android - Xamarin.Forms 使用相机拍照显示方向错误并在后退按钮上崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26296889/

相关文章:

azure - 如何在运行时更改xamarin表单中的MainPage?

android - 如何从RecyclerView读取数据并在BottomSheet中发送

ios - 相机捕获叠加层上的动画角色

c# - Xamarin.Forms:在饼图布局中放置标签

mvvm - 在 ViewModel、Xamarin.Forms 中使用绑定(bind)在 StackLayout 中添加子项

iphone - iPhone 上 UIImagePickerController 和 AVCaptureSession 之间的摄像头差异

android - 我如何解决 Android 3.6 中的渲染 View 问题

java - Andengine 中 Sprite 闪烁

android - Dagger 2 : How to use @Provides and @Binds in same module

android - Camera Error "Can' t Connect to the Camera”或者在某些手机中出现错误 "Camera is using by another app"