Ich möchte nur jedes Foto mit der Kamera Größe ändern, wenn das Foto zu groß ist (z. B. nur Breite> 2048px).Größe ändern Bitmap nur wenn zu groß
Ich habe auf der offiziellen doc
gesehenpublic static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
int reqWidth, int reqHeight) {
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(res, resId, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeResource(res, resId, options);
}
und
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;
}
aber dieser Code beinhalten, dass ich die endgültige Größe kennen. Was ich tun möchte, ist um 50% zu reduzieren, wenn es wirklich zu groß ist, oder nur 20%, wenn die Bitmap nicht zu groß ist, usw. (abhängig von der Kamera des Smartphones ...)
Die Methode calculateInSampleSize() scheint zu tun, was ich will und das Dokument sagt "Zum Beispiel, ein Bild mit der Auflösung 2048x1536, das mit einer inSampleSize von 4 dekodiert wird, erzeugt eine Bitmap von etwa 512x384", aber ich möchte keine endgültige Breite/Höhe einstellen.
Dann, wenn ich die kleinere Bitmap erhalten, möchte ich eine
scaledBitmap.compress(CompressFormat.JPEG, 80, out);
um wieder zu optimieren.
Wie kann ich das bitte lösen?
Das ist nicht genau das, was ich will, weil ich keinen Test für jede Dimension haben will ... aber es funktioniert wahrscheinlich ... – psv