从文件调整图像大小 [英] resize image from file

查看:17
本文介绍了从文件调整图像大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将图像上传到服务器,在此之前,我想调整图像尺寸.我得到一个带有 URI 的图像,如下所示:

I am uploading an image to a server and before that happens, I would like to resize the image dimensions. I get the image with a URI like this:

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

有没有办法根据像素尺寸调整文件大小?

Is there a way to resize the file based on pixel dimensions?

谢谢.

推荐答案

本文来自 ThinkAndroid,网址为:http://thinkandroid.wordpress.com/2009/12/25/调整位图大小/

This is taken from ThinkAndroid at this url: http://thinkandroid.wordpress.com/2009/12/25/resizing-a-bitmap/

我会研究从资源创建位图或可绘制对象的可能性,如果您想更改它的大小,请使用下面的代码.

I would look into the possibility of creating a Bitmap or Drawable from the resource and if you want to change it's size use the code below.

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 应该用于在调整大小时获得更好的质量.

As suggested in other comment Bitmap.createScaledBitmap should be used for better quality when resizing.

这篇关于从文件调整图像大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆