Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
372 views
in Technique[技术] by (71.8m points)

android - resize image from file

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

this is the call to upload the image:

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?

Thanks.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Use Bitmap.createScaledBitmap as others suggested.

However, this function is not very smart. If you're scaling to less than 50% size, you're likely to get this:

enter image description here

Instead of this:

enter image description here

Do you see bad antialiasing of 1st image? createScaledBitmap will get you this result.

Reason is pixel filtering, where some pixels are completely skipped from source if scaling to < 50%.

To get 2nd quality result, you need to halve the Bitmap's resolution if it's more than 2x larger than desired result, then finally you make call to createScaledBitmap.

And there're more approaches to halve (or quarter, or eighten) images. If you have Bitmap in memory, you recursively call Bitmap.createScaledBitmap to halve image.

If you load image from JPG file, implementation is even faster: you use BitmapFactory.decodeFile and setup Options parameter properly, mainly field inSampleSize, which controls subsambling of loaded images, utilizing JPEG's characteristics.

Many apps which provide image thumbnails use blindly Bitmap.createScaledBitmap, and the thumbnails are just ugly. Be smart and use proper image downsampling.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...