图片按指定比例缩放并压缩至指定大小,解决保存图片文件体积过大bug。
来源:互联网 发布:c 多线程编程书籍 编辑:程序博客网 时间:2024/06/05 08:01
需求:服务器指定图片尺寸大小,并且限制图片文件大小,因此在选择图片后需要进行操作,方法如下:
保存图片的时候,会发现实际文件大小大于当前设置的大小,这个问题在最后面解决,先上正确的代码:
1、指定图片尺寸及文件大小及源图片位置及新图片位置:
float hh = 800f;float ww = 480f;int filesize = 100;//单位是K
//源文件位置String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/soon/a.png";//保存新的文件位置String savepath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/soon/b.png";
2、图片处理方法及调用:
需要先创建文件夹:
String file = Environment.getExternalStorageDirectory().getAbsolutePath(); String filename = "soon"; File appDir = new File(file, filename); if (!appDir.exists()) { appDir.mkdirs(); }
b31.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { compressThreeOne(path); }});
public void compressThreeOne(String srcPath) { BitmapFactory.Options opts = new BitmapFactory.Options(); opts.inJustDecodeBounds = true; Bitmap bitmap = BitmapFactory.decodeFile(srcPath, opts); opts.inJustDecodeBounds = false; int w = opts.outWidth; int h = opts.outHeight; int size = 0; if (w <= ww && h <= hh) { size = 1; } else { double scale = w >= h ? w / ww : h / hh; double log = Math.log(scale) / Math.log(2); double logCeil = Math.ceil(log); size = (int) Math.pow(2, logCeil); } opts.inSampleSize = size; bitmap = BitmapFactory.decodeFile(srcPath, opts); ByteArrayOutputStream baos = new ByteArrayOutputStream(); int quality = 100; bitmap.compress(Bitmap.CompressFormat.JPEG, quality, baos); System.out.println(baos.toByteArray().length); while (baos.toByteArray().length > filesize * 1024) { baos.reset(); bitmap.compress(Bitmap.CompressFormat.JPEG, quality, baos); quality -= 5; System.out.println(baos.toByteArray().length); } try { baos.writeTo(new FileOutputStream(savepath)); } catch (Exception e) { e.printStackTrace(); } finally { try { baos.flush(); baos.close(); } catch (IOException e) { e.printStackTrace(); } }}
3、文件大小与获取图片大小不一致的问题:
之前在压缩和缩放后,将图片保存至本地,发现了这个问题,:
之前的方法如下:
public void saveImageToGallery(Bitmap bmp) { // 首先保存图片 String file = Environment.getExternalStorageDirectory().getAbsolutePath(); String filename = "soon"; File appDir = new File(file, filename); if (!appDir.exists()) { appDir.mkdirs(); } String fileName = "b.jpg"; File currentFile = new File(appDir, fileName); FileOutputStream fos = null; try { // currentFile.createNewFile(); fos = new FileOutputStream(currentFile); bmp.compress(Bitmap.CompressFormat.JPEG, 100, fos); fos.flush(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { if (fos != null) { fos.close(); } } catch (IOException e) { e.printStackTrace(); } }}
结果总会比预算的文件大几倍。
正确的方法是:
baos.writeTo(new FileOutputStream(savepath));
阅读全文
0 0
- 图片按指定比例缩放并压缩至指定大小,解决保存图片文件体积过大bug。
- 压缩图片至指定大小
- 图片压缩指定大小
- 将图片缩放成指定大小(压缩方法)
- 载入图片后按指定宽高缩放比例
- Java将图片压缩至指定大小
- 图片压缩为指定大小
- 压缩图片到指定大小
- 图片压缩成指定大小
- //图片压缩到指定大小
- java指定宽度等比例压缩图片
- swift如何压缩图片到指定大小以内,尺寸和体积双压缩
- 把文件流中的图片按指定的大小保存到指定的文件中
- 图片等比例缩放,图片指定宽或高按比例缩放,图片不变形缩放
- 图片缩放成指定的大小
- 动态缩放图片,剪裁指定位置大小
- UIImage图片缩放到指定大小尺寸
- css3 来实现图片在指定容器内等比例缩放,并控制中上下左右居中
- 转换字符串格式为字符+连续出现的次数
- 极简单片机开发
- ftp(vsftp) 在Cortex-A9 ARM CPU s5p4418上的移植
- 枚举类和常量
- install php7 mysql5.6 apache2.4 in centos 6 or 7
- 图片按指定比例缩放并压缩至指定大小,解决保存图片文件体积过大bug。
- 【JavaSE学习笔记】循环结构语句
- 使用FTP上传图片到图片服务器
- 使用Visual Studio 2013进行单元测试
- Jaccard系数学习
- docker的命令
- SVN使用【介绍SVN、快速入门、解决冲突】
- XSS攻击
- getCountLine() 为0的问题