gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 00:11:47 25 4
gpt4 key购买 nike

我已按照 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);
}
}
}

最佳答案

How to increase the quality of image after capture by using Xamarin camera?

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

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 .

关于android - 使用 Xamarin 相机拍摄后如何提高图像质量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48039609/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com