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;
        }
    }
}

你可能感兴趣的:(十年程序员的专栏)