Android 图片的压缩(不失真)及保存代码分析
来源:互联网 发布:西安电子科技大学网络 编辑:程序博客网 时间:2024/06/14 05:20
整理和学习一下最近项目里用到的图片压缩方法,在网上找的资料,记录下以免忘记。
inJustDecodeBounds:
当这个值置为true时,解码时将不会返回bitmap,只会返回这个bitmap的尺寸,又不会将其加载到内存时。这是一个非常有用的属性。
/** * 根据路径获得突破并压缩返回bitmap用于显示 * @param filePath 图片的路径 * @param reqWidth 要求的图片的像素 * @param reqHeight 要求的图片的像素 * @return */ public static Bitmap getSmallBitmap(String filePath, int reqWidth, int reqHeight) { final BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeFile(filePath, options); options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); options.inJustDecodeBounds = false; return BitmapFactory.decodeFile(filePath, options); }
outWidth和outHeight:
表示这个Bitmap的宽和高,一般和inJustDecodeBounds一起使用来获得Bitmap的宽高,但是不加载到内存。
inSampleSize:是一个int,当它小于1的时候,将会被当做1处理,如果大于1,那么就会按照比例(1 / inSampleSize)缩小bitmap的宽和高、降低分辨率,大于1时这个值将会被处置为2的倍数。例如,width=100,height=100,inSampleSize=2,那么就会将bitmap处理为,width=50,height=50,宽高降为1 / 2,像素数降为1 / 4。
public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) { final int height = options.outHeight; final int width = options.outWidth; int inSampleSize = 1; if (height > reqHeight || width > reqWidth) { final int heightRatio = Math.round((float) height / (float) reqHeight); final int widthRatio = Math.round((float) width / (float) reqWidth); inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio; } return inSampleSize; }
public static String bitmapToString(String filePath) { Bitmap bm = getSmallBitmap(filePath,1080,768); ByteArrayOutputStream baos = new ByteArrayOutputStream(); bm.compress(Bitmap.CompressFormat.JPEG, 60, baos); byte[] b = baos.toByteArray(); return Base64.encodeToString(b, Base64.DEFAULT); }
图片保存比较简单;弄清思路就可以:
String savePath = Environment.getExternalStorageDirectory().toString()+"/testPicture2"; String imageName = System.currentTimeMillis() + ".jpg"; File file = new File(savePath); if (!file.exists()){ file.mkdirs(); } File file1 = new File(savePath, imageName); try { BufferedOutputStream bufferedOutputStream = null; bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(file1)); base64ToBitmap(imageString).compress(Bitmap.CompressFormat.JPEG, 100, bufferedOutputStream); bufferedOutputStream.flush(); bufferedOutputStream.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }
阅读全文
0 0
- Android 图片的压缩(不失真)及保存代码分析
- android图片压缩不失真
- Android高效压缩图片不失真的方法总结
- Android拍照按路径保存图片以及图片压缩不失真
- 图片压缩不失真
- 笔记:Android不失真压缩图片
- Android实现图片压缩并且不失真
- android图片压缩不失真实战
- VB压缩图片(不失真)
- 图片极致压缩,不失真
- Android压缩图片并保持不失真的高效方法以及为图片添加水印
- Android压缩图片到100K以下并保持不失真的高效方法
- Android压缩图片到100K以下并保持不失真的高效方法
- Android压缩图片到100K以下并保持不失真的高效方法
- Android压缩图片到100K以下并保持不失真的高效方法
- Android压缩图片到100K以下并保持不失真的高效方法
- Android压缩图片到100K以下并保持不失真的高效方法
- Android压缩图片到100K以下并保持不失真的高效方法
- Use Retation Sensor to control the brightness of LED lights
- BitMap算法
- Python新式类与经典类(旧式类)的区别
- Toast
- 冷门的js和css技巧
- Android 图片的压缩(不失真)及保存代码分析
- vb gridview 与jquery 冻结行列
- WIN8 隐私声明
- C#学习笔记_3.C#数据类型
- HP Gen8 MicroServer服务器安装WIN7_32位系统
- AB 压力测试工具ApacheBench ab压测参数分析
- 轮播图+小圆点展示
- 90度展示闭合
- javascript继承