Re-learn picture compression

Source: Internet
Author: User

One. The existence form of the picture

1. File (binary exists on hard disk)

2. The form of a stream (binary exists in memory)

3. Bitmap form

Different: The format of the file and the form of the stream does not affect the size of the picture, that is, if your phone SD card if it is 100K, then through the form of stream read into memory, also must be accounted for 100K of memory, note is the form of flow.

When the picture hand bitmap form exists, occupy the memory will become much larger, I tried 500K file form of the image loaded into memory, in the form of bitmap, occupy memory will be nearly 10M, of course, this increase in multiples is not fixed.

three ways to detect the size of a picture:File form: file.length () stream form: Speak picture file read into memory input stream, see its byte number Bitmap:bitmap.getByteCount () common methods of compression1. Compress the picture when it is saved locally, that is, compress the picture from bitmap form to file form. Features: The file form of the image is compressed, but when you re-read the compressed file as bitmap, the memory it occupies does not change.
public static void Compressbmptofile (Bitmap bmp,file File) {Bytearrayoutputstream BAOs = new Bytearrayoutputstream (); int options = 80;//personal like starting from 80, bmp.compress (Bitmap.CompressFormat.JPEG, Options, BAOs), while (Baos.tobytearray (). Length/ >) {baos.reset (); Options-= 10;bmp.compress (Bitmap.CompressFormat.JPEG, Options, BAOs);} try {fileoutputstream fos = new FileOutputStream (file); Fos.write (Baos.tobytearray ()); Fos.flush (); Fos.close ();} catch (Exception e) {E.printstacktrace ();}}

Method Description:

This method is to compress the quality of the picture, but will not reduce the image of the pixels, such as your image is 300k, 1280*700 pixels, after this method of compression, file form of the picture is 100, to facilitate uploading to the server, but when Bitmapfactory.decodefile () into memory, when it becomes bitmap, his pixels are still 1280*700, and the image pixels are calculated by Bitmap.getwidth and bitmap.getheight (),

A picture is made up of pixels, and what does each pixel contain? People familiar with PS know that the picture is composed of hue, lightness and saturation. The official document of the method also explains that it will let the picture be reconstructed, but it is possible that the image's bit depth (i.e. color depth) and the transparency of each pixel will change, JPEG onlysupports opaque (opaque), that is, after compressing in JPEG format, The transparent elements in the original picture will disappear. So this format is likely to cause distortion since it is changing the image display quality, to the file form of the image compression, the image of the pixel has not changed, then re-read the compressed file is bitmap, it consumes less memory. (Do not believe can try) because: Bitmap.getbytecount () is the memory used to calculate its pixels, see official explanation: Returns the number of bytes used to store this bitmap ' s pixel S. 2. When the picture is read from the local to memory compression, that is, the picture from the file form into the bitmap form feature: By setting the sampling rate, reduce the pixels of the picture, to the memory of bitmap compression first look at a method: The method is the memory of the bitmap quality compression, by the The above theory can be concluded that the method is invalid, and it is not necessary, because you have read it into memory, and then compress superfluous, although in the acquisition System album pictures, some phones will directly return a bitmap, but in this case, the returned bitmap are compressed, It is not possible to directly return a picture of the bitmap form of an acoustic, the consequences can be imagined

Private Bitmap compressbmpfrombmp (Bitmap image) {Bytearrayoutputstream BAOs = new Bytearrayoutputstream (); int options = 1 00;image.compress (Bitmap.CompressFormat.JPEG, BAOs), while (Baos.tobytearray (). length/1024 >) {baos.reset (); Options-= 10;image.compress (Bitmap.CompressFormat.JPEG, Options, BAOs);} Bytearrayinputstream ISBM = new Bytearrayinputstream (Baos.tobytearray ()); Bitmap Bitmap = Bitmapfactory.decodestream (ISBM, NULL, NULL); return Bitmap;}

The following methods are the same as before

Re-learn picture compression

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.