3 Kinds of image compression processing methods in Android _android

Source: Internet
Author: User

The existence of pictures in Android:

1: File form: Binary form exists with hard disk.
2: The form of the stream: binary form exists in memory.
The form of 3:bitmap

Three different forms of distinction:
File form and flow form: does not affect the size of the picture. In other words, if the image on your phone's SD card is read into memory in the form of streaming, the size in memory is the size of the original.
Note: It is not a bitmap form.
Bitmap: The memory of the picture will become larger.
The following is the form of the code:

 /** * Image Compression Method Summary *//* Image compression Method 01: Quality Compression Method * * Private Bitmap Compressimage (Bitmap beforbitmap) {//
    You can capture data from memory buffers and convert them into byte arrays.
    Bytearrayoutputstream BOS = new Bytearrayoutputstream (); if (Beforbitmap!= null) {//First parameter: format of picture compression; second parameter: compression ratio; third parameter: Compressed data stored in BOS beforbitmap.compress (Compressformat .
      JPEG, MB, BOS);
      int options = 100; Loop to determine whether the compressed picture is greater than 100kb, if greater than, continue to compress, otherwise do not compress while (Bos.tobytearray (). length/1024 >) {bos.reset ();
        Set to empty//compress options% beforbitmap.compress (compressformat.jpeg, Options, BOS);

      Reduce the options = 10 each time; ///Read the data from the BOS and store it in the bytearrayinputstream bytearrayinputstream bis = new Bytearrayinputstream (bos.t
      Obytearray ());
      Convert data to picture Bitmap Afterbitmap = Bitmapfactory.decodestream (bis);
    return afterbitmap;
  return null; * * * Picture compression method 02: Get thumbnail */public Bitmap getthumbnail (int id) {//Get original BitMap Beforebitmap = Bitmapfactory.decoderesource (Mcontext.getresources (), id);
    Wide int w = mcontext.getresources (). Getdimensionpixeloffset (R.dimen.image_w);

    High int h = mcontext.getresources (). Getdimensionpixelsize (R.dimen.image_h);
    Get thumbnail Bitmap afterbitmap = thumbnailutils. Extractthumbnail (Beforebitmap, W, h);

  return afterbitmap; 
   /** * Image Compression * * @param ID * The size of the picture to be manipulated * @param newwidth * Picture specified width * @param newheight * Picture Specified Height * @return/public Bitmap compressbitmap (int id, double newwidth, double newheight) {//obtained
    Get the original Bitmap Beforebitmap = Bitmapfactory.decoderesource (Mcontext.getresources (), id);
    The original width and height of the picture float beforewidth = Beforebitmap.getwidth ();

    float beforeheight = Beforebitmap.getheight ();
    Calculate the wide and high scaling rate float scalewidth = 0;
    float ScaleHeight = 0; if (Beforewidth > Beforeheight) {scalewidth = ((float) newwidTH)/beforewidth;
    ScaleHeight = ((float) newheight)/beforeheight;
      else {ScaleWidth = ((float) newwidth)/beforeheight;
    ScaleHeight = ((float) newheight)/beforewidth;
    //Matrix object matrices = new Matrix ();
    Zoom picture Action Scaling Matrix.postscale (ScaleWidth, ScaleHeight); Create a new Bitmap clip image from the original image Bitmap Afterbitmap = Bitmap.createbitmap (beforebitmap, 0, 0, (int) beforewidth, (int
    ) Beforeheight, Matrix, True);

  return afterbitmap;
 }

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.