从文件中调整图像大小

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

有没有一种根据像素尺寸调整文件大小的方法?
谢谢。
4个回答

22

按照其他人建议,使用Bitmap.createScaledBitmap函数。但是,该函数并不十分智能。如果你缩小到少于50%的大小,很可能会得到这个结果:

enter image description here

而不是这个:

enter image description here

你看到了第一张图片的坏抗锯齿效果吗?createScaledBitmap将给你这样的结果。

原因是像素过滤,如果缩小时,从源中跳过一些像素。

要获得第二个质量的结果,如果位图的分辨率比所需的结果大2倍以上,则需要将其分辨率减半,然后最后调用createScaledBitmap。

还有更多缩小图像的方法。如果您在内存中有位图,请递归调用Bitmap.createScaledBitmap来减半图像。

如果从JPG文件加载图像,则实现速度更快:您可以使用BitmapFactory.decodeFile方法并正确设置Options参数,主要是字段inSampleSize,它控制了加载图像的子采样,利用了JPEG的特性。

许多提供图像缩略图的应用程序都盲目地使用Bitmap.createScaledBitmap,缩略图只是丑陋的。聪明点,使用适当的图像下采样。


那不是“糟糕的抗锯齿”,而是本身的混叠。 - Agent_L

9
这是从ThinkAndroid的这个网址取得的: http://thinkandroid.wordpress.com/2009/12/25/resizing-a-bitmap/ 如果您想改变其大小,请考虑从资源创建Bitmap或Drawable,然后使用下面的代码。
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来获得更好的质量。


5

5

查看Google 建议做什么如@Pointer Null所建议的):

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());
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接