2011-12-07 148 views
9

我正在上傳圖像到服務器,在此之前,我想調整圖像尺寸。我得到一個URI這樣的形象:調整圖像文件的大小

Constants.currImageURI = data.getData(); 

這是調用上傳的圖片:

String response = uploadUserPhoto(new File(getRealPathFromURI(Constants.currImageURI))); 

    public String uploadUserPhoto(File image) { 

    DefaultHttpClient mHttpClient; 
    HttpParams params = new BasicHttpParams(); 
    params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1); 
    mHttpClient = new DefaultHttpClient(params); 

    try { 
     HttpPost httppost = new HttpPost("http://myurl/mobile/image"); 

     MultipartEntity multipartEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); 
     multipartEntity.addPart("userID", new StringBody(Constants.userID)); 
     multipartEntity.addPart("uniqueMobileID", new StringBody(Constants.uniqueMobileID)); 
     multipartEntity.addPart("userfile", new FileBody(image, "mobileimage.jpg", "image/jpeg", "UTF-8")); 
     httppost.setEntity(multipartEntity); 

     HttpResponse response = mHttpClient.execute(httppost); 
     String responseBody = EntityUtils.toString(response.getEntity()); 

     Log.d(TAG, "response: " + responseBody); 
     return responseBody; 

    } catch (Exception e) { 
     Log.d(TAG, e.getMessage()); 
    } 
    return ""; 
} 

是否有辦法來調整基於像素大小的文件?

謝謝。

回答

8

這是從ThinkAndroid採取在這個網址: http://thinkandroid.wordpress.com/2009/12/25/resizing-a-bitmap/

我會研究建立從資源位圖或可繪製的可能性,如果你想改變它的大小使用下面的代碼。

public Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth) { 
    int width = bm.getWidth(); 
    int height = bm.getHeight(); 
    float scaleWidth = ((float) newWidth)/width; 
    float scaleHeight = ((float) newHeight)/height; 

    // Create a matrix for the manipulation 
    Matrix matrix = new Matrix(); 

    // Resize the bit map 
    matrix.postScale(scaleWidth, scaleHeight); 

    // Recreate the new Bitmap 
    Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false); 
    return resizedBitmap; 

} 

編輯:正如其他評論Bitmap.createScaledBitmap建議應調整時,可以使用更好的質量。

21

使用Bitmap.createScaledBitmap正如其他建議。

但是,這個功能不是很聰明。如果你擴展到低於50%的大小,你可能會得到這樣的:

enter image description here

取而代之的是:

enter image description here

你看到第一個形象的壞抗鋸齒? createScaledBitmap會讓你得到這個結果。

原因是像素過濾,其中一些像素完全從源跳過,如果縮放到< 50%。

要獲得第二個質量結果,如果它比所需結果大2倍以上,則需要將位圖分辨率減半,最後再調用createScaledBitmap。

還有更多方法可以將圖像減半(或縮小或變亮)。如果內存中有位圖,則遞歸調用Bitmap.createScaledBitmap以將圖像減半。

如果您從JPG文件加載圖像,實現速度會更快:使用BitmapFactory.decodeFile並正確設置選項參數,主要是使用JPEG特性來控制加載圖像的子圖像的字段inSampleSize

許多提供圖像縮略圖的應用程序都是盲目地使用Bitmap.createScaledBitmap,而縮略圖只是醜陋的。要聰明,並使用適當的圖像下采樣。

3

看看谷歌recommends doingas @Pointer Null advised):

public 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) { 

     final int halfHeight = height/2; 
     final int halfWidth = width/2; 

     // Calculate the largest inSampleSize value that is a power of 2 and keeps both 
     // height and width larger than the requested height and width. 
     while ((halfHeight/inSampleSize) > reqHeight 
       && (halfWidth/inSampleSize) > reqWidth) { 
      inSampleSize *= 2; 
     } 
    } 

    return inSampleSize; 
} 

我調用上面來調整一個大的圖像:

// Check if source and destination exist 
// Check if we have read/write permissions 

int desired_width = 200; 
int desired_height = 200; 

BitmapFactory.Options options = new BitmapFactory.Options(); 
options.inJustDecodeBounds = true; 

BitmapFactory.decodeFile(SOME_PATH_TO_LARGE_IMAGE, options); 

options.inSampleSize = calculateInSampleSize(options, desired_width, desired_height); 
options.inJustDecodeBounds = false; 

Bitmap smaller_bm = BitmapFactory.decodeFile(src_path, options); 

FileOutputStream fOut; 
try { 
    File small_picture = new File(SOME_PATH_STRING); 
    fOut = new FileOutputStream(small_picture); 
    // 0 = small/low quality, 100 = large/high quality 
    smaller_bm.compress(Bitmap.CompressFormat.JPEG, 50, fOut); 
    fOut.flush(); 
    fOut.close(); 
    smaller_bm.recycle(); 
} catch (Exception e) { 
    Log.e(LOG_TAG, "Failed to save/resize image due to: " + e.toString()); 
}