Image Scaling Method in Android

Source: Internet
Author: User

Method 1: Scale by fixed proportion

When developing image browsers and other software, it is often necessary to display the thumbnail of an image. In general, we need to take a thumbnail of the image according to a fixed size, generally, the thumbnails are obtained using the decodefile method of bitmapfactory, and then transmitted to bitmapfactory. the option type parameter is used to retrieve the thumbnail. In option, the attribute value insamplesize indicates that the thumbnail size is one of the fraction of the original image size, that is, if the value is 2, the width and height of the thumbnail are 1/2 of the original image, and the image size is 1/4 of the original size.

However, it is more difficult to obtain a fixed-size thumbnail. For example, we want to make the height of the thumbnail of an image of different sizes PX, and ensure that the image is not distorted, what should we do? We can't always load the original image into the memory for scaling. You must know that in mobile development, memory is very valuable, and a K image, after loading the occupied memory, what is the limit of 100 kb?

After research, we found that there is an injustdecodebounds attribute in options. After a research, we finally understood what it meant. This is what E in the SDK said.

If set to true,
Decoder will return NULL (no bitmap), but the out... fields will still be set,
Allowing the caller to query the bitmap without having to allocate the memory
For its pixels.

This means that if this value is set to true, the actual bitmap will not be returned and the memory space will not be allocated to it, but only some decoded boundary information, that is, the image size information, will be included, then the corresponding method will come out. By setting injustdecodebounds to true, get outheight (original image height) and outwidth (original image width), and then calculate an insamplesize (zoom value ), then you can take the image. Note that the insamplesize may be smaller than 0 and must be determined. That is to say, first set the options attribute injustdecodebounds to true, first obtain the basic image size data (the information is not saved in bitmap, but saved in options), and use options. outheight and options. calculate the size information obtained by outwidth and the size of the image you want, and set injustdecodebounds to false, you can get the desired image scaling Chart Based on the obtained scaling ratio.

DetailsCodeAs follows:

Framelayout
Fr = (framelayout) findviewbyid (R. Id. framelayout01 );


Bitmapfactory. Options = new bitmapfactory. Options ();


Options. injustdecodebounds = true;


Bitmap bitmap = bitmapfactory. decodefile ("/sdcard/test.jpg ",
Options); // at this time, the returned BM is null.


Options. injustdecodebounds = false;


// Scale ratio. Because it is a fixed proportional scaling, only one of the data in the height or width can be calculated.


Int be = (INT) (options. outheight/(float) 200 );


If (be <= 0)


Be = 1;


Options. insamplesize = be;


// Read the image again. Note that the options. injustdecodebounds
Set to false.


Bitmap = bitmapfactory. decodefile ("/sdcard/test.jpg", options );


Int W = bitmap. getwidth ();


Int H = bitmap. getheight ();


System. Out. println (W +"
"+ H );


Imageview IV = new imageview (this );

Iv. setimagebitmap (Bitmap );

In this way, we can read large images without memory overflow. If you want to save the compressed image on the sdcard, It's easy:

File file = new
File ("/sdcard/feng.png ");


Try {


Fileoutputstream out = new fileoutputstream (File );


If (bitmap. Compress (bitmap. compressformat. PNG, 100, out )){

Out. Flush ();

Out. Close ();


}


} Catch (filenotfoundexception e ){


// Todo auto-generated Catch Block


E. printstacktrace ();


} Catch (ioexception e ){


// Todo auto-generated Catch Block


E. printstacktrace ();


}

OK, so that the image is saved in the/sdcard/feng.png file.

Method 2: Scale by the ratio of length to width

However, the scaling here is saved in the ratio of length to width. You can also scale it down to a fixed size:


Int BMP width =
Bitmap. getwidth ();


Int bmpheight =
Bitmap. getheight ();


// Scale the image size


Float scalewidth = (float) swidth
/BMP width; // scale swidth to a fixed size.


Float scaleheight = (float) sheight/bmpheight ;//


Matrix matrix = new matrix ();


Matrix. postscale (scalewidth, scaleheight); // generates a scaled bitmap object.


Bitmap resizebitmap = bitmap. createbitmap (bitmap, 0, 0, BMP width,
Bmpheight, matrix, false );


Bitmap. Recycle ();


Bitmap resizebitmap = bitmap;


// Bitmap to byte []


Byte [] photodata = bitmap2bytes (resizebitmap );


// Save file


String filename = "/sdcard/test.jpg ";


fileutil. writetofile (filename, photodata);

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.