Bitmap相关操作

Bitmap基础相关操作
一、获取 Bitmap 对象
  1. 使用 BitmapFactory 静态方法:
方法名作用
public static Bitmap decodeFile(String pathName, Options opts)将该文件路径的文件转化为位图,opts 为想要进行的操作,如压缩等
public static Bitmap decodeFile(String pathName)将该文件路径的文件转化为位图
public static Bitmap decodeResourceStream(@Nullable Resources res, @Nullable TypedValue value,@Nullable InputStream is, @Nullable Rect pad, @Nullable Options opts)将资源中的文件转化为位图,opts 表示进行的操作
public static Bitmap decodeResource(Resources res, int id, Options opts)将资源中的 id 文件转化为位图
public static Bitmap decodeResource(Resources res, int id, Options opts)将资源中的 id 文件转化为位图,opts 为转化做的操作
public static Bitmap decodeByteArray(byte[] data, int offset, int length, Options opts)将 byte[] 数组转化为位图,offset 为开始位置,length 总共转化的 byte 长度,opts 为转化所做的操作,有时得到的图片数据是 byte[] 数组
public static Bitmap decodeByteArray(byte[] data, int offset, int length)将 byte[] 数组转化为位图,不进行操作
public static Bitmap decodeStream(@Nullable InputStream is, @Nullable Rect outPadding,@Nullable Options opts)将输入流转化为位图。outPadding 为位图的填充矩形,可为空;,opts 为转化做的操作,一般为图片大小适配与压缩
public static Bitmap decodeStream(InputStream is)将输入流转化为位图,没有任何操作
public static Bitmap decodeFileDescriptor(FileDescriptor fd, Rect outPadding, Options opts)从文件描述符解码位图。outPadding 位图填充矩形,可为空;opts 指定解码操作
public static Bitmap decodeFileDescriptor(FileDescriptor fd)从文件描述符解码位图。不进行操作和矩形填充
  1. 使用 Bitmap 静态方法获取位图
方法名作用
public static Bitmap createBitmap(@NonNull Bitmap source, int x, int y, int width, int height)从 source 源位图,起点 x,y 坐标,截取宽 width,高 height 的一张图片,超过源图片大小会报异常
Bitmap 静态截取图片方法较多,具体查看官网或源码即可方法较多,不一一列举
二、图片操作
  1. 截图
    Bitmap.createBitmap() 方法截图图片的部分。

  2. 压缩
    获取图片时使用 Options 对象压缩,options.inSampleSize 参数就是宽高都压缩为原来的 1 / options.inSampleSize 。

  3. 缩放

        int screenWidth = 1080;
        int screenHeight = 1920;
        //直接设置即可
        bitmap.setHeight(screenHeight);
        bitmap.setWidth(screenWidth);
三、常用操作
  1. 从网络获取的图片,response 可以转化为 byte[] 数组或 InputStream 输入流。这时就可以使用工厂方法 decodeStream() 或 decodeBytes() 来转化显示,内存有限,屏幕也需适配,所以一般需要 Options 对象来转化。
//不一定是这样,具体看 HTTP 框架,但一定有转成 InputStream 的方法
Bitmap bitmap = null;
try {
    InputStream inputStream = respons.body().byteStream();
    bitmap = BitmapFactory.decodeStream(inputStream);
    inputStream.close();
    response.close();
} catch (Exception e) {
    Log.e(TAG,"get image error!message:" + e.getMessage());
}

//这个则是 byte[] 数组,有时 InputStream 多个数据读取流会把 Stream 刷新,导致 Stream 为空,所以转为 byte[] 数组转位图,例子只是单张图片的转化

Bitmap bitmap = null;
try {
    byte[] bytes = response.body().bytes();
    bitmap = BitmapFactory.decodeByteArray(bytes,0, bytes.length);
    response.close();
} catch (Exception e) {
    Log.e(TAG,"get image error!message:"+ e.getMessage());
}
  1. 图片若是保存在本地文件中,则可以使用 decodeFile() 方法转化为位图显示。同样一般需要 Options 对象进行适配和压缩。
        Bitmap bitmap = null;
        //直接路径获取
        String path = "xxx/xxx.png";
        bitmap = BitmapFactory.decodeFile(path);

        //转 Stream 再获取
        try {
            InputStream stream = new FileInputStream(path);
            bitmap = BitmapFactory.decodeStream(stream);
            stream.close();
        } catch (Exception e) {
            Log.e(TAG,"load image error!!message:" + e.getMessage());
        }
  1. 图片若是在 assets 目录下,获取方式:
        Bitmap bitmap;
        try {
            String[] pictures = context.getAssets().list("picture");
            for (String path : pictures) {
                InputStream open = null;
                open = context.getAssets().open(path);
                bitmap = BitmapFactory.decodeStream(open);
                open.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
  1. 截图,截取源图片从左上角起点宽高为原来一半的图片。
        //源图片
        Bitmap source;

        int width = source.getWidth();
        int height = source.getHeight();
        
        //截图
        Bitmap tailoring = Bitmap.createBitmap(source, 0, 0, width, height);
  1. 采样压缩,方式很多,主要时 Options 的基本用法。
       String imgPath = "xxx/xxx.png";
        //先获取图片看宽高
        Bitmap source = BitmapFactory.decodeFile(imgPath);

        int screenWidth = 1080;
        int screenHeight = 1920;

        int scalWidth = source.getWidth() / screenWidth;
        int scalHeight = source.getHeight() / screenHeight;
        source = null;

        BitmapFactory.Options options = new BitmapFactory.Options();
        //不读取边,不然会为空
        options.inJustDecodeBounds = false;
        //是否可压缩
        if(scalHeight > 1 || scalWidth > 1) {
            //按照差距较大的压缩
            if(scalWidth > scalHeight) {
                options.inSampleSize = scalWidth;
            } else {
                options.inSampleSize = scalHeight;
            }
        }
        //压缩图片
        Bitmap bitmap = BitmapFactory.decodeFile(imgPath, options);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值