2012-06-19 5 views
0

this 링크에 따라 이미지 크기를 조정합니다.
이미지 크기가 3264x2448이고 샘플 크기를 계산 한 후 이제 24이지만 이미지가 90도 (왼쪽)로 회전합니다.
폭이 높이보다 큰 경우가 발생합니다.
나는 갇혔다. 해결할 수 없습니다. 이미지 크기 조정 문제

I am using accroding to http://developer.android.com/training/displaying-bitmaps/load-bitmap.html 


public static String getCompressedImagePath(String orgImagePath, 
     String storeImagePath) { 
    if (orgImagePath == null) { 
     return null; 
    } 
    Bitmap bitmap = decodeSampledBitmapFromResource(orgImagePath, 100, 100); 
    String absolutePath = ""; 
    FileOutputStream fos = null; 
    try { 

     fos = new FileOutputStream(storeImagePath); 
     bitmap.compress(getCompressionFormatType(orgImagePath), 
       IMAGE_COMPRESS_FACTOR, fos); 
     fos.flush(); 
     absolutePath = storeImagePath; 
    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } catch (NullPointerException e) { 
     e.printStackTrace(); 
    } finally { 
     try { 
      if (fos != null) { 
       fos.close(); 
       fos = null; 
      } 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 
    return absolutePath; 
} 

public static Bitmap decodeSampledBitmapFromResource(String orgImagePath, 
     int reqWidth, int reqHeight) { 
    final BitmapFactory.Options options = new BitmapFactory.Options(); 
    options.inJustDecodeBounds = true; 
    BitmapFactory.decodeFile(orgImagePath, options); 
    options.inSampleSize = calculateInSampleSize(options, reqWidth, 
      reqHeight); 
    options.inJustDecodeBounds = false; 
    return BitmapFactory.decodeFile(orgImagePath, options); 
} 

public static int calculateInSampleSize(BitmapFactory.Options options, 
     int reqWidth, int reqHeight) { 
    final int height = options.outHeight; 
    final int width = options.outWidth; 


    int inSampleSize = 1; 
    if (height > reqHeight || width > reqWidth) { 
     if (width > height) { 
      inSampleSize = Math.round((float) height/(float) reqHeight); 
     } else { 
      inSampleSize = Math.round((float) width/(float) reqWidth); 
     } 
    } 
    return inSampleSize; 
} 

그리고 decodeSampledBitmapFromResource 방법

링크에서 복사 intoimageview으로 압축 된 이미지를 설정하고있다.

+0

코드를 게시하시기 바랍니다 누락 밖의 무엇을 찾아이 응용 프로그램 &을 실행 당신도 사용하고 있습니다. – noob

+0

그리고 일부 샘플 이미지. –

답변

1

글쎄, 내 샘플 애플리케이션 &에 사용 된 & 코드를 사용하면 정상적으로 작동합니다. 그래서 당신은 당신이

활동-ImageScaleTestActivity

XML-main.xml에

 

    <?xml version="1.0" encoding="utf-8"?> 
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:gravity="center" 
     android:orientation="vertical" > 

     <ImageView 
      android:id="@+id/imageView1" 
      android:layout_width="fill_parent" 
      android:layout_height="0dip" 
      android:src="@drawable/ic_launcher" android:layout_weight="1"/> 

     <Button 
      android:id="@+id/button1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Show converted Image" /> 

    </LinearLayout> 

IMAGEUTILS

 

    package com.myTutororial.utils; 

    import java.io.FileNotFoundException; 
    import java.io.FileOutputStream; 
    import java.io.IOException; 

    import android.graphics.Bitmap; 
    import android.graphics.Bitmap.CompressFormat; 
    import android.graphics.BitmapFactory; 

    public class ImageUtils { 
     public static String getCompressedImagePath(String orgImagePath, 
       String storeImagePath) { 
      if (orgImagePath == null) { 
       return null; 
      } 
      Bitmap bitmap = decodeSampledBitmapFromResource(orgImagePath, 100, 100); 
      String absolutePath = ""; 
      FileOutputStream fos = null; 
      try { 

       fos = new FileOutputStream(storeImagePath); 
       bitmap.compress(CompressFormat.PNG, 90, fos); 
       fos.flush(); 
       absolutePath = storeImagePath; 
      } catch (FileNotFoundException e) { 
       e.printStackTrace(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } catch (NullPointerException e) { 
       e.printStackTrace(); 
      } finally { 
       try { 
        if (fos != null) { 
         fos.close(); 
         fos = null; 
        } 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
      } 
      return absolutePath; 
     } 

     public static Bitmap decodeSampledBitmapFromResource(String orgImagePath, 
       int reqWidth, int reqHeight) { 
      final BitmapFactory.Options options = new BitmapFactory.Options(); 
      options.inJustDecodeBounds = true; 
      BitmapFactory.decodeFile(orgImagePath, options); 
      options.inSampleSize = calculateInSampleSize(options, reqWidth, 
        reqHeight); 
      options.inJustDecodeBounds = false; 
      return BitmapFactory.decodeFile(orgImagePath, options); 
     } 

     public static int calculateInSampleSize(BitmapFactory.Options options, 
       int reqWidth, int reqHeight) { 
      final int height = options.outHeight; 
      final int width = options.outWidth; 

      int inSampleSize = 1; 
      if (height > reqHeight || width > reqWidth) { 
       if (width > height) { 
        inSampleSize = Math.round((float) height/(float) reqHeight); 
       } else { 
        inSampleSize = Math.round((float) width/(float) reqWidth); 
       } 
      } 
      return inSampleSize; 
     } 

     }