我从自定义相机中获取旋转图片,我保存图像以获取在ExifInterface上使用它的路径,并使其成为我在ExifInterface上使用的正确方向,但它始终返回值为0,可能是什么问题? (我运行模拟器)
谢谢
这是我的OnPictureTaken方法:
public async void OnPictureTaken(byte[] data, Android.Hardware.Camera camera)
{
var targetWidth = 1080;
var cameraParams = camera.GetParameters();
var ratio = ((decimal)cameraParams.PictureSize.Height) / cameraParams.PictureSize.Width;
var w = cameraParams.PictureSize.Width >= targetWidth
? targetWidth
: cameraParams.PictureSize.Width;
var h = cameraParams.PictureSize.Width >= targetWidth
? (int)Math.Floor(targetWidth * ratio)
: cameraParams.PictureSize.Height;
Java.IO.File sdDir = Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryPictures);
Java.IO.File pictureFileDir = new Java.IO.File(sdDir, "RDCCameraImages");
if (!pictureFileDir.Exists() && !pictureFileDir.Mkdirs())
{
return;
}
String photoFile = "Picture_" + "aaa.jpg";
String imageFilePath = pictureFileDir.Path + Java.IO.File.Separator + photoFile;
Java.IO.File pictureFile = new Java.IO.File(imageFilePath);
FileOutputStream fos = new FileOutputStream(pictureFile);
fos.Write(data);
fos.Close();
ExifInterface exif;
ExifInterface ei = new ExifInterface(pictureFile.AbsolutePath);
int orientation = ei.GetAttributeInt(ExifInterface.TagOrientation, (int)Android.Media.Orientation.Normal);//return 0
var imageStream = new System.IO.MemoryStream();
using (var bm = BitmapFactory.DecodeByteArray(data, 0, data.Length))
using (var scaled = Bitmap.CreateScaledBitmap(bm, w, h, false))
{
Bitmap rotatedBitmap = null;
switch (orientation)
{
case (int)Android.Media.Orientation.Rotate90:
rotatedBitmap = rotateImage(scaled, 90);
break;
case (int)Android.Media.Orientation.Rotate180:
rotatedBitmap = rotateImage(scaled, 180);
break;
case (int)Android.Media.Orientation.Rotate270:
rotatedBitmap = rotateImage(scaled, 270);
break;
case (int)Android.Media.Orientation.Normal:
rotatedBitmap = scaled;
break;
default:
rotatedBitmap = scaled;
break;
}
await scaled.CompressAsync(Bitmap.CompressFormat.Jpeg, 90, imageStream);
await imageStream.FlushAsync();
}
}
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);
}
答案 0 :(得分:0)
ExifInterface,并使其成为我在ExifInterface上使用的正确方向,但它总是返回值为0,可能是什么问题?
ExifInterface并非如此。在您的代码中,您刚刚创建了一张新图片并将其保存在您的设备中,如果我理解正确,您希望ExifInterface
自动检测方向。好吧,它不是那么聪明,ExifInterface
只能使用ExifInterface.SetAttribute
获取我们之前保存到该图像中的信息:
ExifInterface ei = new ExifInterface(pictureFile.AbsolutePath);
//set the attribute
ei.SetAttribute(ExifInterface.TagOrientation, "90");
//save the attributes
ei.SaveAttributes();
//For Test I created a totally new ExifInterface
ExifInterface ei2 = new ExifInterface(pictureFile.AbsolutePath);
//we get the Information of previously saved orientation
var orientationInfo = ei2.GetAttribute(ExifInterface.TagOrientation);
//orientationInfo=90
如何获取相机定位?
您可以使用CameraInfo获取当前相机的方向:
int cameraId=0;
CameraInfo cameraInfo=null;
for (int i = 0; i < Camera.NumberOfCameras; i++)
{
CameraInfo info = new CameraInfo();
Camera.GetCameraInfo(i, info);
if (info.Facing == CameraInfo.CameraFacingBack)
{
cameraId = i;
cameraInfo = info;
}
}
//Open Camera
mCamera = Camera.Open(cameraId);
if (cameraInfo != null)
{
var orientation = cameraInfo.Orientation;
}