Android Application Development (image processing technique 1)

Source: Internet
Author: User

Image processing is often used in Android Application Development. Today we will share some tips on getting fixed-width images.

In mobile development, memory resources are very valuable and there are limits on the memory space for image loading. Therefore, we will attach images to process the images accordingly. Sometimes, in order to increase the response speed, to enhance the user experience, when loading large images, we will first load the thumbnail of the image, such as the original image, so we need to take the image as a fixed size thumbnail, 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, the 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.
The Code is as follows:

Framelayout Fr = (framelayout) findviewbyid (R. Id. framelayout01 );
Bitmapfactory. Options = new bitmapfactory. Options ();
Options. injustdecodebounds = true;
// Obtain the width and height of the image.
Bitmap bitmap = bitmapfactory. decodefile ("/sdcard/test.jpg", options); // The returned BM is empty.
Options. injustdecodebounds = false;
// Calculate the zoom Ratio
Int be = (INT) (options. outheight/(float) 200 );
If (be <= 0)
Be = 1;
Options. insamplesize = be;
// Read the image again. Set options. injustdecodebounds to false this time.
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 to avoid 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 ){
E. printstacktrace ();
} Catch (ioexception e ){
E. printstacktrace ();
}
OK, so that the image is saved in the/sdcard/feng.png file.

The following figure shows the image effects I have tested and compared.

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.