Android Bitmap Compress (image compression) code _android

Source: Internet
Author: User

With the development of mobile hardware, Android's camera is becoming more and more powerful, and can find high resolution images.
Some scenes need to be photographed and uploaded to the service, but because the size of the picture is too large, then the upload will be very slow (in some network situation), and very traffic, to the speed, then you need to reduce the size of the picture. There are two ways to reduce the size of a picture, 1. According to the small picture; 2. Compress large pictures. Getting a small picture in a photo is generally not a good fit. Because, the clarity of the picture will be very poor, but this situation has a benefit is the application speed will be faster; compressed pictures, is the large picture compression small, reduce the quality of the picture, within a certain range, reduce the size of the picture, and meet the demand (picture is still clear). The following group describes the compression of the picture:


1. Please see http://www.jb51.net/article/37760.htm-> want to save pictures to create a directory, start camera application, you need to specify the file
2. Compression process:
2.1 Read pictures from the picture path (the picture is large, can not be all added to the memory processing, if all loaded into memory will be memory overflow)
[Java]

Copy Code code 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 Processing Picture rotation
[Java]
Copy Code code 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 Code code 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 MTX = new Matrix ();
Mtx.postrotate (rotate);
Return Bitmap.createbitmap (Bitmap, 0, 0, W, H, MTX, 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 MTX = new Matrix ();
Mtx.postrotate (rotate);
Return Bitmap.createbitmap (Bitmap, 0, 0, W, H, MTX, true);
}


2.3 Compressed Pictures
[Java]
Copy Code code as follows:

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

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


Complete method Code:
[Java]
Copy Code code 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, 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, BAOs);

}finally{
try {
if (BAOs!= null)
Baos.close ();
catch (IOException e) {
E.printstacktrace ();
}
}
return BM;

}


[Java]
Copy Code code 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'll
Guarantee
A final image with both dimensions larger than or equal to the
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'll
Guarantee
A final image with both dimensions larger than or equal to the
Requested height and width.
Insamplesize = HeightRatio < WidthRatio? Widthratio:heightratio;
}

return insamplesize;
}

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.