【发布时间】:2016-01-31 01:07:37
【问题描述】:
我试图简单地从手机的相机中获取图像。令人惊讶的是,它返回旋转。我在互联网上搜索修复并使用 ExifInterface 遇到了许多解决方案,但它有时只能工作。它看似随机地错误地定位,因为我只是重新编译并看到不同的结果。我发现有些人说这是类本身被窃听的错误。
我发现其他解决方案需要两个额外的库和更多的 java 文件来完成这项工作,但这看起来很荒谬(我正在避免额外的包)。图像最初是如何旋转的(在存储中它们非常好),解决这个问题有多难?另外 - 旋转图像视图也可以(并且似乎比字面上创建旋转图像要容易得多),但我需要知道 多少 来旋转视图。
EDIT---- 我意识到如果使用后置摄像头,图像会从图像拍摄的方向(在意图内)顺时针旋转 270 度,如果使用前置摄像头,则旋转 90 度。因此,我真的只需要一种方法来找出这个方向。
此处调用的意图:
private void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
// Create the File where the photo should go
File photoFile = null;
try {
photoFile = setUpPhotoFile();
mCurrentPhotoPath = photoFile.getAbsolutePath();
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
} catch (IOException ex) {
// Error occurred while creating the File
photoFile = null;
mCurrentPhotoPath = null;
}
// Continue only if the File was successfully created
if (photoFile != null) {
startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
} else {
Toast noStorage = Toast.makeText(this, "Cannot access mounted storage.", Toast.LENGTH_SHORT);
noStorage.show();
}
}
}
在这里创建的位图:
private void setPic() {
/* Get the size of the ImageView */
int targetW = mImageView.getWidth();
int targetH = mImageView.getHeight();
/* Get the size of the image */
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
bmOptions.inJustDecodeBounds = true;
BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);
int photoW = bmOptions.outWidth;
int photoH = bmOptions.outHeight;
/* Figure out which way needs to be reduced less */
int scale = 1;
if (photoH > targetH|| photoW > targetW) {
scale = Math.max(
(int)Math.pow(2, (int) Math.ceil(Math.log(targetW /
(double) photoW)) / Math.log(0.5)),
(int)Math.pow(2, (int) Math.ceil(Math.log(targetH /
(double) photoH)) / Math.log(0.5)));
;
}
/* Set bitmap options to scale the image decode target */
bmOptions.inJustDecodeBounds = false;
bmOptions.inSampleSize = scale;
/* Decode the JPEG file into a Bitmap */
Bitmap bitmap = BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);
/*-----------How should I rotate bitmap/mImageView to correct orientation?*/
/* Associate the Bitmap to the ImageView */
mImageView.setImageBitmap(bitmap);
mImageView.setVisibility(View.VISIBLE);
}
【问题讨论】: