Bitmap类
Bitmap.compress();
Write a compressed version of the bitmap to the specified outputstream. If this returns true , the bitmap can be
reconstructed by passing a corresponding inputstream to BitmapFactory . decodeStream ( ) . Note : not all Formats support all
bitmap configs directly, so it is possible that the returned bitmap from BitmapFactory could be in a different bitdepth,
and/ or may have lost per- pixel alpha ( e. g. JPEG only supports opaque pixels) .
Params :
format – The format of the compressed image
quality – Hint to the compressor, 0 - 100. The value is interpreted differently depending on the Bitmap. CompressFormat .
stream – The outputstream to write the compressed data.
Returns:
true if successfully compressed to the specified stream.
public boolean compress ( CompressFormat format, int quality, OutputStream stream) {
}
图片压缩有三种解决方案
质量压缩
public static void qualityCompress ( Context context) {
try {
open = context. getAssets ( ) . open ( "pic1.jpg" ) ;
} catch ( IOException e) {
e. printStackTrace ( ) ;
}
Bitmap bitmap = BitmapFactory . decodeStream ( open ) ;
int quality = 50 ;
try {
fos = new FileOutputStream ( new File ( context. getFilesDir ( ) , String . valueOf ( System . currentTimeMillis ( ) ) + "_quality.jpeg" ) ) ;
bitmap. compress ( Bitmap. CompressFormat . JPEG, quality, fos) ;
} catch ( FileNotFoundException e) {
e. printStackTrace ( ) ;
} finally {
try {
fos. close ( ) ;
} catch ( IOException e) {
e. printStackTrace ( ) ;
}
}
}
压缩到100K
public static void qualityCompressTo100K ( Context context) {
try {
open = context. getAssets ( ) . open ( "pic1.jpg" ) ;
} catch ( IOException e) {
e. printStackTrace ( ) ;
}
Bitmap bitmap = BitmapFactory . decodeStream ( open ) ;
ByteArrayOutputStream bos = new ByteArrayOutputStream ( ) ;
int quality = 60 ;
try {
fos = new FileOutputStream ( new File ( context. getFilesDir ( ) , String . valueOf ( System . currentTimeMillis ( ) ) + "_quality.jpeg" ) ) ;
Log . i ( TAG, "starting compression" ) ;
do {
bos. reset ( ) ;
quality = quality - 5 ;
bitmap. compress ( Bitmap. CompressFormat . JPEG, quality, bos) ;
Log . i ( TAG, "bos.toByteArray().length:" + bos. toByteArray ( ) . length) ;
} while ( ( bos. toByteArray ( ) . length > 1024 * 100 && quality > 5 ) ) ;
fos. write ( bos. toByteArray ( ) ) ;
} catch ( Exception e) {
e. printStackTrace ( ) ;
} finally {
try {
fos. close ( ) ;
} catch ( IOException e) {
e. printStackTrace ( ) ;
}
}
}
输出结果
2023 - 04 - 11 14 : 11 : 34.770 18135 - 18421 / cn. jj. myapplication I / JJWorld. CompressUtils : starting compression
2023 - 04 - 11 14 : 11 : 34.820 18135 - 18421 / cn. jj. myapplication I / JJWorld. CompressUtils : bos. toByteArray ( ) . length: 346061
2023 - 04 - 11 14 : 11 : 34.857 18135 - 18421 / cn. jj. myapplication I / JJWorld. CompressUtils : bos. toByteArray ( ) . length: 328705
2023 - 04 - 11 14 : 11 : 34.895 18135 - 18421 / cn. jj. myapplication I / JJWorld. CompressUtils : bos. toByteArray ( ) . length: 311990
2023 - 04 - 11 14 : 11 : 34.932 18135 - 18421 / cn. jj. myapplication I / JJWorld. CompressUtils : bos. toByteArray ( ) . length: 292827
2023 - 04 - 11 14 : 11 : 34.968 18135 - 18421 / cn. jj. myapplication I / JJWorld. CompressUtils : bos. toByteArray ( ) . length: 274492
2023 - 04 - 11 14 : 11 : 35.004 18135 - 18421 / cn. jj. myapplication I / JJWorld. CompressUtils : bos. toByteArray ( ) . length: 252523
2023 - 04 - 11 14 : 11 : 35.039 18135 - 18421 / cn. jj. myapplication I / JJWorld. CompressUtils : bos. toByteArray ( ) . length: 227915
2023 - 04 - 11 14 : 11 : 35.074 18135 - 18421 / cn. jj. myapplication I / JJWorld. CompressUtils : bos. toByteArray ( ) . length: 201450
2023 - 04 - 11 14 : 11 : 35.109 18135 - 18421 / cn. jj. myapplication I / JJWorld. CompressUtils : bos. toByteArray ( ) . length: 170260
2023 - 04 - 11 14 : 11 : 35.143 18135 - 18421 / cn. jj. myapplication I / JJWorld. CompressUtils : bos. toByteArray ( ) . length: 134575
2023 - 04 - 11 14 : 11 : 35.179 18135 - 18421 / cn. jj. myapplication I / JJWorld. CompressUtils : bos. toByteArray ( ) . length: 91796
尺寸压缩
Native压缩