Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to compress and save the size and quality of pictures in Android development

2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains "Android development how to achieve picture size and quality compression and preservation", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn "Android development how to achieve picture size and quality compression and preservation" bar!

There are four attributes for pictures in Android

ALPHA_8: occupies 1byte memory per pixel

ARGB_4444: occupies 2byte memory per pixel

ARGB_8888: occupies 4byte memory per pixel (default)

RGB_565: occupies 2byte memory per pixel

The default color mode of Android is ARGB_8888, which has the finest color and the highest display quality. But again, it takes up the most memory. So use RGB_565 when the effect on the picture is not very high (565 has no transparency attribute)

Android commonly used picture format at present

There are png,jpeg and webp

Png: lossless compressed image format, which supports Alpha channel. Android cut materials often use this format.

Jpeg: lossy compressed image format, does not support transparent background, suitable for photos and other colorful (large image compression, not suitable for logo)

Webp: a picture format that provides both lossy compression and lossless compression, derived from the video coding format VP8. From Google's official website, lossless webp is 26% smaller than png on average, lossy webp is 25% smaller than jpeg on average, lossless webp supports Alpha channel, lossy webp supports the same under certain conditions, lossy webp supports after Android4.0 (API 14), lossless and transparent support after Android4.3 (API18)

Use size to compress private Bitmap getimage (String srcPath) {BitmapFactory.Options newOpts = new BitmapFactory.Options (); / / start reading the picture, and set options.inJustDecodeBounds back to true newOpts.inJustDecodeBounds = true; Bitmap bitmap = BitmapFactory.decodeFile (srcPath,newOpts); / / return bm as empty newOpts.inJustDecodeBounds = false; int w = newOpts.outWidth; int h = newOpts.outHeight / / now most of the mainstream phones have a resolution of 800cm 480, so we set the height and width to float hh = 800f / width / 800f / float ww = 480f / 480f / zoom. Because it is a fixed scale scale, only one of the height or width data can be used to calculate int be=1; / / be=1 means not scaling if (w > h & w > ww) {/ / if the width is large, scale be= (int) (newOpts.outWidth / ww) according to the width;} else if (w)

< h && h >

Hh) {/ / if the height is high, scale be = (int) (newOpts.outHeight / hh) according to the width;} if (be 5000) {/ / reset the baos to empty the baos baos.reset () / / quality compression method, where 100 means no compression, and the compressed data is stored in baos image.compress (Bitmap.CompressFormat.JPEG, 10, baos);} else if (length > 4000) {baos.reset (); image.compress (Bitmap.CompressFormat.JPEG, 20, baos);} else if (length > 3000) {baos.reset () Image.compress (Bitmap.CompressFormat.JPEG, 50, baos);} else if (length > 2000) {baos.reset (); image.compress (Bitmap.CompressFormat.JPEG, 70, baos) } / / cycle to determine if the compressed image is larger than 1m, which is greater than continuing to compress while (baos.toByteArray (). Length / 1024 > 1024) {/ / reset baos to clear baos baos.reset (); / / here compress options%, to store compressed data in baos image.compress (Bitmap.CompressFormat.JPEG, options, baos) / / reduce 10 options-= 10 each time;} / / store the compressed data baos in ByteArrayInputStream ByteArrayInputStream isBm = new ByteArrayInputStream (baos.toByteArray ()); / / generate ByteArrayInputStream data into pictures Bitmap bitmap = BitmapFactory.decodeStream (isBm, null, null); return bitmap } mixed compression private Bitmap comp (Bitmap image) {ByteArrayOutputStream baos = new ByteArrayOutputStream (); image.compress (Bitmap.CompressFormat.JPEG, 100, baos); if (baos.toByteArray (). Length / 1024 > 1024) {/ / judge that if the picture is larger than 1m, compress to avoid overflow baos.reset () when generating the picture (BitmapFactory.decodeStream) / / reset baos to empty baos image.compress (Bitmap.CompressFormat.JPEG, 50, baos); / / compress 50% here, store the compressed data in baos} ByteArrayInputStream isBm = new ByteArrayInputStream (baos.toByteArray ()); BitmapFactory.Options newOpts = new BitmapFactory.Options (); / / start reading into the picture, and then set options.inJustDecodeBounds back to true newOpts.inJustDecodeBounds = true; Bitmap bitmap = BitmapFactory.decodeStream (isBm, null, newOpts) NewOpts.inJustDecodeBounds = false; int w = newOpts.outWidth; int h = newOpts.outHeight; / / most of the mainstream phones now have a resolution of 800F / 480, so we set the height and width to float hh = 800f / 480f / 480f / / the width is set to 480f / /. Because it is a fixed scale scale, only one of the height or width data can be used to calculate int be=1; / / be=1 means not scaling if (w > h & w > ww) {/ / if the width is large, scale be= (int) (newOpts.outWidth / ww) according to the width;} else if (w)

< h && h >

Hh) {/ / if the height is high, scale be = (int) (newOpts.outHeight / hh) according to the width fixed size;} if (be)

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 206

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report