Everything you need to know about the Android image Bitmap class

Everything you need to know about the Android image Bitmap class

Bitmap Introduction

Bitmap is an image file format that consists of an array of pixels, each pixel has its own color information. In computer graphics, a Bitmap image can be described as a two-dimensional matrix, where each element represents the color value of a pixel.

The Bitmap in Android is a class used to represent images. It can be used to load, display, and process images. You can create an image object through the Bitmap class and then display it on the screen or process it further.

The following is an example of an ImageView displaying a Bitmap object:

 // 从资源文件中加载一张图片到Bitmap对象Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.image); // 将Bitmap对象显示在ImageView中ImageView imageView = (ImageView) findViewById(R.id.imageView); imageView.setImageBitmap(bitmap);

In addition to loading and displaying images, Bitmap also provides some methods to operate on images, such as scaling, cropping, rotating, etc. At the same time, Bitmap can also be used to process the pixel data of the image and perform pixel-level operations.

It should be noted that the Bitmap object occupies a large space in memory, so you need to pay attention to memory management during use to avoid memory overflow.

Bitmap color channels

In Android, the color channels of a Bitmap are usually stored in the order of ARGB (Alpha, Red, Green, Blue). Each channel usually occupies 8 bits, or one byte, with a value range of 0-255. This storage method is called 32-bit color depth (each pixel occupies 32 bits).

Specifically, ARGB is stored as follows:

  • Alpha channel: used to indicate the transparency of pixels, 0 means completely transparent, and 255 means completely opaque.
  • Red channel: represents the intensity of the red component.
  • Green channel: represents the intensity of the green component.
  • Blue channel: represents the intensity of the blue component.

In memory, Bitmap pixels are usually stored in rows, and each pixel occupies 4 bytes, corresponding to the four ARGB channels, that is, each pixel occupies 32 bits.

Calculation of memory size occupied by Bitmap

In Android, the memory size occupied by a Bitmap can be calculated by the following formula:

[Size (bytes) = width × height × bytes per pixel]

The number of bytes each pixel occupies depends on the Bitmap configuration. Common configurations include:

  • ARGB_8888: Each pixel occupies 4 bytes (one byte for the Alpha channel and 1 byte for each RGB channel)
  • RGB_565: Each pixel takes up 2 bytes (5 bits for red, 6 bits for green, and 5 bits for blue)

Therefore, if you have a Bitmap in ARGB_8888 format with a width of w and a height of h, the memory size it occupies is:

[ w \times h \times 4 ]

If it is a Bitmap in RGB_565 format, the memory size occupied is:

[ w \times h \times 2 ]

It should be noted that this is only the memory size occupied by the Bitmap itself. In fact, in Android, Bitmap will also occupy a certain amount of additional memory, such as Bitmap configuration information, pixel data, etc.

Bitmap usage

In Android, you can use the Bitmap class to process images. The Bitmap class can be used to load, display, save, and process images.

You can use the following methods to load a Bitmap:

  1. Load a Bitmap from a resource file:
 Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.image);
  1. Load a Bitmap from a file:
 Bitmap bitmap = BitmapFactory.decodeFile("path/to/file/image.jpg");
  1. Loading a Bitmap from an InputStream:
 InputStream inputStream = ...; // 从网络或其他来源获取输入流Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
  1. Loading a Bitmap from a byte array:
 byte[] byteArray = ...; // 从网络或其他来源获取byte数组Bitmap bitmap = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
  1. Loading Bitmap from Uri (for Android 10 and above):
 Uri uri = ...; // 从内容提供者或其他来源获取Uri Bitmap bitmap = ImageDecoder.decodeBitmap(ImageDecoder.createSource(getContentResolver(), uri));

Here are some common Bitmap usage methods:

  1. Loading an image:
 Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.image);
  1. Display image:
 imageView.setImageBitmap(bitmap);
  1. Resize the image:
 Bitmap resizedBitmap = Bitmap.createScaledBitmap(bitmap, newWidth, newHeight, true);
  1. Save the image:
 FileOutputStream out = new FileOutputStream(file); bitmap.compress(Bitmap.CompressFormat.PNG, 100, out); out.flush(); out.close();
  1. Image Processing:
 Canvas canvas = new Canvas(bitmap); Paint paint = new Paint(); // 在图像上绘制文本canvas.drawText("Hello, World!", x, y, paint);

Summarize

In Android development, Bitmap is a commonly used class used to represent image data. The following is a summary of Android Bitmap:

  1. Create Bitmap: You can obtain Bitmap objects from resources, files, streams, etc. through the static methods of the BitmapFactory class, or you can create blank Bitmap objects through the static methods of the Bitmap class.
  2. Memory management of Bitmap: Since Bitmap objects occupy a large amount of memory, you need to release Bitmap objects that are no longer needed in a timely manner to avoid memory overflow. You can call the recycle() method of Bitmap to release the memory occupied by the Bitmap object.
  3. Bitmap operations: You can scale, rotate, crop, and perform other operations on a Bitmap, or you can combine multiple Bitmaps into a new Bitmap.
  4. Bitmap display: You can display Bitmap on controls such as ImageView and Canvas, or save Bitmap as an image file.
  5. Bitmap pixel operations: You can directly operate the pixel data of the Bitmap, such as modifying the pixel color, obtaining the pixel value, etc.

Bitmap plays an important role in Android development. Developers need to understand Bitmap creation, memory management, operation, and display in order to effectively process image data.

<<:  Eight open source free web screenshot/recording tools

>>:  Things about Android application hardening

Recommend

2019: New trends in brand marketing planning and social media marketing!

1. Brand marketing planning will shape new trends...

Notes on Xiaohongshu’s “commercialization” of promoting products?

Lei Jun, who became a soul singer popular on Bili...

Brand Marketing: 8 Failure Experiences of New Brands!

I have been writing less recently, which has led ...

Baidu bidding promotion, master these 5 points

The competition in the Baidu bidding industry is ...

Tmall Double 11 event marketing tactics!

On Double 11 in 2019, Tmall’s final transaction v...

It is said online that constipation can cause colorectal cancer. Is this true?

When it comes to constipation, many people have t...