2016-04-25 18 views
0

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

gesehen
public 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?

Antwort

0

Schließlich entschied ich mich für eine andere Lösung:

public static void resizeBitmapOnFileSystem(String originalPath, File photoCompressedFile) throws IOException { 
    final BitmapFactory.Options options = new BitmapFactory.Options(); 
    options.inJustDecodeBounds = true; 
    BitmapFactory.decodeFile(originalPath, options); 

    int minimalLengthSide = 300; //Minimal length of the shorter side of the picture. Used to calculate the best 'scale' value. 
    int scale = calculateInSampleSize(options, minimalLengthSide, minimalLengthSide); 

    ByteArrayOutputStream out = null; 
    FileOutputStream fo = null; 
    Bitmap scaledBitmap = null; 
    try { 
     out = new ByteArrayOutputStream(); 
     fo = new FileOutputStream(photoCompressedFile); 

     scaledBitmap = loadScaledBitmap(originalPath, scale); 
     if (scaledBitmap != null) { 
      scaledBitmap.compress(CompressFormat.JPEG, 75, out); 
      fo.write(out.toByteArray()); 
     } 
    } finally { 
     IOUtils.closeQuietly(out); 
     IOUtils.closeQuietly(fo); 
     recycleQuietly(scaledBitmap); 
    } 
} 

/** 
* From offical Android documentation 
* @param options 
* @param reqWidth 
* @param reqHeight 
* @return 
*/ 
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; 
} 

Auch wenn die resizeBitmapOnFileSystem() Methode nicht sehr "handlich" ist, ist der Trick, die minimalLengthSide Variable. Ich stelle die minimale Länge der kürzeren Seite ein, die ich haben möchte (hier 300px), und die Methode calculateInSampleSize() berechnet automatisch die geeignete Skala.

Verhältnis Breite/Höhe wird beibehalten und ich verwende nicht Bitmap.createScaledBitmap(realImage, width, height, filter) , die "teuer" oder weniger effizient sein kann.

0

Sie können Breite und Höhe der Bitmap als bitmap.getWidth() und bitmap.getHeight() erhalten. Sobald Sie Breite und Höhe haben, prüfen Sie, welcher Wert kleiner ist. Wenn die Breite kleiner als die Höhe ist, speichern Sie width in einer Variablen namens value, andernfalls speichern Sie height.

Und dann können Sie die Bitmap komprimieren, indem Sie den Wert wie folgt überprüfen.

Bitmap bitmap = your bitmap; 

int width = bitmap.getWidth(); 
int height = bitmap.getHeight(); 

int value; 
if (width < height) { 
    value = width; 
} else { 
    value = height; 
} 

Bitmap scaledBitmap; 

if (value > 5000) { 
    scaledBitmap = bitmap.compress(CompressFormat.JPEG, 80, out); 
} else if (value > 4000) { 
    scaledBitmap = bitmap.compress(CompressFormat.JPEG, 60, out); 
} else if (value > 3000) { 
    scaledBitmap = bitmap.compress(CompressFormat.JPEG, 40, out); 
} else if (value > 2048) { 
    scaledBitmap = bitmap.compress(CompressFormat.JPEG, 20, out); 
} 
+0

Das ist nicht genau das, was ich will, weil ich keinen Test für jede Dimension haben will ... aber es funktioniert wahrscheinlich ... – psv

Verwandte Themen