缩小位图后图像质量问题

时间:2014-12-26 07:13:41

标签: android bitmap imagefilter

我正在缩小一些位图并获得低质量的结果。结果图像看起来与原始图像不同。我使用以下代码缩小图像,

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

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

*请帮助如果有任何过滤器,如lanczos,catrom可以在缩小尺寸后提高图像质量*

2 个答案:

答案 0 :(得分:0)

使用此方法

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

答案 1 :(得分:0)

options.inScaled = false;替换为

options.inSampleSize =2