Android 图片压缩的一些小技巧,以及bitmap和byte[]之间的转换

对于获取到的图片进行压缩然后上传,这个事情还是很重要的而且是很实用的。


public byte[] compressBitmap(Bitmap bitmap) {
    ByteArrayOutputStream baos = null;
    try {
        baos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
        baos.close();
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        byte[] buffer = baos.toByteArray();
        BitmapFactory.decodeByteArray(buffer, 0, buffer.length, options);
        int outWidth = options.outWidth;
        int scale = outWidth / WIDTH;
        if (scale > 1) {
            options.inSampleSize = scale;
            options.inJustDecodeBounds = false;
            Bitmap tempBitmap = BitmapFactory.decodeByteArray(buffer, 0, buffer.length, options);
            baos = new ByteArrayOutputStream();
            tempBitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
            baos.close();
        }
        return baos.toByteArray();
    } catch (Exception e) {
        return null;
    } finally {
        try {
            if (baos != null) {
                baos.close();
            }
        } catch (IOException e) {
            return null;
        }
    }
}

public byte[] compressBitmap(byte[] buffer) {
    if (buffer == null || buffer.length == 0) {
        return null;
    }
    ByteArrayOutputStream baos = null;
    try {
        // 只获取图片的大小信息,而不是将整张图片载入在内存中,避免内存溢出
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeByteArray(buffer, 0, buffer.length, options);
        int outWidth = options.outWidth;
        int scale = outWidth / WIDTH;
        if (scale > 1) {
            options.inSampleSize = scale;
            options.inJustDecodeBounds = false;
            Bitmap bitmap = BitmapFactory.decodeByteArray(buffer, 0, buffer.length, options);
            baos = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
            return baos.toByteArray();
        } else {
            return buffer;
        }
    } catch (Exception e) {
        return null;
    } catch (OutOfMemoryError e) {
        return null;
    } finally {
        try {
            if (baos != null) {
                baos.close();
            }
        } catch (IOException e) {
            return null;
        }
    }
}


public Bitmap compressBitmap(String path) {
    Bitmap bitmap = null;
    try {
        bitmap = BitmapFactory.decodeFile(path);
        int scale = 1;
        if(bitmap.getWidth() > bitmap.getHeight()){
            //横向拍摄的照片
            scale = bitmap.getHeight() / WIDTH;
        }else{
            //竖屏拍摄的照片
            scale = bitmap.getWidth() / WIDTH;
        }
        BitmapFactory.Options opt = new BitmapFactory.Options();
        opt.inJustDecodeBounds = false;
        if (scale > 1) {
            //压缩到1/scale
            opt.inSampleSize = scale;
            bitmap = BitmapFactory.decodeFile(path, opt);
        } else {
            bitmap = BitmapFactory.decodeFile(path, opt);
        }
    } catch (OutOfMemoryError e) {
        return null;
    }
    return bitmap;
}

public byte[] bitmapToByte(Bitmap bitmap) {
    ByteArrayOutputStream bos = null;
    try {
        if (bitmap == null) {
            return null;
        }
        bos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos);
        return bos.toByteArray();

    } catch (Exception e) {
        return null;
    } catch (OutOfMemoryError e) {
        return null;
    } finally {
        try {
            if (bos != null)
                bos.close();
        } catch (IOException e) {
            return null;
        }
    }
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值