android image compression

Image OOM occurs frequently during mobile phone development. The methods to effectively control APP image OOM are:
1. ListView generation view optimization
2. LruCashe cache
3. Soft reference, weak reference (SoftReference or WeakReference), but this is not recommended. As of Android 2.3 (API level 9) the garbage collector collects soft/weak references more aggressively, which makes them rather ineffective. Also, prior to Android 3.0 (API level 11), visible bitmaps stored in native memory were not freed, potentially causing the app to temporarily exceed its memory limit and crash.
4, picture compression

This article first introduces two kinds of picture compression methods, and hope that experts will criticize and give advice.

First: Quality Compression:
private Bitmap compressImage(Bitmap image) {

		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		image.compress(Bitmap.CompressFormat.JPEG, 100, baos);//Quality compression method, where 100 means no compression, store the compressed data in baos
		int options = 100;
		while ( baos.toByteArray().length / 1024>100) { //Loop to determine if the compressed image is larger than 100kb, and continue to compress		
			baos.reset();//Reset baos to clear baos
options -= 10;//Reduce by 10 each time
			image.compress(Bitmap.CompressFormat.JPEG, options, baos);//compress options% here, and store the compressed data in baos
			
		}
		ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());//Store the compressed data baos in ByteArrayInputStream
		Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, null);//Generate ByteArrayInputStream data into pictures
		return bitmap;
	}

Second, proportional compression:
private Bitmap getimage(String srcPath) {
		BitmapFactory.Options newOpts = new BitmapFactory.Options();
		//Start reading the picture, set options.inJustDecodeBounds back to true at this time
		newOpts.inJustDecodeBounds = true;
		Bitmap bitmap = BitmapFactory.decodeFile(srcPath,newOpts);//The return bm is empty at this time
		
		newOpts.inJustDecodeBounds = false;
		int w = newOpts.outWidth;
		int h = newOpts.outHeight;
		//Now mainstream mobile phones are mostly 800*480 resolution, so we set the height and width to
		float hh = 800f;//Here the height is set to 800f
		float ww = 480f;//The width is set to 480f here
		// Zoom ratio. Since it is a fixed scale scaling, only one data of height or width can be used for calculation.
		int be = 1;//be=1 means no scaling
		if (w > h && w > ww) {//If the width is large, it will be scaled according to the fixed size of the width
			be = (int) (newOpts.outWidth / ww);
		} else if (w < h && h > hh) {//If the height is high, it will be scaled according to the fixed size of the width
			be = (int) (newOpts.outHeight / hh);
		}
		if (be <= 0)
			be = 1;
		newOpts.inSampleSize = be;//Set the zoom ratio
		//Re-read the picture, note that options.inJustDecodeBounds has been set back to false at this time
		bitmap = BitmapFactory.decodeFile(srcPath, newOpts);
		return compressImage(bitmap);//Compress the scale and then perform quality compression
	}


Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326988851&siteId=291194637