美文网首页
11.2 Bitmap

11.2 Bitmap

作者: jadefly | 来源:发表于2018-08-24 17:52 被阅读0次

简介

用于图像处理,可以获取图像文件信息,进行图像剪切、旋转、缩放等操作,并可以指定格式保存图像文件。


BitmapFactory

Bitmap构造是私有的,需要通过工厂类来实例化。

从文件读取图片:public static Bitmap decodeFile(String pathName)

                             public static Bitmap decodeFile(String pathName, Options opts)

        eg:Bitmap bitmap = BitmapFactory.decodeFile("/sdcard/bitmap.png");

从输入流读取图片:不会对所加载的图片进行缩放,相当从resourse占用内存小,效率高

                                public static Bitmap decodeStream(InputStream is)

                                public static Bitmap decodeStream(InputStream is, Rect outPadding, Options opts)

        eg:InputStream is = mContext.getAssets().open("bitmap.png"); //本地或网络

                bitmap = BitmapFactory.decodeStream(is);

从资源文件读取图片:加载的图片可能会经过缩放,消耗内存,大量使用容易OOM。(可将resources转成流,使用流读取)

                                    适用场景:性能要求不高,需要图片自适应情况

                                    public static Bitmap decodeResource(Resources res, int id)

                                    public static Bitmap decodeResource(Resources res, int id, Options opts)

        eg:Bitmap bitmap = BitmapFactory.decodeResource(mContext.getResources(),R.raw.bitmap);

从数组读取图片:public static Bitmap decodeByteArray(byte[] data, int offset, int length)

                             public static Bitmap decodeByteArray(byte[] data, int offset, int length, Options opts)

从文件读取文件 :与decodeFile不同的是这个直接调用JNI函数进行读取,效率比较高。

                              public static Bitmap decodeFileDescriptor(FileDescriptor fd)

                              public static Bitmap decodeFileDescriptor(FileDescriptor fd, Rect outPadding, Options opts)

注:除了使用BitmapFactory从资源获取位图,还可以使用BitmapDrawable对象获取位图

        ( new BitmapDrawable(XXXX).getBitmap )

Option参数

boolean inJustDecodeBounds:设置为true,不获取图片,不分配内存,只返回图片的高度宽度信息

                                                    (适用于只想知道bitmap尺寸,不想将其加载到内存)

int inSampleSize:图片缩放的倍数

                采样率:1.一般是2的指数1、2、4、8、16....;

                               2.设置小于1会被当做1处理,1为原始大小;

                               3.若采样率为2,宽和高为原先1/2,像素和占用内存为原来1/4;

int outWidth:获取图片的宽度值

int outHeight:获取图片的高度值

int inDensity:用于位图的像素压缩比

int inTargetDensity:用于目标位图的像素压缩比(要生成的位图)

byte[] inTempStorage:创建临时文件,将图片存储

boolean inScaled:设置为true时进行图片压缩,从inDensity到inTargetDensity

boolean inDither:如果为true,解码器尝试抖动解码(采用随机噪声色来填充,优化低位图色带明显的问题)

Bitmap.Config inPreferredConfig:设置解码器

String outMimeType:设置解码图像

boolean inPurgeable:当存储Pixel的内存空间在系统内存不足时是否可以被回收

boolean inInputShareable:inPurgeable为true情况下才生效,是否可以共享一个InputStream

boolean inPreferQualityOverSpeed:为true则优先保证Bitmap质量其次是解码速度

boolean inMutable:配置Bitmap是否可以更改,比如:在Bitmap上隔几个像素加一条线段

inScreenDensity:当前屏幕的像素密度

Bitmap.Config inPreferredConfig

Bitmap.Config ARGB_4444:代表 16位 ARGB位图 ,每个像素占用 2byte 内存(glide默认使用这个)

Bitmap.Config ARGB_8888:代表 32位 ARGB位图,每个像素占用 4byte 内存(想提升质量可使用这个)

Bitmap.Config ALPHA_8:代表 8位 Alpha位图,每个像素占用 1byte 内存(只有透明度没有颜色值)()

Bitmap.Config RGB_565:代表 8位 RGB位图,每个像素占用 2byte 内存(没有透明色,会导致每张图都会有一个底)

注:1.位图位数越高代表其可以存储的颜色信息越多,图像越逼真,占用内存越大;

       2.Android中一张图片(BitMap)占用的内存 = 图片长度 * 图片宽度 * 单位像素占用的字节数;


常用方法

void recycle() :回收位图占用的内存空间,把位图标记为Dead

boolean isRecycled():判断位图内存是否已释放

int getWidth():获取位图的宽度

int getHeight():获取位图的高度

boolean isMutable():图片是否可修改

int getScaledWidth(Canvas canvas):获取指定密度转换后的图像的宽度

int getScaledHeight(Canvas canvas):获取指定密度转换后的图像的高度

boolean compress(format,quality, stream):按指定的图片格式以及画质,将图片转换为输出流

                               format:压缩图像的格式,如Bitmap.CompressFormat.PNG或Bitmap.CompressFormat.JPEG

                               quality:画质0-100,0表示最低画质压缩,100以最高画质压缩(对于PNG等无损格式的图片,会忽略此项设置)

                               stream: OutputStream中写入压缩数据

                               return: 是否成功压缩到指定的流

static Bitmap createBitmap(Bitmap src):以src为原图生成不可变得新图像

static Bitmap createScaledBitmap(...):以src为原图,指定高、宽、是否可变,创建新的图像

static Bitmap createBitmap(int width, int height, Config config):创建指定格式、大小的位图

static Bitmap createBitmap(...) //以source为原图,指定起始坐标xy、宽、高,创建新的图像


Bitmap与其他格式之间转换

转换

使用

使用

相关文章

  • 11.2 Bitmap

    简介 用于图像处理,可以获取图像文件信息,进行图像剪切、旋转、缩放等操作,并可以指定格式保存图像文件。 Bitma...

  • 11.2-1 Bitmap2 OOM

    简介 Out Of Memory(内存溢出):占用内存超出系统分配内存; memory leak(内存泄漏):占用...

  • Bitmap方法总汇

    Bitmap缩放 Bitmap裁剪 Bitmap旋转

  • Android中drawable转换为bitmap

    Bitmap转换为Drawable Drawable转换为bitmap 从资源文件中获取Bitmap Bitmap...

  • Bitmap-详解

    参考资料 目录 Bitmap BitmapFactory Bitmap加载方法 Bitmap | Drawable...

  • Bitmap详解

    Bitmap的分析与使用 Bitmap的创建创建Bitmap的时候,Java不提供new Bitmap()的形式去...

  • Bitmap的分析与使用

    Bitmap的分析与使用 Bitmap的创建创建Bitmap的时候,Java不提供new Bitmap()的形式去...

  • 2019-11-05

    Bitmap Bitmap 细说Bitmap bitmap的六种压缩方式,Android图片压缩 1.先讲讲屏幕密...

  • 统计反馈(以前叫 Cardinality Feedback)

    Cardinality Feedback Cardinality Feedback基数反馈是版本11.2(11.2...

  • Android Mat 转 Bitmap

    在安卓中将Mat转为Bitmap代码如下: Bitmap bmpCanny = Bitmap.createBitm...

网友评论

      本文标题:11.2 Bitmap

      本文链接:https://www.haomeiwen.com/subject/gcnyiftx.html