如何更正从 Android 设备捕获的图片的方向

此示例显示如何在 Android 设备上正确显示图像并将其显示。

首先,我们必须使用一个按钮和一个 imageview 创建示例应用程序。一旦用户点击按钮,相机就会启动,在用户选择图片后,它将以正确的方向显示在屏幕上。

  1. 添加名为 TakePictureButton 的按钮和名为 TakenPictureImageView 的 imageview:

StackOverflow 文档

  1. 现在打开活动代码:

这里首先参考你的控件:

ImageView _takenPictureImageView;
Button _takePictureButton;

    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        SetContentView(Resource.Layout.Main);

        _takenPictureImageView = FindViewById<ImageView>(Resource.Id.TakenPictureImageView);
        _takePictureButton = FindViewById<Button>(Resource.Id.TakePictureButton);

        _takePictureButton.Click += delegate 
        {
            takePicture();
        };
    }
  1. 在我们的应用程序中,我们将使用 Components Store 中提供的 Xamarin Mobile 组件:

StackOverflow 文档

  1. 将它添加到项目后,我们就可以继续了。添加以下代码,负责启动摄像头。应该在按钮单击中调用此方法,如上面的代码所示:

    void takePicture()
     {
         var picker = new MediaPicker(this);
         DateTime now = DateTime.Now;
         var intent = picker.GetTakePhotoUI(new StoreCameraMediaOptions
         {
             Name = "picture_" + now.Day + "_" + now.Month + "_" + now.Year + ".jpg",
             Directory = null
         });
         StartActivityForResult(intent, 1);
     }
    
  2. 一旦用户拍照,我们应该以正确的方向显示它。要使用以下方法。它负责从拍摄的图像中检索 exif 信息(包括拍照期间的方向),而不是创建具有正确方向的位图:

    Bitmap loadAndResizeBitmap(string filePath)
     {
         BitmapFactory.Options options = new BitmapFactory.Options { InJustDecodeBounds = true };
         BitmapFactory.DecodeFile(filePath, options);
    
         int REQUIRED_SIZE = 100;
         int width_tmp = options.OutWidth, height_tmp = options.OutHeight;
         int scale = 4;
         while (true)
         {
             if (width_tmp / 2 < REQUIRED_SIZE || height_tmp / 2 < REQUIRED_SIZE)
                 break;
             width_tmp /= 2;
             height_tmp /= 2;
             scale++;
         }
    
         options.InSampleSize = scale;
         options.InJustDecodeBounds = false;
         Bitmap resizedBitmap = BitmapFactory.DecodeFile(filePath, options);
    
         ExifInterface exif = null;
         try
         {
             exif = new ExifInterface(filePath);
             string orientation = exif.GetAttribute(ExifInterface.TagOrientation);
    
             Matrix matrix = new Matrix();
             switch (orientation)
             {
                 case "1": // landscape
                     break;
                 case "3":
                     matrix.PreRotate(180);
                     resizedBitmap = Bitmap.CreateBitmap(resizedBitmap, 0, 0, resizedBitmap.Width, resizedBitmap.Height, matrix, false);
                     matrix.Dispose();
                     matrix = null;
                     break;
                 case "4":
                     matrix.PreRotate(180);
                     resizedBitmap = Bitmap.CreateBitmap(resizedBitmap, 0, 0, resizedBitmap.Width, resizedBitmap.Height, matrix, false);
                     matrix.Dispose();
                     matrix = null;
                     break;
                 case "5":
                     matrix.PreRotate(90);
                     resizedBitmap = Bitmap.CreateBitmap(resizedBitmap, 0, 0, resizedBitmap.Width, resizedBitmap.Height, matrix, false);
                     matrix.Dispose();
                     matrix = null;
                     break;
                 case "6": // portrait
                     matrix.PreRotate(90);
                     resizedBitmap = Bitmap.CreateBitmap(resizedBitmap, 0, 0, resizedBitmap.Width, resizedBitmap.Height, matrix, false);
                     matrix.Dispose();
                     matrix = null;
                     break;
                 case "7":
                     matrix.PreRotate(-90);
                     resizedBitmap = Bitmap.CreateBitmap(resizedBitmap, 0, 0, resizedBitmap.Width, resizedBitmap.Height, matrix, false);
                     matrix.Dispose();
                     matrix = null;
                     break;
                 case "8":
                     matrix.PreRotate(-90);
                     resizedBitmap = Bitmap.CreateBitmap(resizedBitmap, 0, 0, resizedBitmap.Width, resizedBitmap.Height, matrix, false);
                     matrix.Dispose();
                     matrix = null;
                     break;
             }
    
             return resizedBitmap;
         }
    
         catch (IOException ex)
         {
             Console.WriteLine("An exception was thrown when reading exif from media file...:" + ex.Message);
             return null;
         }
     }
    
  3. 应该在用户拍照后调用的 OnActivityResult 方法中调用上面的方法:

    protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
     {
         base.OnActivityResult(requestCode, resultCode, data);
    
         if (requestCode == 1)
         {
             if (resultCode == Result.Ok)
             {
                 data.GetMediaFileExtraAsync(this).ContinueWith(t =>
                 {
                     using (Bitmap bmp = loadAndResizeBitmap(t.Result.Path))
                     {
                         if (bmp != null)
                         _takenPictureImageView.SetImageBitmap(bmp);
                     }
    
                 }, TaskScheduler.FromCurrentSynchronizationContext());
             }
         }
     }
    
  4. 启动应用程序。拍照看看结果:

http://i.stack.imgur.com/AFLk3.jpg http://i.stack.imgur.com/yU75s.jpg

而已。现在,你将以正确的方向显示所有拍摄的照片。