Xamarin android app问题

时间:2016-09-30 05:12:06

标签: xamarin

我使用xamarin构建了一个应用程序,它使用移动相机捕获图像并将其保存在库中。所有工作正常,但我需要将图像的DPI设置为600像素,高度和宽度将为1200可以帮助我如何在保存到图库之前设置DPI。如果需要,我也会分享我的代码。

public class MainActivity : Activity

{
    public object ImageSource { get; private set; }

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

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

            Button button = FindViewById<Button>(Resource.Id.myButton);
            ImageView imageView1 = FindViewById<ImageView>(Resource.Id.imageView1);
            button.Click += TakeAPicture;
        }
    }
    public bool IsThereAnAppToTakePictures()
    {
        Intent intent = new Intent(MediaStore.ActionImageCapture);
        IList<ResolveInfo> availableActivities =
   PackageManager.QueryIntentActivities(intent, PackageInfoFlags.MatchDefaultOnly);
        return availableActivities != null && availableActivities.Count > 0;
    }
    private void CreateDirectoryForPictures()
    {
        App._dir = new File(
            Android.OS.Environment.GetExternalStoragePublicDirectory(
                Android.OS.Environment.DirectoryPictures), "CameraAppDemo");
        if (!App._dir.Exists())
        {
            App._dir.Mkdirs();
        }
    }
    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, Android.Net.Uri.FromFile(App._file));
        StartActivityForResult(intent, 0);
    }

    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);
        Android.Net.Uri contentUri = Android.Net.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.
        ImageView imageView1 = FindViewById<ImageView>(Resource.Id.imageView1);
        int height = Resources.DisplayMetrics.HeightPixels;
        int width = imageView1.Height;
        App.bitmap = App._file.Path.LoadAndResizeBitmap(width, height);



        if (App.bitmap != null)
        {

            imageView1.SetImageBitmap(App.bitmap);
            App.bitmap = null;
        }

        // Dispose of the Java side bitmap.
        GC.Collect();
    }

}
public static class App
{
    public static File _file;
    public static File _dir;
    public static Bitmap bitmap;
}



 public static class BitmapHelpers
    {
        public static Bitmap LoadAndResizeBitmap(this string fileName, int width, int height)
        {
            // First we get the 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;
        }
    }
}

0 个答案:

没有答案