我选择了相机或图库来拍摄上传照片。 我使用以下代码解码图像
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;
}
}
现在,如果此图像采用水平视图,那么我应该将其更改为垂直。因为收据上传。
在我的图库图片或拍摄的图片宽度>高度我想将图像旋转到垂直视图 对此有何建议?
答案 0 :(得分:1)
这是我用它的代码,
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);
}
答案 1 :(得分:0)
您只需在ImageView
中进行设置,然后将视图旋转90度即可达到您想要的任何方向。
答案 2 :(得分:0)
你需要使用矩阵,如下所示,它适用于我
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是你的位图。