Android:旋转并显示文件中的图像 [英] Android: Rotate and display image from file
问题描述
我有一个非常简单的ImageView布局.我的应用程序打开相机,保存图像,然后使用BitmapFactory.decodeFile
在ImageView中显示图像.唯一的问题是它旋转了.我知道a)这是由于手机的摄像头默认设置为横向,所以这需要用代码处理,并且b)图像处理必须在与UI分开的线程中完成.
I have a very simple layout with an ImageView. My application opens the camera, saves an image, and then displays the image in the ImageView with BitmapFactory.decodeFile
. The only issue is that it's rotated. I understand that a) this is due to the phone's camera defaulting to landscape, so this needs to be handled in code and b) image processing must be done in a separate thread from the UI.
Android培训文档似乎旨在从资源ID而不是文件路径加载图像.因为我可以完全按照教程进行操作,但是最终还是难以解决分歧.
The Android training documentation seems to be geared toward loading images from resource id's instead of file paths. This just throws a wrench in things as I'm able to follow the tutorials up to a point but ultimately have trouble reconciling the differences.
我当然是新来的,所以如果有人可以给我一个高水平的概述来完成此任务需要做什么,我将非常感激.以下代码是我当前将位图添加到ImageView的位置.我假设对单独线程的调用将在onCreate
中进行?
I'm certainly new at this so if someone could just give me a high-level overview of what needs to be done to accomplish this, I'd really appreciate it. The following code is where I'm currently adding the bitmap to the ImageView. I assume that my call to the separate thread would go in onCreate
?
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_image);
Bitmap bm = BitmapFactory.decodeFile(MainActivity.image_path);
displayImageView = (ImageView) findViewById(R.id.myImageView);
displayImageView.setImageBitmap(bm);
}
推荐答案
使用 ExifInterface 旋转
picturePath = getIntent().getStringExtra("path");
Bitmap bitmap = BitmapFactory.decodeFile(picturePath);
ExifInterface exif = new ExifInterface(picturePath);
int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED);
Matrix matrix = new Matrix();
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
matrix.postRotate(90);
break;
case ExifInterface.ORIENTATION_ROTATE_180:
matrix.postRotate(180);
break;
case ExifInterface.ORIENTATION_ROTATE_270:
matrix.postRotate(270);
break;
default:
break;
}
myImageView.setImageBitmap(bitmap);
bitmap.recycle();
注意::此处选择picturePath
来自Gallery
这篇关于Android:旋转并显示文件中的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!