我编写了一个自定义相机活动来处理在调用意图图像捕获时遇到的某些Android设备的问题。用户能够选择保存图像或仅使用从OnPictureTakenCallback
返回的数据。
我的问题是如何正确显示拍摄方向的图像,我通过调用SetRequestedOrientation
强制将活动显示为纵向。
当用户拍照时,我如何知道相机的正确方向?例如,用户可能以90度(纵向)的旋转拍照。
我尝试使用窗口管理器的默认显示getRotation()
,但是设置请求的方向为纵向后,它只返回Surface.ROTATION_0
。
更新:为了澄清我的另一个问题,如果用户不保存图像,我该如何仅从图片回调中的byte[]
数据确定方向?
更新:在尝试下面的答案之后,我得到的仅为ExifInterface.ORIENTATION_NORMAL。此外,我已经更改了代码,只保存从相机返回的文件,因为我不确定是否有一种简单的方法可以仅通过byte[]
数据确定方向。
private PictureCallback mPicture = new PictureCallback()
{
@Override
public void onPictureTaken(byte[] data, Camera camera)
{
File directory = new File(android.os.Environment.getExternalStoragePublicDirectory(android.os.Environment.DIRECTORY_PICTURES),
"MyApp");
if(!directory.exists())
{
if(!directory.mkdirs())
{
Log.d("CAMERA", "Unable to create directory to save photos.");
return;
}
}
File file = new File(directory.getPath() + file.separator + "IMG_" + SimpleDateFormat.getDateTimeInstance().toString() + ".jpg");
FileOutputStream fos = new FileOutputStream(file);
fos.write(data);
fos.close();
ExifInterface exif = new ExifInterface(file.getCanonicalPath());
int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
int rotate = 0;
switch (orientation)
{
case ExifInterface.ORIENTATION_ROTATE_90:
rotate = 90;
break;
case ExifInterface.ORIENTATION_ROTATE_180:
rotate = 180;
break;
case ExifInterface.ORIENTATION_ROTATE_270:
rotate = 270;
break;
case default:
break;
}
}
};