【发布时间】:2012-07-25 08:46:09
【问题描述】:
我编写了一个自定义相机活动来处理我在调用 Intent 图像捕获时遇到的某些问题。用户可以选择保存图像或仅使用从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;
}
}
};
【问题讨论】:
标签: android camera orientation