2017-12-25 15 views
-1

写真を撮る、またはギャラリーから画像を選択するアプリケーションを構築する必要があり、50kbのサイズでサーバーに送信する必要があります。ビットマップを50kbに圧縮する

これまでは、ビットマップの幅と高さのサイズを変更できるコードしか見つかりませんでしたが、そのおかげでビットマップの品質が低下しました。

public Bitmap getResizedBitmap(Bitmap image, int maxSize) { 
    int width = image.getWidth(); 
    int height = image.getHeight(); 

    float bitmapRatio = (float)width/(float) height; 
    if (bitmapRatio > 1) { 
     width = maxSize; 
     height = (int) (width/bitmapRatio); 
    } else { 
     height = maxSize; 
     width = (int) (height * bitmapRatio); 
    } 
    return Bitmap.createScaledBitmap(image, width, height, true); 
} 

ありがとうございます!

+0

あなたは、この[リンク](https://stackoverflow.com/questions/28424942/decrease-image-size-without-losing-its-quality-in-android)をしてみてくださいでした – Developer

答えて

0
ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
    image.compress(Bitmap.CompressFormat.JPEG, 100, baos); 
    int options = 100; 
    while (baos.toByteArray().length > 1024*50) { 
     baos.reset(); 
     image.compress(Bitmap.CompressFormat.JPEG, options, baos); 
     options -= 10; 
    } 
    ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray()); 
    Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, null); 
    return bitmap; 
関連する問題