1. Attributes in Bitmapfactory.options
In the image compression, by setting some values of bitmapfactory.options to change the properties of the picture, the following we look at the bitmapfactory.options often used in the attribute meaning:
- Options.inpreferredconfig -Set the bitmap preference configuration. The value is bitmap.config.argb_8888,bitmap.config.argb_4444,bitmap.config.argb_8888. Bitmap.Config.RGB_565.
I feel argb_8888, as the name implies. This is to set the display quality of the bitmap.
- Options.outheight -Get the height of the bitmap.
- Options.outwidth -Get the width of the bitmap.
- Options.outmimetype -Gets the MIME value of the bitmap.
- options.insamplesize -Compression ratio. Suppose options.insamplesize = 4; Then the width and height of the compressed picture are 1/4 of the original picture. The pixel of the compressed picture is 1/16 of the original picture.
- options. injustdecodebounds - This is what is described in the official documentation:
- if set to True, the decoder would return null (no bitmap), but
the out ... fields would still BES set, allowing the caller to query
the bitmap without have to allocate the memory for its pixels.
- Options opts) or bitmapfactory.decodexxx (..., Options opts) method does not actually return a bitmap object, but instead returns NULL. Although we return null, we are able to get some of the value of the bitmap through the options. such as its width, height, mimetype equivalent.
This eliminates the need to allocate memory space for the bitmap object and to get its width and height. thus saving memory. So this property is very helpful for checking our images before compressing them. The technique used frequently is to avoid the image being too large to cause oom, so before we load the picture we set it to true, get the width, the high value of the picture, and then make some inference checks. Determines whether the picture is compressed. Let's take a look at the example of loading a 405x579 image:
-
Final Bitmapfactory.options Options = new Bitmapfactory.options (); options.injustdecodebounds = true; Bitmap Bitmap = Bitmapfactory.decoderesource (GetResource (), r.mipmap.two, options); LOG.V ("Zxy", "bitmap=" +bitmap); int height = options.outheight;int width = options.outwidth; String mimeType = Options.outmimetype; LOG.V ("Zxy", "mimetype=" +mimetype+ ", height=" +height+ ", width=" +width ");The above code outputs: to be able to know that it is indeed returning null. and also obtained the width and height equivalence of the bitmap.
2, through the example to see how to compress the picture
public class Mainactivity extends Actionbaractivity {private ImageView mimageview, Mresizeimageview; @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); Mimageview = (ImageView) Findviewbyid (R.id.imageview); Mresizeimageview = (ImageView) Findviewbyid (R.id.resize_imageview); Mimageview.setimagebitmap (Bitmapfactory.decoderesource (Getresources (), r.mipmap.two)); Bitmap Bitmap = Compressbitmap (Getresources (), R.mipmap.two, 100, 100); LOG.V ("Zxy", "compressbitmap,width=" + bitmap.getwidth () + ", height=" + bitmap.getheight ()); Mresizeimageview.setimagebitmap (bitmap); }/** * @param res Resource * @param resId Resource ID * @param targetwidth target image width, Unit px * @param targetheight The height of the target picture, the unit PX * @return returns the compressed picture of Bitmap */public Bitmap Compressbitmap (Resources res, int resId, int targetwidth , int targetheight) {finAl bitmapfactory.options Options = new Bitmapfactory.options (); Options.injustdecodebounds = true;//set to True, save memory Bitmapfactory.decoderesource (res, resId, options);//return NULL I NT height = options.outheight;//Gets the source picture height, the unit px int width = options.outwidth;//Gets the width of the source picture. Unit px//calculation insamplesize options.insamplesize = calculateinsamplesize (width,height,targetwidth,targetheight); Options.injustdecodebounds = false;//is set to False to return to bitmap return Bitmapfactory.decoderesource (res,resid,option s); /** * Calculate the compression scale * @param width of the source picture * @param height of the source picture * @param targetwidth the width of the target picture * @param ta Rgetheight High * @return insamplesize compression ratio for target images */public int calculateinsamplesize (int width,int height, int tar getwidth, int targetheight) {int insamplesize = 1; if (Height > Targetheight | | width > targetwidth) {//Calculate the actual width of the picture and the ratio of the target picture width to the final int heightrate = Math.Round (float) HeigHT/(float) targetheight); Final int widthrate = Math.Round ((float) width/(float) targetwidth); Choose the minimum ratio as insamplesize insamplesize = Heightrate < widthrate? Heightrate:widthrate; } return insamplesize; }}We can see the width and height of the compressed image by looking at log:
Let's look again at:
Image compression when working with large images in Android