当我尝试通过我的应用程序绑定从相机拍摄的照片并将其绑定到图像视图并从图库中绑定图像时,我遇到了一些问题。 我使用Micromax手机测试我们的应用程序,它工作正常,并将图像绑定到手机上,但是当我们更换手机时,例如三星,原始方向与硬件方向不同,所以发生的事情是它具有约束力根据硬件设定的方向。 我试图纠正方向,但根本没有工作。 任何人都可以试着解决我的问题......
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Java.IO;
using Android.Graphics;
using Android.Provider;
using Android.Content.PM;
using MyApplication.Droid;
using Android.Media;
namespace MyApplication.Droid
{
[Activity(Label = "CameraActivity")]
public class CameraActivity : Activity
{
File _file;
File _dir;
Bitmap bitmap;
ImageView _imageView, _imageView1;
int PickImageId = 1;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.Camera);
if (IsThereAnAppToTakePictures())
{
CreateDirectoryForPictures();
Button button = FindViewById<Button>(Resource.Id.TakePik);
_imageView = FindViewById<ImageView>(Resource.Id.imageView2);
_imageView1= FindViewById<ImageView>(Resource.Id.imageView1);
button.Click += TakeAPicture;
}
Button button1 = FindViewById<Button>(Resource.Id.Galary);
button1.Click += delegate
{
Intent intent = new Intent();
intent.SetType("image/*");
intent.SetAction(Intent.ActionGetContent);
StartActivityForResult(Intent.CreateChooser(intent, "Select Picture"), PickImageId);
};
// Create your application here
}
private void CreateDirectoryForPictures()
{
_dir = new File(
Android.OS.Environment.GetExternalStoragePublicDirectory(
Android.OS.Environment.DirectoryPictures), "MRohit Task");
if (!_dir.Exists())
{
_dir.Mkdirs();
}
}
private bool IsThereAnAppToTakePictures()
{
Intent intent = new Intent(MediaStore.ActionImageCapture);
IList<ResolveInfo> availableActivities =
PackageManager.QueryIntentActivities(intent, PackageInfoFlags.MatchDefaultOnly);
return availableActivities != null && availableActivities.Count > 0;
}
private void TakeAPicture(object sender, EventArgs eventArgs)
{
Intent intent = new Intent(MediaStore.ActionImageCapture);
_file = new File(_dir, String.Format("MRohit_Task_{0}.jpg", Guid.NewGuid()));
intent.PutExtra(MediaStore.ExtraOutput, Android.Net.Uri.FromFile(_file));
StartActivityForResult(intent, 0);
}
protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
{if (requestCode == 1)
{
Android.Net.Uri uri = data.Data;
_imageView1.SetImageURI(uri);
}
else
{
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(_file);
mediaScanIntent.SetData(contentUri);
SendBroadcast(mediaScanIntent);
// string str_file_path = Android.Net.Uri.Parse(_file.Path.ToString()).ToString();
// 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;
bitmap = _file.Path.LoadAndResizeBitmap(width, height);
ExifInterface ei = new ExifInterface(_file.Path);
int orientation = ei.GetAttributeInt(ExifInterface.TagOrientation,
(int)Android.Media.Orientation.Undefined);
switch (orientation)
{
case (int)Android.Media.Orientation.Rotate90:
rotateImage(bitmap, 90);
break;
case (int)Android.Media.Orientation.Rotate180:
rotateImage(bitmap, 180);
break;
case (int)Android.Media.Orientation.Rotate270:
rotateImage(bitmap, 270);
break;
case (int)Android.Media.Orientation.Normal:
default:
break;
}
if (bitmap != null)
{
_imageView.SetImageBitmap(bitmap);
bitmap = null;
}
// Dispose of the Java side bitmap.
GC.Collect();
}
}
public static Bitmap rotateImage(Bitmap source, float angle)
{
Matrix matrix = new Matrix();
matrix.PostRotate(angle);
return Bitmap.CreateBitmap(source, 0, 0, source.Width, source.Height,
matrix, true);
}
}
//To crop the image size
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;
}
}
}