Three image compression methods in Android

Source: Internet
Author: User

Three image compression methods in Android

This article mainly introduces three image compression processing methods in Android. This article describes three methods: quality compression, thumbnails, and image scaling, and provides sample code. For more information, see

The format of images in Android:

1: File Format: the binary format exists in the hard disk.

2: stream format: the binary format exists in memory.

3: Bitmap format

There are three differences:

File Format and stream format: it does not affect the image size. That is to say, if the image on your mobile phone's SD card is read to the memory in the form of a stream, the size in the memory is also the size of the source image.

Note: It is not in the form of Bitmap.

Bitmap format: the memory occupied by the image increases instantly.

The following code is used:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

/**

* Summary of image compression methods

*/

 

/*

* Image compression method 01: quality compression method

*/

Private Bitmap compressImage (Bitmap beforBitmap ){

 

// Capture the data in the memory buffer and convert it into a byte array.

ByteArrayOutputStream bos = new ByteArrayOutputStream ();

If (beforBitmap! = Null ){

// The first parameter is the image compression format, the second parameter is the compression ratio, and the third parameter is the compressed data stored in bos.

BeforBitmap. compress (CompressFormat. JPEG, 100, bos );

Int options = 100;

// Cyclically determine whether the compressed image is larger than kb. If the size is greater than kb, continue to compress the image; otherwise, the image is not compressed.

While (bos. toByteArray (). length/1024> 100 ){

Bos. reset (); // set to null

// Compress options %

BeforBitmap. compress (CompressFormat. JPEG, options, bos );

// Decrease by 10 each time

Options-= 10;

 

}

// Read data from bos and store it in ByteArrayInputStream

ByteArrayInputStream bis = new ByteArrayInputStream (

Bos. toByteArray ());

// Convert data into images

Bitmap afterBitmap = BitmapFactory. decodeStream (bis );

Return afterBitmap;

}

Return null;

}

 

/*

* Image compression method 02: Obtain the thumbnail

*/

Public Bitmap getThumbnail (int id ){

// Obtain the source Image

Bitmap beforeBitmap = BitmapFactory. decodeResource (

MContext. getResources (), id );

// Width

Int w = mContext. getResources ()

. GetDimensionPixelOffset (R. dimen. image_w );

// High

Int h = mContext. getResources (). getDimensionPixelSize (R. dimen. image_h );

 

// Obtain the thumbnail

Bitmap afterBitmap = ThumbnailUtils

. ExtractThumbnail (beforeBitmap, w, h );

Return afterBitmap;

 

}

 

/**

* Image Compression 03

*

* @ Param id

* Size of the image to be operated

* @ Param newWidth

* The specified width of the image.

* @ Param newHeight

* Image Height

* @ Return

*/

Public Bitmap compressBitmap (int id, double newWidth, double newHeight ){

// Obtain the source Image

Bitmap beforeBitmap = BitmapFactory. decodeResource (

MContext. getResources (), id );

// Original width and height of the image

Float beforeWidth = beforeBitmap. getWidth ();

Float beforeHeight = beforeBitmap. getHeight ();

 

// Calculate the width-to-height 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

Matrix matrix = new Matrix ();

// Scale the image.

Matrix. postScale (scaleWidth, scaleHeight );

// Create a new Bitmap to cut the image from the original image

Bitmap afterBitmap = Bitmap. createBitmap (beforeBitmap, 0, 0,

(Int) beforeWidth, (int) beforeHeight, matrix, true );

Return afterBitmap;

 

}

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.