Bitmapfactory.options detailed

Source: Internet
Author: User

Public Bitmap


Inbitmap


If set, decode methods that take the Options object would attempt to reuse this bitmap when loading content.


public int


Indensity


The pixel density to use for the bitmap.


public Boolean


Indither


If Dither is true, the decoder would attempt to dither the decoded image.


public Boolean


Ininputshareable


This field works on conjuction with Inpurgeable.


public Boolean


Injustdecodebounds


If set to True, the decoder would return null (no bitmap), but the ...


public Boolean


Inmutable


If set, decode methods would always return a mutable Bitmap instead of an immutable one.


public Boolean


Inpreferqualityoverspeed


If Inpreferqualityoverspeed is set to true, the decoder would try to decode the reconstructed image to a higher quality Eve n at the expense of the decoding speed.


Publicbitmap.config


Inpreferredconfig


If This is non-null, the decoder would try to decode into this internal configuration.


public Boolean


Inpurgeable


If the is set to true and then the resulting bitmap would allocate its pixels such that they can be purged if the system need s to reclaim memory.


public int


Insamplesize


If set to a value of > 1, requests the decoder to subsample the original image, returning a smaller image to save memory.


public Boolean


Inscaled


When this flag was set, if Indensity and intargetdensity are not 0, the bitmap would be scaled to match intargetdensity when Loaded, rather than relying on the graphics system scaling it each time it's drawn to a Canvas.


public int


Inscreendensity


The pixel density of the actual is being used.


public int


Intargetdensity


The pixel density of the destination this bitmap would be a drawn to.


Public byte[]


Intempstorage


Temp storage to use for decoding.


public Boolean


Mcancel


Flag to indicate that cancel have been called on the This object.


public int


Outheight


The resulting height of the bitmap, set independent of the state of Injustdecodebounds.


Public String


Outmimetype


If known, this string was set to the mimetype of the decoded image.


public int


Outwidth


The resulting width of the bitmap, set independent of the state of Injustdecodebounds.


The above is extracted from the Android SDK documentation, simply look at the instructions to understand what it means.

Let's go back to our topic: How do I get the size of a picture?

The idea is simple:

First we turn this image into bitmap, and then use the bitmap getwidth () and the GetHeight () method to take the picture's width high.

The new problem has come again, when the breakthrough is turned into bitmap through the Bitmapfactory.decodefile (String path) method, we often encounter the problem of Oom (out of Memory) when we encounter larger images. How to avoid it?

This is the kind of bitmapfactory.options we mentioned above.

Bitmapfactory.options This class, there is a field called Injustdecodebounds. The description of this member in the SDK is this:

If set to True, the decoder would return null (no bitmap), but the ...

That is, if we set it to true, then Bitmapfactory.decodefile (String path, Options opt) will not really return a bitmap to you, it will simply take it wide and high back to you, This will not take up too much memory, and it will not happen so frequently with oom.

The sample code is as follows:

Bitmapfactory.options Options = new Bitmapfactory.options ();

Options.injustdecodebounds = true;

Bitmap bmp = Bitmapfactory.decodefile (path, options);

/* The BMP returned here is NULL */

After this code, Options.outwidth and Options.outheight are the width and height we want.

With wide, high information, how do we get thumbnails of the size of a picture without distortion?

For example, we need to get a thumbnail with a width of 200 if the image is not deformed.

So we need to figure out what the height of the image is after scaling.

/* Calculate the height of the picture */

/* Here's the idea, if you need more precision to ensure that the picture does not deform, you need to do a math operation * *

int height = options.outheight * 200/options.outwidth;

Options.outwidth = 200;

Options.outheight = height;

/* This will really return a bitmap to you */

Options.injustdecodebounds = false;

Bitmap bmp = Bitmapfactory.decodefile (path, options);

Image.setimagebitmap (BMP);

Copy Code

So though we can get the size of ImageView we expect

However, when executing the bitmapfactory.decodefile (path, options), no memory is saved. To conserve memory, you also need to use the Insamplesize member variable in the Bitmapfactory.options class.

We can calculate this value according to the actual width of the picture and the width of the height we expect.

Insamplesize = options.outwidth/200;

In addition, in order to save memory we can also use the following several fields:

Options.inpreferredconfig = Bitmap.Config.ARGB_4444; Default is Bitmap.Config.ARGB_8888

/* The following two fields need to be combined with */

Options.inpurgeable = true;

Options.ininputshareable = true;




This error is sometimes encountered when decoding a picture with bitmapfactory. This is often caused by a large picture. To work properly, you need to allocate less memory space to store.

BitmapFactory.Options.inSampleSize

Setting the appropriate insamplesize allows the bitmapfactory to allocate less space to eliminate the error. Refer to the SDK documentation for the specific meaning of insamplesize. For example:

Bitmapfactory.options opts = new Bitmapfactory.options ();

Opts.insamplesize = 4;

Bitmap Bitmap = Bitmapfactory.decodefile (ImageFile, opts);

Setting the right insamplesize is one of the keys to solving the problem . Bitmapfactory.options provides another member of Injustdecodebounds.

Bitmapfactory.options opts = new Bitmapfactory.options ();


Opts.injustdecodebounds = true;


Bitmap Bitmap = Bitmapfactory.decodefile (ImageFile, opts);


After setting Injustdecodebounds to True, DecodeFile does not allocate space, but calculates the length and width of the original picture , which is opts.width and opts.height. With these two parameters, a proper insamplesize can be obtained by a certain algorithm.

Looking at the Android source code, we learned that in order to get the proper insamplesize,android provides a method of dynamic calculation.

public static int Computesamplesize (bitmapfactory.options Options, int minsidelength, int maxnumofpixels) {

int initialsize = computeinitialsamplesize (options, Minsidelength, maxnumofpixels);

int roundedsize;

if (initialsize <= 8) {

Roundedsize = 1;

while (Roundedsize < initialsize) {

Roundedsize <<= 1;

}

} else {

Roundedsize = (initialsize + 7)/8 * 8;

}

return roundedsize;

}


private static int computeinitialsamplesize (bitmapfactory.options Options, int minsidelength, int maxnumofpixels) {

Double w = options.outwidth;

Double h = options.outheight;

int lowerbound = (Maxnumofpixels = =-1)? 1: (int) Math.ceil (math.sqrt (w * h/maxnumofpixels));

int upperbound = (Minsidelength = =-1)? : (int) math.min (Math.floor (w/minsidelength), Math.floor (h/minsidelength));

if (Upperbound < lowerbound) {

Return the larger one when there is no overlapping zone.

return lowerbound;

}

if ((Maxnumofpixels = =-1) && (minsidelength = =-1)) {

return 1;

} else if (minsidelength = =-1) {

return lowerbound;

} else {

return upperbound;

}

}


Using this algorithm, the insamplesize of the image can be calculated dynamically.


Bitmapfactory.options opts = new Bitmapfactory.options ();

Opts.injustdecodebounds = true;

Bitmapfactory.decodefile (ImageFile, opts);

Opts.insamplesize = Computesamplesize (opts,-1, 128*128);

Opts.injustdecodebounds = false;

try {

Bitmap bmp = Bitmapfactory.decodefile (ImageFile, opts);

Imageview.setimagebitmap (BMP);

} catch (OutOfMemoryError err) {

}





To synthesize the above, the complete code is:


[Java] View Plaincopy



  1. public static Bitmap Createimagethumbnail (String filePath) {

  2. Bitmap Bitmap = null;

  3. Bitmapfactory.options opts = new Bitmapfactory.options ();

  4. Opts.injustdecodebounds = true;

  5. Bitmapfactory.decodefile (FilePath, opts);

  6. Opts.insamplesize = Computesamplesize (opts,-1, 128*128);

  7. Opts.injustdecodebounds = false;

  8. try {

  9. Bitmap = Bitmapfactory.decodefile (FilePath, opts);

  10. }catch (Exception e) {

  11. Todo:handle exception

  12. }

  13. return bitmap;

  14. }

  15. public static int Computesamplesize (bitmapfactory.options Options, int minsidelength, int maxnumofpixels) {

  16. int initialsize = computeinitialsamplesize (options, Minsidelength, maxnumofpixels);

  17. int roundedsize;

  18. if (initialsize <= 8) {

  19. Roundedsize = 1;

  20. while (Roundedsize < initialsize) {

  21. Roundedsize <<= 1;

  22. }

  23. } else {

  24. Roundedsize = (initialsize + 7)/8 * 8;

  25. }

  26. return roundedsize;

  27. }

  28. private static int computeinitialsamplesize (bitmapfactory.options options,int minsidelength, int maxnumofpixels) {

  29. Double w = options.outwidth;

  30. Double h = options.outheight;

  31. int lowerbound = (Maxnumofpixels = =-1)? 1: (int) Math.ceil (math.sqrt (w * h/maxnumofpixels));

  32. int upperbound = (Minsidelength = =-1)? :(int) math.min (Math.floor (w/minsidelength), Math.floor (h/minsidelength));

  33. if (Upperbound < lowerbound) {

  34. Return the larger one when there is no overlapping zone.

  35. return lowerbound;

  36. }

  37. if ((Maxnumofpixels = =-1) && (minsidelength = =-1)) {

  38. return 1;

  39. } else if (minsidelength = =-1) {

  40. return lowerbound;

  41. } else {

  42. return upperbound;

  43. }

  44. }



Bitmapfactory.options detailed

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.