Android 图片旋转
Android image rotation
我选择了相机或图库来拍摄要上传的图像。
我已经使用下面的代码解码图像
public static Bitmap decodeSampledBitmapFromFile(String path, int reqWidth, int reqHeight) {
path = new File(path).getAbsolutePath();
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(path, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeFile(path, options);
}
public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
final int halfHeight = height / 2;
final int halfWidth = width / 2;
// Calculate the largest inSampleSize value that is a power of 2 and keeps both
// height and width larger than the requested height and width.
while ((halfHeight / inSampleSize) >= reqHeight
&& (halfWidth / inSampleSize) >= reqWidth) {
inSampleSize *= 2;
}
}
return inSampleSize;
}
}
现在,如果这张图片是水平视图,那么我应该将其更改为垂直视图。因为收据上传。
在我的画廊图片或拍摄的图片宽度 > 高度我想将图片旋转到垂直视图
有什么建议吗??
您可以将其设置为 ImageView
并将视图旋转 90 度到您想要的任何方向。
你需要如下使用矩阵,它对我有用
Matrix matrix = getRotation(context, Uri.fromFile(f));
b = Bitmap.createBitmap(b, 0, 0, b.getWidth(), b.getHeight(), matrix, true);
这里是 getRotation() 方法
public static Matrix getRotation(Context context, Uri selectedImage) {
Matrix matrix = new Matrix();
ExifInterface exif;
try {
exif = new ExifInterface(selectedImage.getPath());
int orientation = exif.getAttributeInt(
ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_NORMAL);
Utility.log("orientation", orientation + "");
if (orientation == 6) {
matrix.postRotate(90);
} else if (orientation == 3) {
matrix.postRotate(180);
matrix.postScale((float) b.getWidth(), (float) b.getHeight());
} else if (orientation == 8) {
matrix.postRotate(270);
} else {
}
} catch (IOException e) {
e.printStackTrace();
}
return matrix;
}
这里的 b 是你的位图。
这是我使用的代码,对我有用,
public Bitmap decodeFile(String filePath) {
int imageOrientation = 10;
ExifInterface ei;
try {
ei = new ExifInterface(filePath);
imageOrientation = ei.getAttributeInt(
ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_NORMAL);
} catch (IOException e) {
e.printStackTrace();
}
// Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeFile(filePath, o);
// The new size we want to scale to
final int REQUIRED_SIZE = 1024;
// Find the correct scale value. It should be the power of 2.
int width_tmp = o.outWidth, height_tmp = o.outHeight;
int scale = 1;
while (true) {
if (width_tmp < REQUIRED_SIZE && height_tmp < REQUIRED_SIZE)
break;
width_tmp /= 2;
height_tmp /= 2;
scale *= 2;
}
// Decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
bitMap = BitmapFactory.decodeFile(filePath, o2);
switch (imageOrientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
rotateImage(bitMap, 90);
break;
case ExifInterface.ORIENTATION_ROTATE_180:
rotateImage(bitMap, 180);
break;
case ExifInterface.ORIENTATION_ROTATE_270:
rotateImage(bitMap, 270);
break;
default:
break;
}
return bitMap;
}
这是旋转方法,
public void rotateImage(Bitmap img, int degree) {
Matrix matrix = new Matrix();
matrix.postRotate(degree);
bitMap = Bitmap.createBitmap(img, 0, 0, img.getWidth(),
img.getHeight(), matrix, true);
}
我选择了相机或图库来拍摄要上传的图像。 我已经使用下面的代码解码图像
public static Bitmap decodeSampledBitmapFromFile(String path, int reqWidth, int reqHeight) {
path = new File(path).getAbsolutePath();
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(path, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeFile(path, options);
}
public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
final int halfHeight = height / 2;
final int halfWidth = width / 2;
// Calculate the largest inSampleSize value that is a power of 2 and keeps both
// height and width larger than the requested height and width.
while ((halfHeight / inSampleSize) >= reqHeight
&& (halfWidth / inSampleSize) >= reqWidth) {
inSampleSize *= 2;
}
}
return inSampleSize;
}
}
现在,如果这张图片是水平视图,那么我应该将其更改为垂直视图。因为收据上传。
在我的画廊图片或拍摄的图片宽度 > 高度我想将图片旋转到垂直视图 有什么建议吗??
您可以将其设置为 ImageView
并将视图旋转 90 度到您想要的任何方向。
你需要如下使用矩阵,它对我有用
Matrix matrix = getRotation(context, Uri.fromFile(f));
b = Bitmap.createBitmap(b, 0, 0, b.getWidth(), b.getHeight(), matrix, true);
这里是 getRotation() 方法
public static Matrix getRotation(Context context, Uri selectedImage) {
Matrix matrix = new Matrix();
ExifInterface exif;
try {
exif = new ExifInterface(selectedImage.getPath());
int orientation = exif.getAttributeInt(
ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_NORMAL);
Utility.log("orientation", orientation + "");
if (orientation == 6) {
matrix.postRotate(90);
} else if (orientation == 3) {
matrix.postRotate(180);
matrix.postScale((float) b.getWidth(), (float) b.getHeight());
} else if (orientation == 8) {
matrix.postRotate(270);
} else {
}
} catch (IOException e) {
e.printStackTrace();
}
return matrix;
}
这里的 b 是你的位图。
这是我使用的代码,对我有用,
public Bitmap decodeFile(String filePath) {
int imageOrientation = 10;
ExifInterface ei;
try {
ei = new ExifInterface(filePath);
imageOrientation = ei.getAttributeInt(
ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_NORMAL);
} catch (IOException e) {
e.printStackTrace();
}
// Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeFile(filePath, o);
// The new size we want to scale to
final int REQUIRED_SIZE = 1024;
// Find the correct scale value. It should be the power of 2.
int width_tmp = o.outWidth, height_tmp = o.outHeight;
int scale = 1;
while (true) {
if (width_tmp < REQUIRED_SIZE && height_tmp < REQUIRED_SIZE)
break;
width_tmp /= 2;
height_tmp /= 2;
scale *= 2;
}
// Decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
bitMap = BitmapFactory.decodeFile(filePath, o2);
switch (imageOrientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
rotateImage(bitMap, 90);
break;
case ExifInterface.ORIENTATION_ROTATE_180:
rotateImage(bitMap, 180);
break;
case ExifInterface.ORIENTATION_ROTATE_270:
rotateImage(bitMap, 270);
break;
default:
break;
}
return bitMap;
}
这是旋转方法,
public void rotateImage(Bitmap img, int degree) {
Matrix matrix = new Matrix();
matrix.postRotate(degree);
bitMap = Bitmap.createBitmap(img, 0, 0, img.getWidth(),
img.getHeight(), matrix, true);
}