“This is the fifth day of my participation in the First Challenge 2022. For details: First Challenge 2022”

How to compress an image

Image application scenarios are too many, how to deal with a good picture, ensure clarity while occupying a small space, become the primary pursuit of the goal, then how to compress a picture?

Image format

I don’t know if you have encountered such a scenario, using the compression tool to compress a 100M JPG file folder, the compression is about 95MB, while other files can be compressed more than 50M.

This is because JPG is a compressed format.

Images also have many coding formats, such as JPG, PNG, BMP and so on are our common image formats, so what is the difference between these image formats?

  • JPG is a lossy compression coding, it out of color space conversion, sampling rate, block processing technology, but also using Huffman coding. This is a compression algorithm because it converts the RGB color array to YUV and then reduces the UV sampling rate.

  • PNG is a lossless compression coding, increase the transparency of the channel, can be true to reduce the size of the image file, because it is lossless compression, and the use of transparency support, so the same file, than JPG memory

  • BMP stands for Bitmap and is uncompressed code, so it takes up a lot of memory.

Compression way

  • Image size

  • Image quality

  • Image coding

Fundamentally speaking, picture compression is always carried out around these three directions.

Due to the high resolution of the camera, although the quality of the picture has been improved, the original picture will take up a lot of space. If it is transmitted on the network or for simple preview, it is unnecessary, such as uploading the head picture and other scenes. So we compress the image size to make it fit the business scenario and reduce the size footprint.

Example of an algorithm for compressing images in Android:

Public static void compressBitmapToFile(Bitmap BMP, File File){// Int ratio = 2; Bitmap result = Bitmap.createBitmap(bmp.getWidth() / ratio, bmp.getHeight() / ratio, Config.ARGB_8888); Canvas canvas = new Canvas(result); Rect rect = new Rect(0, 0, bmp.getWidth() / ratio, bmp.getHeight() / ratio); canvas.drawBitmap(bmp, null, rect, null); ByteArrayOutputStream baos = new ByteArrayOutputStream(); result.compress(Bitmap.CompressFormat.JPEG, 100 ,baos); try { FileOutputStream fos = new FileOutputStream(file); fos.write(baos.toByteArray()); fos.flush(); fos.close(); } catch (Exception e) { e.printStackTrace(); }}Copy the code

Mass compression also has two directions, one is to reduce mass by compressing bit depth and transparency, and the other is to compress by sampling rate.

Examples of methods in Android

Bitmap bitmap; bitmap.compress(Bitmap.CompressFormat.JPEG, options, baos); // Quality compression method, adjust the value of options, where 100 means no compression, the compressed data stored in baOSCopy the code

options.inSampleSize = computeSize();

Copy the code

Image coding above, in addition to the common JPG, PNG, many large companies have begun to use webP format, it has better image data compression algorithm, so the volume is smaller.

Convert the image to webp format in AndroidStudio.

Right click on the image file, click Convert to WebP, and adjust the Settings.

How to load WebP? In Android, webP cannot be loaded like ordinary pictures. At present, the mainstream picture loading framework has supported WebP.