The choice of the Android bitmap compression method is detailed _android

Source: Internet
Author: User

Just to modify the bug ran into a problem, first describe the problem.

1. Test said to share the article to the micro-letter failure, QQ success.

Navigate to the micro-credit sharing interface.

2. Share other articles to micro-letter success.

There is a problem with the interface! I almost found the interface, okay?

3. Breakpoint micro-letter sharing, found a sudden compression failure.

Code writing problem, the following will decompose

4. Find the reason, micro-letter to share the size of the thumbnail 32k limit, the code is the first picture of the article compression, the picture is too large, compression code also has problems.

Start to solve the problem

Here are two ways to solve this problem:
1. The interface provides the sharing content of the article, which is limited when the editor edits the article.

{
  "title": "How to Begain Android?",
  "content": "Doing it, do it, doing it",
  "picture": "Yun_qi_img/share_thumb.jpg "
  ...
  ...
}

The picture field is a <32k thumbnail.

PS: In my current level, personally feel that this is the best, not to say Android easy, but for the future expansion and overall project expansion planning is relatively good, of course, the level is limited, perhaps I do not see far enough, I hope to advise.

2. Compress the captured article picture.

The current project cannot modify the interface (the camel is too big), so the image can only be compressed, which is the hole above. First look at the original written compression code:

----code that may be wrong----

int options = m; 

Bytearrayoutputstream outputs = new Bytearrayoutputstream (); 
Obitmap.compress (Bitmap.CompressFormat.JPEG, options, outputs); While

(Outputs.tobytearray (). length/1024 >) { 
  outputs.reset ();
  Obitmap.compress (Bitmap.CompressFormat.JPEG, options, outputs);
  options =;
}

Bytearrayinputstream inputs = new Bytearrayinputstream (Outputs.tobytearray ()); 
Bitmap Bitmap = bitmapfactory.decodestream (inputs, NULL, NULL);

Here using the bitmap compress () method for the so-called ' quality compression ', control parameters is the value of options, but the problem is the value of the options, when the picture is very large, even if the option is 0, the picture is greater than 32K, At this point option is negative in the while, and then the compress () operation throws the throw new IllegalArgumentException ("Quality must be 0..100");

Reason:

Using the Compress () method of the options value of the method is not to lose pixels, but only by modifying other images such as transparency and other attributes, so that the picture size changes, so it can not be unlimited compression, to a value will not continue to become smaller.

Solve:

The first time to think of the quality of compression, when the options are 0, or greater than 32K, the sample rate compression.

Bitmapfactory.options newopts = new Bitmapfactory.options ();
Newopts.insamplesize = 2;
Bitmap = Bitmapfactory.decodefile (PicturePath, newopts);

Each time the size of the bitmap is judged, the bitmapfactory.options insamplesize is +1 or 1 (not less than 1). But Insamplesize's value is int type, when the picture is very big, 1/2, 1/3, 1/4 of the gap is still somewhat big. So can only give up, finally adopted a scaling method, the first code:

public static byte[] Wechatbitmaptobytearray (Bitmap bmp, Boolean needrecycle) {//First a large range of compression bytearrayoutputstre
  AM output = new Bytearrayoutputstream ();
  Bmp.compress (Bitmap.CompressFormat.JPEG, Output); FLOAT zoom = (float) math.sqrt (* 1024/(float) output.tobytearray (). length);
  Get scaling//set matrix data matrices = new Matrix (); 

  Matrix.setscale (zoom, zoom); Creation of new BITMAP based on matrix data Bitmap Resultbitmap = bitmap.createbitmap (BMP, 0, 0, bmp.getwidth (), Bmp.getheight (), Matrix, True

  );

  Output.reset ();

  Resultbitmap.compress (Bitmap.CompressFormat.JPEG, Output); If the above compression is still greater than 32K, a small trimming compression while (Output.tobytearray (). length > * 1024) {Matrix.setscale (0.9f, 0.9f); Every time you shrink 1/10 resultbitmap = bitmap.createbitmap (resultbitmap, 0, 0, RESULTBITMAP.G

    Etwidth (), Resultbitmap.getheight (), matrix,true);
    Output.reset ();
  Resultbitmap.compress (Bitmap.CompressFormat.JPEG, Output); } return result;

 }

At this point, temporarily fill up the problem caused by the first nonstandard. Of course, the above explanations are from the book and My big Internet access method, I have some integration, if you also encounter this problem, also fortunate this article was you see, can help to one or two, is my intention.

Through this article hope to help the students in need, thank you for your support of this site!

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.