Android 图片压缩的一些小技巧,以及bitmap和byte[]之间的转换
2018-01-09 17:31
543 查看
对于获取到的图片进行压缩然后上传,这个事情还是很重要的而且是很实用的。
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); } [b]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; } } }
相关文章推荐
- Android学习 之 Bitmap Drawable byte[] 三者之间的转换以及把数组存入数据库及提取数据重新组合成所需对象,如图像
- android在处理图片资源的时候,Drawable、Bitmap、byte[]之间的转换
- Android学习 之 Bitmap Drawable byte[] 三者之间的转换以及把数组存入数据库及提取数据重新组合成所需对象,如图像
- android 计算Bitmap占byte大小,以及Bitmap中像素数的计算,另外再给出一个按尺寸裁剪图片的方法
- android 将图片内容解析成字节数组,将字节数组转换为ImageView可调用的Bitmap对象,图片缩放,把字节数组保存为一个文件,把Bitmap转Byte
- Android图片二进制与Bitmap、Drawable之间的转换
- byte[]和stream以及图片之间的转换关系
- android --- Drawable、Bitmap、byte[]之间的转换
- Android Bitmap与DrawAble与byte[]与InputStream之间的转换工具类
- C# 图片与byte[]之间以及byte[]与string之间的转换
- Bitmap Drawable byte[] 三者之间的转换以及把数组存入数据库及提取数据重新组合成所需对象
- Android图片二进制与Bitmap、Drawable之间的转换
- [Android] bitmap/drawable/byte的转换和获取圆角/倒影图片
- Android Drawable和Bitmap图片之间转换
- Android Bitmap与DrawAble与byte[]与InputStream之间的转换工具类【转】
- android 将图片内容解析成字节数组,将字节数组转换为ImageView可调用的Bitmap对象,图片缩放,把字节数组保存为一个文件,把Bitmap转Byte
- android 将图片内容解析成字节数组,将字节数组转换为ImageView可调用的Bitmap对象,图片缩放,把字节数组保存为一个文件,把Bitmap转Byte
- android 将图片内容解析成字节数组,将字节数组转换为ImageView可调用的Bitmap对象,图片缩放,把字节数组保存为一个文件,把Bitmap转Byte
- Android图片二进制与Bitmap、Drawable之间的转换
- android 将图片内容解析成字节数组,将字节数组转换为ImageView可调用的Bitmap对象,图片缩放,把字节数组保存为一个文件,把Bitmap转Byte