使用Xamarin相机如何提高捕获后的图像质量?

时间:2017-12-31 02:59:06

标签: android visual-studio xamarin.android

我已按照https://www.youtube.com/watch?v=GaizJhx-FVg上的教程激活相机功能,但在捕获图像后,其质量变得非常低。关于如何调整图像质量的任何想法?

这是相机功能代码:

using Android.App;
using Android.Widget;
using Android.OS;
using System;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Graphics;

namespace Interactive_Ringgit_Recognizer_Try
{
    [Activity(Label = "Interactive_Ringgit_Recognizer", MainLauncher = true)]
    public class MainActivity : Activity
    {
        ImageView imageView;

        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.Main);

            var btnCamera = FindViewById<Button>(Resource.Id.btnCamera);
            imageView = FindViewById<ImageView>(Resource.Id.imageView);

            btnCamera.Click += BtnCamera_Click;
        }

        protected override void OnActivityResult(int requestCode,[GeneratedEnum] Result resultCode, Intent data)
        {
            base.OnActivityResult(requestCode, resultCode, data);
            Bitmap bitmap = (Bitmap)data.Extras.Get("data");
            imageView.SetImageBitmap(bitmap);
        }

        private void BtnCamera_Click(object sender, EventArgs e)
        {
            Intent intent = new Intent         (Android.Provider.MediaStore.ActionImageCapture);
            StartActivityForResult(intent, 0);
        }
    }
}

1 个答案:

答案 0 :(得分:1)

  

如何使用Xamarin相机拍摄后提高图像质量?

当您从相机拍摄照片时,您可以创建一个目录来保存照片,通过创建文件并将其Uri添加到意图中,相机应用程序会将生成的照片保存在此文件中。

externals

public static class App { public static Java.IO.File _file; public static Java.IO.File _dir; public static Bitmap bitmap; } private void TakeAPicture(object sender, EventArgs eventArgs) { Intent intent = new Intent(MediaStore.ActionImageCapture); App._file = new File(App._dir, String.Format("myPhoto_{0}.jpg", Guid.NewGuid())); intent.PutExtra(MediaStore.ExtraOutput, Uri.FromFile(App._file)); StartActivityForResult(intent, 0); } 方法中,我们会在设备图片库中提供照片。然后,您可以调用辅助方法来调整要显示的图像的大小。

OnActivityResult

protected override void OnActivityResult(int requestCode, Result resultCode, Intent data) { base.OnActivityResult(requestCode, resultCode, data); // Make it available in the gallery Intent mediaScanIntent = new Intent(Intent.ActionMediaScannerScanFile); Uri contentUri = Uri.FromFile(App._file); mediaScanIntent.SetData(contentUri); SendBroadcast(mediaScanIntent); // Display in ImageView. We will resize the bitmap to fit the display // Loading the full sized image will consume to much memory // and cause the application to crash. int height = Resources.DisplayMetrics.HeightPixels; int width = _imageView.Height; App.bitmap = App._file.Path.LoadAndResizeBitmap(width, height); if (App.bitmap != null) { _imageView.SetImageBitmap(App.bitmap); App.bitmap = null; } // Dispose of the Java side bitmap. GC.Collect(); } 方法:

LoadAndResizeBitmap

有关完整代码,您可以参考此official document