在 android 拍完照片后,我们如何以垂直模式显示图像?
How can we show image in vertical mode after picture taken in android?
Java代码
private void previewCapturedImage()
{
try
{
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 1;
ExifInterface exif = null;
try
{
exif = new ExifInterface(fileUri.getPath());
}
catch (IOException e)
{
e.printStackTrace();
}
String exifOrientation = exif.getAttribute(ExifInterface.TAG_ORIENTATION);
Log.i("file path",exifOrientation);
final Bitmap bitmap = BitmapFactory.decodeFile(fileUri.getPath());
previewimage.setImageBitmap(bitmap);
}
catch (NullPointerException e) {
e.printStackTrace();
}
}
XML
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/imageview"
android:adjustViewBounds="true"
android:scaleType="fitCenter"/>
此处有些phone 显示的是纵向图像,有些phone 显示的是拍摄后的横向图像。如何在所有类型的 phone 中以纵向显示图像。
private void previewCapturedImage() {
try {
// bimatp factory
BitmapFactory.Options options = new BitmapFactory.Options();
// downsizing image as it throws OutOfMemory Exception for larger
// images
options.inSampleSize = 1;
ExifInterface exif = null;
int orientation = 0;//Since API Level 5
try {
exif = new ExifInterface(fileUri.getPath());
orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
} catch (IOException e) {
e.printStackTrace();
}
String exifOrientation = exif.getAttribute(ExifInterface.TAG_ORIENTATION);
Log.i("file path",exifOrientation);
final Bitmap bitmap = BitmapFactory.decodeFile(fileUri.getPath());
previewimage.setImageBitmap(bitmap);
switch(orientation) {
case ExifInterface.ORIENTATION_ROTATE_270:
Log.i("RotateBitmap","270");
RotateBitmap(bitmap, 270);
previewimage.setRotation(270);
break;
case ExifInterface.ORIENTATION_ROTATE_90:
Log.i("RotateBitmap","90");
RotateBitmap(bitmap, 270);
previewimage.setRotation(90);
break;
case ExifInterface.ORIENTATION_ROTATE_180:
Log.i("RotateBitmap","180");
RotateBitmap(bitmap, 180);
previewimage.setRotation(180);
break;
// etc.
}
} catch (NullPointerException e) {
e.printStackTrace();
}
}
public static Bitmap RotateBitmap(Bitmap source, float angle)
{
Matrix matrix = new Matrix();
matrix.postRotate(angle);
previewimage.setImageBitmap(source);
return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true);
//previewimage.setImageBitmap(source);
}
矩阵用于在Android
中旋转位图
只需使用
Matrix matrix = new Matrix();
matrix.postRotate(angle_in_degrees);
并将 matrix
传递给 bitmap.createBitmap 构造函数。
Java代码
private void previewCapturedImage()
{
try
{
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 1;
ExifInterface exif = null;
try
{
exif = new ExifInterface(fileUri.getPath());
}
catch (IOException e)
{
e.printStackTrace();
}
String exifOrientation = exif.getAttribute(ExifInterface.TAG_ORIENTATION);
Log.i("file path",exifOrientation);
final Bitmap bitmap = BitmapFactory.decodeFile(fileUri.getPath());
previewimage.setImageBitmap(bitmap);
}
catch (NullPointerException e) {
e.printStackTrace();
}
}
XML
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/imageview"
android:adjustViewBounds="true"
android:scaleType="fitCenter"/>
此处有些phone 显示的是纵向图像,有些phone 显示的是拍摄后的横向图像。如何在所有类型的 phone 中以纵向显示图像。
private void previewCapturedImage() {
try {
// bimatp factory
BitmapFactory.Options options = new BitmapFactory.Options();
// downsizing image as it throws OutOfMemory Exception for larger
// images
options.inSampleSize = 1;
ExifInterface exif = null;
int orientation = 0;//Since API Level 5
try {
exif = new ExifInterface(fileUri.getPath());
orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
} catch (IOException e) {
e.printStackTrace();
}
String exifOrientation = exif.getAttribute(ExifInterface.TAG_ORIENTATION);
Log.i("file path",exifOrientation);
final Bitmap bitmap = BitmapFactory.decodeFile(fileUri.getPath());
previewimage.setImageBitmap(bitmap);
switch(orientation) {
case ExifInterface.ORIENTATION_ROTATE_270:
Log.i("RotateBitmap","270");
RotateBitmap(bitmap, 270);
previewimage.setRotation(270);
break;
case ExifInterface.ORIENTATION_ROTATE_90:
Log.i("RotateBitmap","90");
RotateBitmap(bitmap, 270);
previewimage.setRotation(90);
break;
case ExifInterface.ORIENTATION_ROTATE_180:
Log.i("RotateBitmap","180");
RotateBitmap(bitmap, 180);
previewimage.setRotation(180);
break;
// etc.
}
} catch (NullPointerException e) {
e.printStackTrace();
}
}
public static Bitmap RotateBitmap(Bitmap source, float angle)
{
Matrix matrix = new Matrix();
matrix.postRotate(angle);
previewimage.setImageBitmap(source);
return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true);
//previewimage.setImageBitmap(source);
}
矩阵用于在Android
中旋转位图只需使用
Matrix matrix = new Matrix();
matrix.postRotate(angle_in_degrees);
并将 matrix
传递给 bitmap.createBitmap 构造函数。