我已经按照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);
}
}
}
使用Xamarin相机如何提高捕获后的图像质量?
从相机拍摄照片时,您可以创建一个目录来保存照片,通过创建文件并将其Uri添加到意图中,相机应用程序会将生成的照片保存在此文件中。
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
方法:
public static Bitmap LoadAndResizeBitmap(this string fileName, int width, int height)
{
// First we get the dimensions of the file on disk
BitmapFactory.Options options = new BitmapFactory.Options { InJustDecodeBounds = true };
BitmapFactory.DecodeFile(fileName, options);
// Next we calculate the ratio that we need to resize the image by
// in order to fit the requested dimensions.
int outHeight = options.OutHeight;
int outWidth = options.OutWidth;
int inSampleSize = 1;
if (outHeight > height || outWidth > width)
{
inSampleSize = outWidth > outHeight
? outHeight / height
: outWidth / width;
}
// Now we will load the image and have BitmapFactory resize it for us.
options.InSampleSize = inSampleSize;
options.InJustDecodeBounds = false;
Bitmap resizedBitmap = BitmapFactory.DecodeFile(fileName, options);
return resizedBitmap;
}
对于完整的代码,您可以参考这个official document。