0

I'm scaling down some bitmaps and getting low quality results.The result image looks different than original image.. I have used following code to scale down image,

Options options = new BitmapFactory.Options();
options.inScaled = false;
Bitmap ib=BitmapFactory.decodeResource(getResources(), R.drawable.card,options);

Bitmap mm=Bitmap.createScaledBitmap(ib,ib.getWidth()/5,ib.getHeight()/5, true);

AND

 BitmapFactory.Options m_bmpFactoryOptions = new BitmapFactory.Options();

 m_bmpFactoryOptions.inJustDecodeBounds = true;

 Bitmap m_bitmap = BitmapFactory.decodeFile(p_file, m_bmpFactoryOptions);

 int m_heightRatio =

 (int) Math.ceil(m_bmpFactoryOptions.outHeight / (float) p_height);

 int m_widthRatio =

 (int) Math.ceil(m_bmpFactoryOptions.outWidth / (float) p_width);
 if (m_heightRatio > 1 || m_widthRatio > 1)
 {
     if (m_heightRatio > m_widthRatio)
     {
        m_bmpFactoryOptions.inSampleSize = m_heightRatio;
     }
     else
     {
        m_bmpFactoryOptions.inSampleSize = m_widthRatio;
     }
     }       
     m_bmpFactoryOptions.inJustDecodeBounds = false;


     m_bitmap = BitmapFactory.decodeFile(p_file, m_bmpFactoryOptions);

*Please help If there are any filters like lanczos,catrom to improve image quality after scale down *

Sagar Badave
  • 121
  • 1
  • 3
  • 9
  • I think you should use specific number instead of getWidth()/5 or getHeight()/5. Because if your image is already small then it reduces the quality of image. – Biswajit Dec 26 '14 at 07:25
  • you shouldnt scale bitmap = width/ 5 and height /5 , example you have 500x500 image , you will scale to 100x100 image that is bad . Better is use specific number like 500, 500 so image alway scale to 500x500 – squalle0nhart Dec 26 '14 at 07:32
  • @Biswajit: orgBitmap.getWidth()/5 ororgBitmap.getHeight()/5 this operation i have done on original bitmap..to get Scaled bitmap – Sagar Badave Dec 26 '14 at 08:34
  • There are any resapling filter to get good quality image even after down scale – Sagar Badave Dec 26 '14 at 08:46
  • Did you try this? - http://stackoverflow.com/questions/4837715/how-to-resize-a-bitmap-in-android – Wonil Dec 26 '14 at 09:16
  • @Wonil: i have tried that earlier .. gets bad quality image after down scale...... – Sagar Badave Dec 26 '14 at 09:22

2 Answers2

0

Use this method

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);
}


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) {

// Calculate ratios of height and width to requested height and width
final int heightRatio = Math.round((float) height / (float) reqHeight);
    final int widthRatio = Math.round((float) width / (float) reqWidth);

// Choose the smallest ratio as inSampleSize value, this will guarantee
// a final image with both dimensions larger than or equal to the
// requested height and width.
inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
 }

 return inSampleSize;
}
T_V
  • 17,440
  • 6
  • 36
  • 48
0

Replace options.inScaled = false; with

options.inSampleSize =2
Sanjay Hirani
  • 406
  • 2
  • 6
  • 18
  • Check http://developer.android.com/reference/android/graphics/BitmapFactory.Options.html#inSampleSize This method does scaling and you cannot get the same quality with **sub sampled** image. – Sanjay Hirani Dec 26 '14 at 11:25