2013-03-22 58 views
0

我嘗試調整圖像大小,但調整大小以獲得低分辨率圖像。有沒有其他的解決方案來調整大小和使用Java代碼的ororid。當圖像調整大小,以獲得非常低的分辨率圖像在Android?

 BitmapFactory.Options options = new BitmapFactory.Options(); 
      options.inSampleSize = 4; 
      Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath(), options); 
      int h = 300; 
      int w = 300; 
      Bitmap scaled = Bitmap.createScaledBitmap(myBitmap, h, w, true); 

      String root = Environment.getExternalStorageDirectory() 
        .toString(); 
      File myDir = new File(root + "/upload_images"); 
      myDir.mkdirs(); 
      String fname = null; 
      if (rname == null) { 
       fname = "Image.jpg"; 
      } else { 
       fname = rname + ".jpg"; 
       Log.i("log_tag", "File anem::" + fname); 
      } 
      file = new File(myDir, fname); 
      Log.i("log_tag", "" + file); 
      if (file.exists()) 
       file.delete(); 
      try { 
       FileOutputStream out = new FileOutputStream(file); 
       scaled.compress(Bitmap.CompressFormat.JPEG, 90, out); 
       out.flush(); 
       out.close(); 
+0

有什麼理由不與此版本的工作,你是不是清楚這一點。但是你正在縮小它並壓縮成JPEG(而不是無損PNG),所以你將會受到很大質量的影響。 – 2013-03-22 07:09:18

+0

引用此...猜它會有幫助http://stackoverflow.com/questions/4231817/quality-problems-when-resizing-an-image-at-runtime?rq=1 – 2013-03-22 07:10:46

+0

@ Karan梅爾感謝它的工作良好, – 2013-03-22 07:22:15

回答

0

改變這一行:

scaled.compress(Bitmap.CompressFormat.JPEG, 100, out); 

scaled.compress(Bitmap.CompressFormat.JPEG, 90, out); 

,我會saggest你下面的方法進行圖像壓縮位圖。

//decodes image and scales it to reduce memory consumption 
private Bitmap decodeFile(File f){ 
    try { 
     //Decode image size 
     BitmapFactory.Options o = new BitmapFactory.Options(); 
     o.inJustDecodeBounds = true; 
     BitmapFactory.decodeStream(new FileInputStream(f),null,o); 

     //The new size we want to scale to 
     final int REQUIRED_SIZE=70; 

     //Find the correct scale value. It should be the power of 2. 
     int scale=1; 
     while(o.outWidth/scale/2>=REQUIRED_SIZE && o.outHeight/scale/2>=REQUIRED_SIZE) 
      scale*=2; 

     //Decode with inSampleSize 
     BitmapFactory.Options o2 = new BitmapFactory.Options(); 
     o2.inSampleSize=scale; 
     return BitmapFactory.decodeStream(new FileInputStream(f), null, o2); 
    } catch (FileNotFoundException e) {} 
    return null; 
} 

更多詳情檢查此:https://stackoverflow.com/a/823966/1168654

相關問題