Reliable Image Compression for android

Source: Internet
Author: User

Reliable Image Compression for android

First, let's take a look at the quality compression method:

Private Bitmap compressImage (Bitmap image ){

ByteArrayOutputStream baos = new ByteArrayOutputStream ();

Image. compress (Bitmap. CompressFormat. JPEG, 100, baos); // quality compression method. Here, 100 indicates no compression. Store the compressed data in baos.

Int options = 100;

While (baos. toByteArray (). length/1024> 100) {// cyclically determine if the image is larger than kb after compression and greater than Continue compression

Baos. reset (); // reset baos to clear baos

Image. compress (Bitmap. CompressFormat. JPEG, options, baos); // compress options % to save the compressed data to baos.

Options-= 10; // reduce each time by 10

}

ByteArrayInputStream isBm = new ByteArrayInputStream (baos. toByteArray (); // store the compressed data baos in ByteArrayInputStream

Bitmap bitmap = BitmapFactory. decodeStream (isBm, null, null); // generates an image from ByteArrayInputStream

Return bitmap;

}

Second, the image is compressed proportionally (the image is obtained and compressed Based on the path ):

Private Bitmap getimage (String srcPath ){

BitmapFactory. Options newOpts = new BitmapFactory. Options ();

// Start reading the image, and set options. inJustDecodeBounds back to true.

NewOpts. inJustDecodeBounds = true;

Bitmap bitmap = BitmapFactory. decodeFile (srcPath, newOpts); // at this time, the returned bm is empty.

NewOpts. inJustDecodeBounds = false;

Int w = newOpts. outWidth;

Int h = newOpts. outHeight;

// Currently, most mainstream mobile phones have a resolution of 800*480, so we set the height and width

Float hh = 800f; // set the height to 800f.

Float ww = 480f; // set the width to 480f.

// Scale ratio. Because it is a fixed proportional scaling, only one of the data in the height or width can be calculated.

Int be = 1; // be = 1 indicates no Scaling

If (w> h & w> ww) {// if the width is large, scale it according to the fixed width.

Be = (int) (newOpts. outWidth/ww );

} Else if (w

Be = (int) (newOpts. outHeight/hh );

}

If (be <= 0)

Be = 1;

NewOpts. inSampleSize = be; // set the scaling ratio.

// Read the image again. Note that options. inJustDecodeBounds has been set back to false.

Bitmap = BitmapFactory. decodeFile (srcPath, newOpts );

Return compressImage (bitmap); // compress the proportion before performing quality compression.

}

Third: proportional image compression method (based on 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) {// determine if the image is larger than 1 MB, compress the image to avoid overflow when generating the image (BitmapFactory. decodeStream ).

Baos. reset (); // reset baos to clear baos

Image. compress (Bitmap. CompressFormat. JPEG, 50, baos); // compress 50% to save the compressed data to baos.

}

ByteArrayInputStream isBm = new ByteArrayInputStream (baos. toByteArray ());

BitmapFactory. Options newOpts = new BitmapFactory. Options ();

// Start reading the image, and set options. inJustDecodeBounds back to true.

NewOpts. inJustDecodeBounds = true;

Bitmap bitmap = BitmapFactory. decodeStream (isBm, null, newOpts );

NewOpts. inJustDecodeBounds = false;

Int w = newOpts. outWidth;

Int h = newOpts. outHeight;

// Currently, most mainstream mobile phones have a resolution of 800*480, so we set the height and width

Float hh = 800f; // set the height to 800f.

Float ww = 480f; // set the width to 480f.

// Scale ratio. Because it is a fixed proportional scaling, only one of the data in the height or width can be calculated.

Int be = 1; // be = 1 indicates no Scaling

If (w> h & w> ww) {// if the width is large, scale it according to the fixed width.

Be = (int) (newOpts. outWidth/ww );

} Else if (w

Be = (int) (newOpts. outHeight/hh );

}

If (be <= 0)

Be = 1;

NewOpts. inSampleSize = be; // set the scaling ratio.

// Read the image again. Note that options. inJustDecodeBounds has been set back to false.

IsBm = new ByteArrayInputStream (baos. toByteArray ());

Bitmap = BitmapFactory. decodeStream (isBm, null, newOpts );

Return compressImage (bitmap); // compress the proportion before performing quality compression.

}

Related Article

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.