Examples of three methods of android image compression
- 2020-05-19 05:43:32
- OfStack
android image compression method:
1. Mass compression method:
private Bitmap compressImage(Bitmap image) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.JPEG, 100, baos);// Mass compression method, here 100 Is not compressed, the compressed data stored in baos In the
int options = 100;
while ( baos.toByteArray().length / 1024>100) { // Loop to determine if the image is larger than if compressed 100kb, Greater than continue to compress
baos.reset();// reset baos The empty baos
options -= 10;// It goes down every time 10
image.compress(Bitmap.CompressFormat.JPEG, options, baos);// This compression options% , to store the compressed data into baos In the
}
ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());// You compress the data baos Deposit to ByteArrayInputStream In the
Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, null);// the ByteArrayInputStream Data generation picture
return bitmap;
}
2. Image compression method (get the image according to the path and compress it) :
private Bitmap getimage(String srcPath) {
BitmapFactory.Options newOpts = new BitmapFactory.Options();
// Start reading in the picture options.inJustDecodeBounds Set back true the
newOpts.inJustDecodeBounds = true;
Bitmap bitmap = BitmapFactory.decodeFile(srcPath,newOpts);// At this time back bm Is empty
newOpts.inJustDecodeBounds = false;
int w = newOpts.outWidth;
int h = newOpts.outHeight;
// Now the mainstream mobile phone is more 800*480 The resolution, so the height and the width are set to zero
float hh = 800f;// I'm going to set the height to zero 800f
float ww = 480f;// I'm going to set the width to zero 480f
// Zoom ratio. Since the scale is fixed, only the height or width is used 1 The data can be calculated
int be = 1;//be=1 Means no scaling
if (w > h && w > ww) {// If the width is large, it will scale according to the width
be = (int) (newOpts.outWidth / ww);
} else if (w < h && h > hh) {// If the height is high, it will scale according to the width
be = (int) (newOpts.outHeight / hh);
}
if (be <= 0)
be = 1;
newOpts.inSampleSize = be;// Set the scale
// Read in the picture again and notice that it is already there options.inJustDecodeBounds Set back false the
bitmap = BitmapFactory.decodeFile(srcPath, newOpts);
return compressImage(bitmap);// After the compression of a good proportion of the size of the mass compression
}
Picture proportion compression, I saw 1 algorithm, said relatively fast.
be = (int) ((w / STANDARD_WIDTH + h/ STANDARD_HEIGHT) / 2);
Conclusion 2: the compression ratio of the picture is (width compression ratio + height compression ratio) /2..
3. Image compression by proportional size (according to Bitmap image compression) :
private Bitmap comp(Bitmap image) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.JPEG, 100, baos);
if( baos.toByteArray().length / 1024>1024) {// Judge if the picture is greater than 1M, Compress to avoid generating images ( BitmapFactory.decodeStream When the overflow
baos.reset();// reset baos The empty baos
image.compress(Bitmap.CompressFormat.JPEG, 50, baos);// This compression 50% , to store the compressed data into baos In the
}
ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());
BitmapFactory.Options newOpts = new BitmapFactory.Options();
// Start reading in the picture options.inJustDecodeBounds Set back true the
newOpts.inJustDecodeBounds = true;
Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, newOpts);
newOpts.inJustDecodeBounds = false;
int w = newOpts.outWidth;
int h = newOpts.outHeight;
// Now the mainstream mobile phone is more 800*480 The resolution, so the height and the width are set to zero
float hh = 800f;// I'm going to set the height to zero 800f
float ww = 480f;// I'm going to set the width to zero 480f
// Zoom ratio. Since the scale is fixed, only the height or width is used 1 The data can be calculated
int be = 1;//be=1 Means no scaling
if (w > h && w > ww) {// If the width is large, it will scale according to the width
be = (int) (newOpts.outWidth / ww);
} else if (w < h && h > hh) {// If the height is high, it will scale according to the width
be = (int) (newOpts.outHeight / hh);
}
if (be <= 0)
be = 1;
newOpts.inSampleSize = be;// Set the scale
newOpts.inPreferredConfig = Config.RGB_565;// Lower the image from ARGB888 to RGB565
// Read in the picture again and notice that it is already there options.inJustDecodeBounds Set back false the
isBm = new ByteArrayInputStream(baos.toByteArray());
bitmap = BitmapFactory.decodeStream(isBm, null, newOpts);
return compressImage(bitmap);// After the compression of a good proportion of the size of the mass compression
}