我试图向用户显示来自相机的正面朝上的图像 - 无论他如何握住手机。
我的代码很简单:
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra("return-data", true);
startActivityForResult(cameraIntent, CAMERA_RESULT);
然后我将返回的图像附加到 onActivityResult 中的 ImageView。
我知道如何旋转图像,但我如何知道用户拍照时如何握住相机?
最佳答案
我几乎可以肯定你不能使用“return-data”额外功能来做到这一点。 您必须传递不希望创建图片的位置的文件名,然后使用文件的 Exif 来检查方向。
String FOLDER = "YOUR_FOLDER";
String FILENAME = "YOUR_FILENAME";
int TAKE_PHOTO_REQUEST_CODE = 0;
File photoFile;
void TakePhoto()
{
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
photoFile = new File(FOLDER , FILENAME );
Uri pictureFileUri = Uri.fromFile(photoFile);
takePictureIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, pictureFileUri);
startActivityForResult(takePictureIntent, TAKE_PHOTO_REQUEST_CODE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
switch(requestCode) {
case TAKE_PHOTO_REQUEST_CODE:
if(resultCode == RESULT_OK)
{
int orientation = 0;
try
{
ExifInterface exif = new ExifInterface(path);
int exifOrientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, -1);
switch (exifOrientation)
{
case ExifInterface.ORIENTATION_NORMAL:
orientation = 0;
break;
case ExifInterface.ORIENTATION_ROTATE_90:
orientation = 90;
break;
case ExifInterface.ORIENTATION_ROTATE_180:
orientation = 180;
break;
case ExifInterface.ORIENTATION_ROTATE_270:
orientation = -90;
break;
}
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// Here you can rotate the bitmap knowing the orientation of the camera when the photo was taken
}
}
关于java - 来自 Android 相机的图像,正面朝上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26703356/