2011-12-07 38 views
9

Tôi đang tải hình ảnh lên máy chủ và trước khi điều đó xảy ra, tôi muốn thay đổi kích thước kích thước hình ảnh. Tôi có được hình ảnh với một URI như thế này:thay đổi kích thước hình ảnh từ tập tin

Constants.currImageURI = data.getData(); 

này được gọi để tải lên hình ảnh:

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 ""; 
} 

Có cách nào để thay đổi kích thước tập tin dựa trên kích thước pixel?

Cảm ơn.

Trả lời

8

này được lấy từ ThinkAndroid tại url này: http://thinkandroid.wordpress.com/2009/12/25/resizing-a-bitmap/

tôi sẽ xem xét khả năng tạo ra một Bitmap hoặc drawable từ tài nguyên và nếu bạn muốn thay đổi nó kích thước sử dụng mã dưới đây.

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; 

} 

CHỈNH SỬA: Như được đề xuất trong nhận xét khác Bitmap.createScaledBitmap nên được sử dụng để có chất lượng tốt hơn khi thay đổi kích thước.

21

Sử dụng Bitmap.createScaledBitmap như những người khác đề nghị.

Tuy nhiên, chức năng này không thông minh lắm. Nếu bạn đang mở rộng quy mô để kích thước nhỏ hơn 50%, bạn đang có khả năng để có được điều này:

enter image description here

Thay vì điều này:

enter image description here

Bạn có thấy khử răng cưa xấu hình ảnh của 1 ? createScaledBitmap sẽ cho bạn kết quả này.

Lý do là lọc pixel, trong đó một số pixel bị bỏ qua hoàn toàn khỏi nguồn nếu chia tỷ lệ thành < 50%.

Để có được kết quả chất lượng thứ 2, bạn cần giảm một nửa độ phân giải của Bitmap nếu độ phân giải lớn hơn 2 lần so với kết quả mong muốn, sau đó bạn thực hiện cuộc gọi tới createScaledBitmap.

Và có nhiều cách tiếp cận hơn để giảm một nửa (hoặc quý hoặc tám) hình ảnh. Nếu bạn có Bitmap trong bộ nhớ, bạn đệ quy gọi Bitmap.createScaledBitmap để giảm một nửa hình ảnh.

Nếu bạn tải hình ảnh từ tệp JPG, triển khai thậm chí còn nhanh hơn: bạn sử dụng BitmapFactory.decodeFile và thiết lập thông số Tùy chọn đúng cách, chủ yếu là trường inSampleSize, điều khiển việc xếp hạng các hình ảnh đã tải, sử dụng các đặc tính của JPEG.

Nhiều ứng dụng cung cấp hình thu nhỏ hình ảnh sử dụng Bitmap.createScaledBitmap một cách mù quáng và hình thu nhỏ chỉ xấu xí. Hãy thông minh và sử dụng định dạng hình ảnh thích hợp.

3

Xem những gì Google recommends doing (as @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; 
} 

tôi gọi là ở trên để thay đổi kích thước một hình ảnh lớn:

// 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()); 
} 
Các vấn đề liên quan