2013-08-01 3 views
0

카메라에서 찍은 사진을 서버로 보내려고합니다. 그러나 그것은 1M 정도의 크기이고 나는 그것이 두 가지라고 생각합니다. 따라서 나는 사진의 크기를 줄이고 싶다. 아래의 방법을 사용하고 있습니다.
saveFile - 단순히 파일에 비트 맵을 저장하고 경로를 반환하십시오.
calculateInSampleSize은 사진 디멘션을 조정하도록 inSampleSize을 반환합니다.
decodeSampledBitmapFromResource은 파일에서 birmap을 디코딩하고 새로운 매개 변수로 다시 작성합니다.
Evrything은 좋지만 작동하지 않는 것 같습니다. small3.png의 Dimentions은 1240 * 768 * 24이며 크기는 여전히 1M입니다.사진 크기를 줄이는 방법?

나는

photo = saveFile(decodeSampledBitmapFromResource(picturePath, 
        800, 800),3); 

방법

public String saveFile(Bitmap bm,int id) { 
     String file_path = Environment.getExternalStorageDirectory() 
       .getAbsolutePath() + "/Mk"; 
     File dir = new File(file_path); 
     if (!dir.exists()) 
      dir.mkdirs(); 
     File file = new File(dir, "smaller" + id + ".png"); 
     FileOutputStream fOut; 
     try { 
      fOut = new FileOutputStream(file); 
      bm.compress(Bitmap.CompressFormat.PNG, 85, fOut); 
      fOut.flush(); 
      fOut.close(); 


      } 
     } catch (FileNotFoundException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 


     return file.getPath(); 

    } 

    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; 
    } 
public static Bitmap decodeSampledBitmapFromResource(String path, 
      int reqWidth, int reqHeight) { 
     Log.d("path", path); 
     // First decode with inJustDecodeBounds=true to check dimensions 
     final BitmapFactory.Options options = new BitmapFactory.Options(); 
     options.inJustDecodeBounds = true; 
     BitmapFactory.decodeFile(path, options); 

     // Calculate inSampleSize 
     options.inSampleSize = calculateInSampleSize(options, reqWidth, 
       reqHeight); 

     // Decode bitmap with inSampleSize set 
     options.inJustDecodeBounds = false; 
     return BitmapFactory.decodeFile(path, options); 
    } 

답변

1

왜 모두 한 번에 두 번 그냥 그것을 할 이미지를 작성하여 호출

내가 무엇을

BitmapFactory.Options options = new BitmapFactory.Options(); 
options.inSampleSize = 4; 

Bitmap myBitmap = BitmapFactory.decodeFile({ImagePath}, options); 
FileOutputStream fileOutputStream = new FileOutputStream({ImagePath}); 
BufferedOutputStream bos = new BufferedOutputStream(fileOutputStream); 
myBitmapClose.compress(CompressFormat.JPEG, imageQuality, bos); 
if (bos != null) { 
    bos.flush(); 
    bos.close(); 
} 
if (fileOutputStream != null) { 
    fileOutputStream.flush(); 
    fileOutputStream.close(); 
} 

efinitely 마음을 설정 imageCompression 유형 CompressFormat.JPEG이

+0

원래 사진을 유지할 생각이 없습니다. – Yarh

+0

괜찮습니까? 샘플 크기와 품질을 처리 할 수 ​​있습니다. –

0

내 코드 그게 전부

myBitmapClose.compress(CompressFormat.JPEG, imageQuality, bos); 

... 난 경로 문자열이고 이미지의 크기를 줄이려 라인을 압축합니다.

String pics=(arraylist.get(position).getImage()); 
try{ 
    BitmapFactory.Options op = new BitmapFactory.Options(); 
    op.inJustDecodeBounds = true; 
    //op.inSampleSize = 20; op.outWidth = 25; 
    op.outHeight = 35; 
    bmp = BitmapFactory.decodeFile(pics); 
    image.setImageBitmap(bmp); 
}catch(Exception e) { 
} 
관련 문제