Write before
Previous work on the processing of pictures can actually be done, but today I read a blog and found that I have some misunderstandings in my knowledge, record it here for my future study.
Quality compression for android picture compression.
Previously, it was thought that image quality compression could reduce oom generation. In fact, instead, he just guaranteed image quality while reducing file size, he would not actually change the memory occupied.Quality compression is to change the bit depth and transparency of the picture while preserving the pixels, so as to compress the picture. The size of the picture file compressed by it will change, but the memory occupied by the imported bitmap will not change.Because you want to keep the pixels unchanged, it cannot be compressed indefinitely and will not continue to get smaller after reaching a value.Obviously, this method does not apply to thumbnails, nor does it actually work to reduce memory usage by compressing pictures, only when you want to reduce file size while maintaining picture quality
Quality compression:
//Parameter 1 bitmap Picture Parameter 2 Maximum number of pictures you want to compress
public Bitmap compressImage(Bitmap image,int imageSize) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
//Quality compression method, where 100 means compression quality and 100 means no compression, storing compressed data in baos
image.compress(Bitmap.CompressFormat.JPEG, 100, baos);
int options = 100;
//Loop to determine if the compressed picture is larger than the maximum value you set and continue compressing if it is larger than
while ( baos.toByteArray().length / 1024>imageSize) {
//Resetting baos clears the baos
baos.reset();
//Reduce by 5 each time
options -= 5;
//Compress options here to store compressed data in baos
image.compress(Bitmap.CompressFormat.JPEG, options, baos);
}
//Store compressed data baos in ByteArrayInputStream
ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());
//Generate pictures from ByteArrayInputStream data
Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, null);
return bitmap;
}
Size compression for android picture compression.
Dimension compression is to compress the pixels of a picture, the size of a picture's memory Picture type Width Height,
public Bitmap getSmallIcon(String filePath,int yourWidth,int yourHeight) {
BitmapFactory.Options opt= new BitmapFactory.Options();
//Disable memory allocation for bitmap
opt.inJustDecodeBounds = true;
BitmapFactory.decodeFile(filePath, opt);
opt.inSampleSize = calculateInSampleSize(opt, yourWidth,yourHeight);
//Allow memory allocation for bitmap to get compressed bitmap pictures
opt.inJustDecodeBounds = false;
Bitmap bitmap = BitmapFactory.decodeFile(filePath, opt);
return bitmap ;
}
public static int calculateInSampleSize(BitmapFactory.Options options,int yourWidth, int yourHeight) {
//Get the length, width and MIME properties of the picture
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > yourHeight|| width > yourWidth) {
// Calculate the ratio of actual width to target width
final int heightRatio = Math.round((float) height / (float) yourHeight);
final int widthRatio = Math.round((float) width / (float) yourWidth);
// Select the smallest ratio of width to height as the value of inSampleSize to ensure the width and height of the final picture
// Must be greater than or equal to the width and height of the target.
inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
}
return inSampleSize;
}
Write after
In actual development, these two methods are best used in combination.
I have also used some third-party compression frameworks like luban in my study. I find that there are always some problems, such as compressing several pictures anr, etc. I write my own music.
If you have a good idea, please let me know.Thank you for sharing.