2012-07-20 119 views
0

我從SD卡加載圖片到位圖。文件是jpeg。我這樣做是這樣的:調整位圖而不損失質量

Bitmap b = BitmapFactory.decodeFile("/sdcard/DinEgen/"+name.get(position), null); 
     Bitmap img = Bitmap.createScaledBitmap(b, width, height, true); 

     b.recycle(); 
     i.setImageBitmap(img); 

但是,當我scaledBitmap我失去了質量。當我只使用「位圖b」質量是好的。所以當我使用createScaledBitmap時,我正在失去quelity。如何縮放位圖b的大小從hightwidth變量?

回答

1

我已經爲它寫了一個類。

public class BitmapSizeHelper 
{ 

    public static enum ScalingLogic 
     { 
      CROP, FIT 
     } 

    public static Bitmap getBitmapFromResources(Resources res, int resId, int dstWidth, int dstHeight, ScalingLogic scalingLogic) 
     { 
      Options options = new Options(); 
      options.inJustDecodeBounds = true; 
      BitmapFactory.decodeResource(res, resId, options); 
      options.inJustDecodeBounds = false; 
      options.inSampleSize = calculateSampleSize(options.outWidth, options.outHeight, dstWidth, dstHeight, scalingLogic); 
      Bitmap unscaledBitmap = BitmapFactory.decodeResource(res, resId, options); 

      return unscaledBitmap; 

     } 

    public static Bitmap getBitmapFromPath(int targetW, int targetH, String photopath, int dstWidth, int dstHeight, ScalingLogic scalingLogic) 
     { 

      Options options = new Options(); 
      options.inJustDecodeBounds = true; 
      BitmapFactory.decodeFile(photopath, options); 
      options.inJustDecodeBounds = false; 
      options.inSampleSize = calculateSampleSize(options.outWidth, options.outHeight, dstWidth, dstHeight, scalingLogic); 
      Bitmap bitmap = BitmapFactory.decodeFile(photopath, options); 

      return bitmap; 

     } 

    public static int calculateSampleSize(int srcWidth, int srcHeight, int dstWidth, int dstHeight, ScalingLogic scalingLogic) 
     { 
      if (scalingLogic == ScalingLogic.FIT) 
       { 
        final float srcAspect = (float) srcWidth/(float) srcHeight; 
        final float dstAspect = (float) dstWidth/(float) dstHeight; 

        if (srcAspect > dstAspect) 
         { 
          return srcWidth/dstWidth; 
         } 
        else 
         { 
          return srcHeight/dstHeight; 
         } 
       } 
      else 
       { 
        final float srcAspect = (float) srcWidth/(float) srcHeight; 
        final float dstAspect = (float) dstWidth/(float) dstHeight; 

        if (srcAspect > dstAspect) 
         { 
          return srcHeight/dstHeight; 
         } 
        else 
         { 
          return srcWidth/dstWidth; 
         } 
       } 
     } 

    public static Bitmap createScaledBitmap(Bitmap unscaledBitmap, int dstWidth, int dstHeight, ScalingLogic scalingLogic) 
     { 
      Rect srcRect = calculateSrcRect(unscaledBitmap.getWidth(), unscaledBitmap.getHeight(), dstWidth, dstHeight, scalingLogic); 
      Rect dstRect = calculateDstRect(unscaledBitmap.getWidth(), unscaledBitmap.getHeight(), dstWidth, dstHeight, scalingLogic); 
      Bitmap scaledBitmap = Bitmap.createBitmap(dstRect.width(), dstRect.height(), Config.ARGB_8888); 
      Canvas canvas = new Canvas(scaledBitmap); 
      canvas.drawBitmap(unscaledBitmap, srcRect, dstRect, new Paint(Paint.FILTER_BITMAP_FLAG)); 

      return scaledBitmap; 
     } 

    public static Rect calculateSrcRect(int srcWidth, int srcHeight, int dstWidth, int dstHeight, ScalingLogic scalingLogic) 
     { 
      if (scalingLogic == ScalingLogic.CROP) 
       { 
        final float srcAspect = (float) srcWidth/(float) srcHeight; 
        final float dstAspect = (float) dstWidth/(float) dstHeight; 

        if (srcAspect > dstAspect) 
         { 
          final int srcRectWidth = (int) (srcHeight * dstAspect); 
          final int srcRectLeft = (srcWidth - srcRectWidth)/2; 
          return new Rect(srcRectLeft, 0, srcRectLeft + srcRectWidth, srcHeight); 
         } 
        else 
         { 
          final int srcRectHeight = (int) (srcWidth/dstAspect); 
          final int scrRectTop = (int) (srcHeight - srcRectHeight)/2; 
          return new Rect(0, scrRectTop, srcWidth, scrRectTop + srcRectHeight); 
         } 
       } 
      else 
       { 
        return new Rect(0, 0, srcWidth, srcHeight); 
       } 
     } 

    public static Rect calculateDstRect(int srcWidth, int srcHeight, int dstWidth, int dstHeight, ScalingLogic scalingLogic) 
     { 
      if (scalingLogic == ScalingLogic.FIT) 
       { 
        final float srcAspect = (float) srcWidth/(float) srcHeight; 
        final float dstAspect = (float) dstWidth/(float) dstHeight; 

        if (srcAspect > dstAspect) 
         { 
          return new Rect(0, 0, dstWidth, (int) (dstWidth/srcAspect)); 
         } 
        else 
         { 
          return new Rect(0, 0, (int) (dstHeight * srcAspect), dstHeight); 
         } 
       } 
      else 
       { 
        return new Rect(0, 0, dstWidth, dstHeight); 
       } 
     } 

}` 
+0

你的形象得到扭曲/鬆質量是因爲圖像的長寬比根據源不能維持的原因寬度和高度以及目標寬度和高度。所以你需要決定你想使用裁剪或適合的縮放邏輯。 – Shachillies 2012-07-20 08:30:56

+0

只是謝謝...你救了我的一天...真棒男人非常感謝。 像魅力一樣工作.......... THANKs THANKs – 2013-06-19 14:11:26

0

不能不失品質,除非他們是vector graphics調整圖像。儘管如此,儘可能減少質量損失。

-2

當你解碼JPEG,使用BitmapOptions明確地告訴未按比例等

Options options = new BitmapFactory.Options(); 
options.inScaled = false; 
options.inDither = false; 
options.inPreferredConfig = Bitmap.Config.ARGB_8888; 
Bitmap b = BitmapFactory.decodeFile("/sdcard/DinEgen/"+name.get(position), options);