With the development of mobile hardware, the camera function of Android bitmap compressandroid is becoming more and more powerful and able to find high resolution images. Some scenes, need to take photos and upload to the service, but because the size of the picture is too large, then the upload will be very slow (in some network conditions), and very consumption of traffic, to speed up, then you need to reduce the size of the picture. There are two ways to reduce the size of a picture, 1. Take a small picture; 2. Compress large pictures. Take pictures of small images generally do not meet the requirements, because the clarity of the picture will be very poor, but this situation has a benefit is that the application speed will be faster; compress the picture, is to compress the large picture, reduce the quality of the picture, in a certain range, reduce the size of the picture, and meet the needs (picture is still clear). The following group should describe the compression of the picture: 1. For photos, see http://blog.csdn.net/luhuajcdd/article/details/8826587
To save a picture to a directory, you need to specify a file when you start the camera app2. Compression process: 2.1 Read the picture from the picture path (the picture is very large, can not be all added to the memory processing, if all loaded into memory memory overflow)
[Java]View Plaincopy
- 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 Handling Picture Rotation
[Java]View Plaincopy
- int degree = Readpicturedegree (FilePath);
- BM = Rotatebitmap (bm,degree);
[Java]View Plaincopy
- 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]View Plaincopy
- 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
- Matrix MTX = new Matrix ();
- Mtx.postrotate (rotate);
- return Bitmap.createbitmap (Bitmap, 0, 0, W, H, MTX, true);
- }
2.3 Compressing pictures
[Java]View Plaincopy
- Bm.compress (Bitmap.CompressFormat.JPEG, BAOs); 30 is the compression ratio, which means compression 70%; If no compression is 100, it indicates a compression rate of 0
The Complete method code:
[Java]View Plaincopy
- 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]View Plaincopy
- 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 would
- //Guarantee
- //A final image with both dimensions larger than or equal to the
- //requested height and width.
- Insamplesize = HeightRatio < WidthRatio? Widthratio:heightratio;
- }
- return insamplesize;
- }
Android Bitmap Compress (image compression)