Android bitmap compress (image compression) Code

Source: Internet
Author: User

With the development of mobile phone hardware, android camera functions become more and more powerful, and can find high-resolution images.
In some scenarios, you need to take photos and upload them to the service. However, because the image size is too large, uploading will be very slow (in some network scenarios) and will consume a lot of traffic, to achieve high speed, we need to reduce the image size. There are two ways to reduce the image size: 1. Take a small image; 2. Compress a large image. It is generally not suitable for obtaining small images during photography because the quality of the images is poor, but the advantage of this situation is that the application speed is faster; compressing the images is to compress the large images, reduce the image quality, reduce the image size within a certain range, and meet the requirements (the image is still clear ). The following sections describe image compression:

1. Take a photo please view the http://www.jb51.net/article/37760.htm-> to save the picture to the formulated directory, You need to specify the file when starting the Camera Application
2. compression process:
2.1 read images from the image path (images are large and cannot be added to the memory for processing. If all images are loaded to the memory, the memory will overflow)
[Java]

Copy codeThe Code is as follows: final BitmapFactory. Options options = new BitmapFactory. Options ();
Options. inJustDecodeBounds = true;
BitmapFactory. decodeFile (filePath, options );

// Calculate inSampleSize
Options. inSampleSize = calculateInSampleSize (options, 480,800 );

// Decode bitmap with inSampleSize set
Options. inJustDecodeBounds = false;

Bitmap bm = BitmapFactory. decodeFile (filePath, options );

Final BitmapFactory. Options options = new BitmapFactory. Options ();
Options. inJustDecodeBounds = true;
BitmapFactory. decodeFile (filePath, options );

// Calculate inSampleSize
Options. inSampleSize = calculateInSampleSize (options, 480,800 );

// Decode bitmap with inSampleSize set
Options. inJustDecodeBounds = false;

Bitmap bm = BitmapFactory. decodeFile (filePath, options );

2.2 process image rotation
[Java]Copy codeThe Code is as follows: int degree = readPictureDegree (filePath );
Bm = rotateBitmap (bm, degree );

Int degree = readPictureDegree (filePath );
Bm = rotateBitmap (bm, degree); [java] view plaincopyprint? Private static int readPictureDegree (String path ){
Int degree = 0;
Try {
ExifInterface exifInterface = new ExifInterface (path );
Int orientation = exifInterface. getAttributeInt (ExifInterface. TAG_ORIENTATION, ExifInterface. ORIENTATION_NORMAL );
Switch (orientation ){
Case ExifInterface. ORIENTATION_ROTATE_90:
Degree = 90;
Break;
Case ExifInterface. ORIENTATION_ROTATE_180:
Degree = 180;
Break;
Case ExifInterface. ORIENTATION_ROTATE_270:
Degree = 270;
Break;
}
} Catch (IOException e ){
E. printStackTrace ();
}
Return degree;
}

Private static int readPictureDegree (String path ){
Int degree = 0;
Try {
ExifInterface exifInterface = new ExifInterface (path );
Int orientation = exifInterface. getAttributeInt (ExifInterface. TAG_ORIENTATION, ExifInterface. ORIENTATION_NORMAL );
Switch (orientation ){
Case ExifInterface. ORIENTATION_ROTATE_90:
Degree = 90;
Break;
Case ExifInterface. ORIENTATION_ROTATE_180:
Degree = 180;
Break;
Case ExifInterface. ORIENTATION_ROTATE_270:
Degree = 270;
Break;
}
} Catch (IOException e ){
E. printStackTrace ();
}
Return degree;
}

[Java]Copy codeThe Code is as follows: view plaincopyprint? Private static Bitmap rotateBitmap (Bitmap bitmap, int rotate ){
If (bitmap = null)
Return null;

Int w = bitmap. getWidth ();
Int h = bitmap. getHeight ();

// Setting post rotate to 90
Matrix CTX = new Matrix ();
CTX. postRotate (rotate );
Return Bitmap. createBitmap (bitmap, 0, 0, w, h, CTX, true );
}

Private static Bitmap rotateBitmap (Bitmap bitmap, int rotate ){
If (bitmap = null)
Return null;

Int w = bitmap. getWidth ();
Int h = bitmap. getHeight ();

// Setting post rotate to 90
Matrix CTX = new Matrix ();
CTX. postRotate (rotate );
Return Bitmap. createBitmap (bitmap, 0, 0, w, h, CTX, true );
}

2.3 compressed image
[Java]Copy codeThe Code is as follows: bm. compress (Bitmap. CompressFormat. JPEG, 30, baos); // 30 is the compression ratio, which indicates that the compression rate is 70%. If the compression rate is 100, the compression rate is 0.

Bm. compress (Bitmap. CompressFormat. JPEG, 30, baos); // 30 indicates the compression rate, which means the compression rate is 70%. If the compression rate is 100, the compression rate is 0.

Complete method code:
[Java]Copy codeThe Code is as follows: public static Bitmap getSmallBitmap (String filePath ){

Final BitmapFactory. Options options = new BitmapFactory. Options ();
Options. inJustDecodeBounds = true;
BitmapFactory. decodeFile (filePath, options );

// Calculate inSampleSize
Options. inSampleSize = calculateInSampleSize (options, 480,800 );

// Decode bitmap with inSampleSize set
Options. inJustDecodeBounds = false;

Bitmap bm = BitmapFactory. decodeFile (filePath, options );
If (bm = null ){
Return null;
}
Int degree = readPictureDegree (filePath );
Bm = rotateBitmap (bm, degree );
ByteArrayOutputStream baos = null;
Try {
Baos = new ByteArrayOutputStream ();
Bm. compress (Bitmap. CompressFormat. JPEG, 30, baos );

} Finally {
Try {
If (baos! = Null)
Baos. close ();
} Catch (IOException e ){
E. printStackTrace ();
}
}
Return bm;

}

Public static Bitmap getSmallBitmap (String filePath ){

Final BitmapFactory. Options options = new BitmapFactory. Options ();
Options. inJustDecodeBounds = true;
BitmapFactory. decodeFile (filePath, options );

// Calculate inSampleSize
Options. inSampleSize = calculateInSampleSize (options, 480,800 );

// Decode bitmap with inSampleSize set
Options. inJustDecodeBounds = false;

Bitmap bm = BitmapFactory. decodeFile (filePath, options );
If (bm = null ){
Return null;
}
Int degree = readPictureDegree (filePath );
Bm = rotateBitmap (bm, degree );
ByteArrayOutputStream baos = null;
Try {
Baos = new ByteArrayOutputStream ();
Bm. compress (Bitmap. CompressFormat. JPEG, 30, baos );

} Finally {
Try {
If (baos! = Null)
Baos. close ();
} Catch (IOException e ){
E. printStackTrace ();
}
}
Return bm;

}

[Java]Copy codeThe Code is as follows: view plaincopyprint? Private static int calculateInSampleSize (BitmapFactory. Options options,
Int reqWidth, int reqHeight ){
// Raw height and width of image
Final int height = options. outHeight;
Final int width = options. outWidth;
Int inSampleSize = 1;

If (height> reqHeight | width> reqWidth ){

// Calculate ratios of height and width to requested height and
// Width
Final int heightRatio = Math. round (float) height
/(Float) reqHeight );
Final int widthRatio = Math. round (float) width/(float) reqWidth );

// Choose the smallest ratio as inSampleSize value, this will
// Guarantee
// A final image with both dimensions larger than or equal to
// Requested height and width.
InSampleSize = heightRatio <widthRatio? WidthRatio: heightRatio;
}

Return inSampleSize;
}

Private static int calculateInSampleSize (BitmapFactory. Options options,
Int reqWidth, int reqHeight ){
// Raw height and width of image
Final int height = options. outHeight;
Final int width = options. outWidth;
Int inSampleSize = 1;

If (height> reqHeight | width> reqWidth ){

// Calculate ratios of height and width to requested height and
// Width
Final int heightRatio = Math. round (float) height
/(Float) reqHeight );
Final int widthRatio = Math. round (float) width/(float) reqWidth );

// Choose the smallest ratio as inSampleSize value, this will
// Guarantee
// A final image with both dimensions larger than or equal to
// Requested height and width.
InSampleSize = heightRatio <widthRatio? WidthRatio: heightRatio;
}

Return inSampleSize;
}

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.