Basic use of BitMap

Basic use of BitMap

How many ways can we create a BItMap?

Answer: There are two types: one is to use the BitmapFactory class to load, and the other is to use the Bitmap class to load.

一、BitmapFactory

/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBASk1XMTQwNw==,size_20,color_FFFFFF,t_70,g_se,x_16.png)]

1.1 BitmapFactory provides a variety of static methods for creating bitmaps

//从资源文件中通过id加载bitmap
//Resources res:资源文件,可以context.getResources()获得
//id:资源文件的id,如R.drawable.xxx
public static Bitmap decodeResources(Resources res,int id)
//第二种只是第一种的重载方法,多了个Options参数
public static Bitmap decodeResources(Resources res,int id,Options opt)

//传入文件路径加载,比如加载sd卡中的文件
//pathName:文件的全路径名
public static Bitmap decodeFile(String pathName);
public static Bitmap decodeFile(String pathName,Options opt);

//从byte数组中加载
//offset:对应data数组的起始下标
//length:截取的data数组的长度
public static Bitmap decodeByteArray(byte[] data,int offset , int length);
public static Bitmap decodeByteArray(byte[] data,int offset , int length,Options opt);

//FileDescriptor :包含解码位图的数据文件的路径
//通过该方式从路径加载bitmap比decodeFile更节省内存,原因不解释了。
public static Bitmap decodeFileDescriptor(FileDescriptor fd);
public static Bitmap decodeFileDescriptor(FileDescriptor fd,Rect outPadding,Options opt);

decodeFile, decodeResource, decodeStream, and decodeByteArray are used to support loading a Bitmap object from the file system, resource, input stream, and byte array, respectively, where decodeFile and decodeResource indirectly call the decodeStream method. These four types of methods are finally in Android. The bottom layer implementation corresponds to several native methods of the BitmapFactory class.

A native method is an interface for Java to call non-Java code. A native method means that the implementation of the method is implemented in a non-Java language, such as C or C++.

1.2 Bitmap.Options

There are many ways to create one Bitmap, and many of them require one to be passed in Bitmap.Options. What is it and what does it do?

This parameter plays a very important role. It can set the sampling rate of the Bitmap . By changing the width, height and zoom ratio of the picture, the purpose of reducing the number of picture pixels is achieved . In a word, by setting this parameter, we can do a good job Control display and usage Bitmap. In the actual development process, this value can be set flexibly to reduce OOMthe probability of occurrence.

  1. How to load bitmap efficiently?

By BitmapFactory.Optionspressing a certain number 采样率to load the reduced image, the reduced image will ImageViewbe displayed in , which will reduce the memory usage and avoid OOM to a certain extent, and improve the performance of Bitmap loading. The four methods of loading images provided by BitmapFactory all support the BitmapFactory.Options parameter

  1. Scale the image through BitmapFactory.Options, mainly using the inSampleSize parameter, that is, the sampling rate.

When inSampleSize is 1, the size of the sampled picture is the original size of the picture;
when inSampleSize is greater than 1, such as 2, then the width/height of the sampled picture is 1/2 of the size of the original picture, and the number of pixels is 1/4 of the original image, the memory size it occupies is also 1/4 of the original image.

Take a picture with 1024×1024 pixels as an example, assuming it is stored in ARGB8888 format, then the memory it occupies is 1024×1024×4 or 4MB. If the inSampleSize is 2, the memory occupation of the sampled picture is only 512×512× 4, which is 1MB.

The sampling rate acts on the width/height at the same time, which will cause the size of the scaled image to decrease in the form of the 2 power of the sampling rate, that is, the scaling ratio is 1/(inSampleSize to the 2 power)

​ For example, if inSampleSize is 4, then the zoom ratio is 1/16.
​ There is a special case, that is, when inSampleSize is less than 1, its effect is equivalent to 1, that is, there is no scaling effect.

Parameter Description:

[External link picture transfer failed, the source site may have an anti-theft link mechanism, it is recommended to save the picture and upload it directly (img-fW5BDNEx-1685879059957) (common method in Bitmap/watermark, type_d3F5LXplbmhlaQ, shadow_50, text_Q1NETiBASk1XMTQwNw==, size_20, color_FFFFFF ,t_70,g_se,x_16-16841591976211.png)]

for example:

BitmapFactory.Options options = new BitmapFactory.Options();
// 设置inJustDecodeBounds为true后,再使用decodeFile()等方法,并不会真正的分配空间,
//即解码出来的Bitmap为null,但是可计算出原始图片的宽度和高度,即options.outWidth和options.outHeight
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(getResources(), R.drawable.img3, options);
float  srcWidth = options.outWidth;
float  srcHeight = options.outHeight;
int inSampleSize = 1;
float  height = 100;
float  width = 528;

if (srcHeight > height || srcWidth > width) {
    
    
    if (srcWidth > srcHeight) {
    
    
        inSampleSize = Math.round(srcHeight / height);
    } else {
    
    
        inSampleSize = Math.round(srcWidth / width);
    }
}

options.inJustDecodeBounds = false;
options.inSampleSize = inSampleSize;

Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.img3, options);
float  outWidth = options.outWidth;
float  outHeight = options.outHeight;
binding.image.setImageBitmap(bitmap);


Log.d(TAG, "图片的内存: " + bitmap.getAllocationByteCount());
Log.d(TAG, "图片的采樣率: " + inSampleSize);
Log.d(TAG, "原來图片的宽度/高度: " + srcWidth + " / " + srcHeight);
Log.d(TAG, "現在图片的宽度/高度: " + outWidth + " / " + outHeight);

[External link picture transfer failed, the source site may have an anti-theft link mechanism, it is recommended to save the picture and upload it directly (img-vwaFg0xv-1685879059957) (common method in Bitmap/image-20230515220322103.png)]

2. Bitmap static method

//width和height是长和宽单位px,config是存储格式
static Bitmap createBitmap(int width , int height Bitmap.Config config)
// 根据一幅图像创建一份一模一样的实例
static Bitmap createBitmap(Bitmap bm)
//截取一幅bitmap,起点是(x,y),width和height分别对应宽高
static Bitmap createBitmap(Bitmap bm,int x,int y,int width,int height)
//比上面的裁剪函数多了两个参数,Matrix:给裁剪后的图像添加矩阵 boolean filter:是否给图像添加滤波效果
static Bitmap createBitmap(Bitmap bm,int x,int y,int width,int height,Matrix m,boolean filter);
//用于缩放bitmap,dstWidth和dstHeight分别是目标宽高
createScaledBitmap(Bitmap bm,int dstWidth,int dstHeight,boolean filter)

[External link picture transfer failed, the source site may have an anti-theft link mechanism, it is recommended to save the picture and upload it directly (img-C0cIT21O-1685879059958) (common method in Bitmap/image-20230515220522631.png)]

These methods can be roughly divided into three categories:

  • 1. Create a new Bitmap based on the existing Bitmap
/**
* 通过矩阵的方式,返回原始 Bitmap 中的一个不可变子集。新 Bitmap 可能返回的就是原始的 Bitmap,也可能还是复制出来的。
* 新 Bitmap 与原始 Bitmap 具有相同的密度(density)和颜色空间;
*
* @param source   原始 Bitmap
* @param x        在原始 Bitmap 中 x方向的其起始坐标(你可能只需要原始 Bitmap x方向上的一部分)
* @param y        在原始 Bitmap 中 y方向的其起始坐标(你可能只需要原始 Bitmap y方向上的一部分)
* @param width    需要返回 Bitmap 的宽度(px)(如果超过原始Bitmap宽度会报错)
* @param height   需要返回 Bitmap 的高度(px)(如果超过原始Bitmap高度会报错)
* @param m        Matrix类型,表示需要做的变换操作
* @param filter   是否需要过滤,只有 matrix 变换不只有平移操作才有效
*/
public static Bitmap createBitmap(@NonNull Bitmap source, int x, int y, int width, int height,
            @Nullable Matrix m, boolean filter) 


  • 2. Create an empty Bitmap through the pixel array
/**
     * 
     * 返回具有指定宽度和高度的不可变位图,每个像素值设置为colors数组中的对应值。
     * 其初始密度由给定的确定DisplayMetrics。新创建的位图位于sRGB 颜色空间中。
     * @param display  显示将显示此位图的显示的度量标准
     * @param colors   用于初始化像素的sRGB数组
     * @param offset   颜色数组中第一个颜色之前要跳过的值的数量
     * @param stride   行之间数组中的颜色数(必须> = width或<= -width)
     * @param width    位图的宽度
     * @param height   位图的高度
     * @param config   要创建的位图配置。如果配置不支持每像素alpha(例如RGB_565),
     * 那么colors []中的alpha字节将被忽略(假设为FF)
     */
    public static Bitmap createBitmap(@NonNull DisplayMetrics display,
            @NonNull @ColorInt int[] colors, int offset, int stride,
            int width, int height, @NonNull Config config) 


  • 3. Create a scaled Bitmap
/**
* 对Bitmap进行缩放,缩放成宽 dstWidth、高 dstHeight 的新Bitmap
*/
public static Bitmap createScaledBitmap(@NonNull Bitmap src, int dstWidth, int dstHeight,boolean filter)

3. Summary of creating Bitmap

  1. To load images, you can use BitmapFactory and Bitmap.create series methods
  2. Options can be used to zoom pictures, get picture information, configure zoom ratio and other functions
  3. If you need to crop or zoom the picture, you can only use the create series of functions
  4. Pay attention to loading and creating bitmaps to catch OOM exceptions through try catch

Guess you like

Origin blog.csdn.net/qq_43867812/article/details/131036153