java #android
#java #Android
Вопрос:
У меня проблема с желанием уменьшить определение изображения. Проблема в том, что когда я уменьшаю изображение, оно поворачивается и сохраняется.
Я пытаюсь использовать exif, чтобы снова повернуть его, чтобы он хорошо сохранялся, но у меня не было хорошего результата.
Кто-нибудь может показать мне, как я могу это сделать. Спасибо.
public File saveBitmapToFile(Direccion direccion, File file) {
try {
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
o.inSampleSize = 5;
FileInputStream inputStream = new FileInputStream(file);
//Bitmap selectedBitmap = null;
BitmapFactory.decodeStream(inputStream, null, o);
inputStream.close();
// The new size we want to scale to
final int REQUIRED_SIZE = 30;
// Find the correct scale value. It should be the power of 2.
int scale = 1;
while (o.outWidth / scale / 2 >= REQUIRED_SIZE amp;amp;
o.outHeight / scale / 2 >= REQUIRED_SIZE) {
scale *= 2;
}
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
inputStream = new FileInputStream(file);
Bitmap selectedBitmap = BitmapFactory.decodeStream(inputStream, null, o2);
inputStream.close();
Uri uri = getLastPhotoUri(direccion);
try {
ExifInterface exif = new ExifInterface(Objects.requireNonNull(uri.getPath()));
int rotation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
int rotationInDegrees = exifToDegrees(rotation);
Matrix matrix = new Matrix(); if (rotation != 0f) {matrix.preRotate(rotationInDegrees);}
Bitmap k = getBitmapFromUri(uri);
try {
//boolean hasImageFlag = false;
if (rotation != 0) {
Bitmap.createBitmap(k, 0, 0, k.getWidth(), k.getHeight(), matrix, true);
}else{
Bitmap.createBitmap(k, 0, 0, k.getWidth(), k.getHeight());
}
// hasImageFlag = true;
} catch (Exception e) {
}
} catch (Exception e) {
}
file.createNewFile();
FileOutputStream outputStream = new FileOutputStream(file);
selectedBitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
return file;
} catch (Exception e) {
return null;
}
}
private int exifToDegrees(int exifOrientation) {
if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_90) {
return 90;
} else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_180) {
return 180;
} else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_270)
{ return 270; } return 0;
}