android压缩图片,并将其转化成base64提交到服务器
2019独角兽企业重金招聘Python工程师标准>>>
提交图片到服务器,如果图片太大的话,会导致内存溢出。所以我们需要对图片进行压缩 并且转化成base64编码进行提交
1.传入图片的url 压缩图片并转化成base64
/*** 传入url 先压缩 后转为base64* @param srcPath* @return*/ private String getimage(String srcPath) {BitmapFactory.Options newOpts = new BitmapFactory.Options();//开始读入图片,此时把options.inJustDecodeBounds 设回true了newOpts.inJustDecodeBounds = true;Bitmap bitmap = BitmapFactory.decodeFile(srcPath,newOpts);//此时返回bm为空newOpts.inJustDecodeBounds = false;int w = newOpts.outWidth;int h = newOpts.outHeight;//现在主流手机比较多是800*480分辨率,所以高和宽我们设置为float hh = 1280f;//这里设置高度为800ffloat ww = 720f;//这里设置宽度为480f//缩放比。由于是固定比例缩放,只用高或者宽其中一个数据进行计算即可int be = 1;//be=1表示不缩放if (w > h && w > ww) {//如果宽度大的话根据宽度固定大小缩放be = (int) (newOpts.outWidth / ww);} else if (w < h && h > hh) {//如果高度高的话根据宽度固定大小缩放be = (int) (newOpts.outHeight / hh);}if (be <= 0)be = 1;newOpts.inSampleSize = be;//设置缩放比例//重新读入图片,注意此时已经把options.inJustDecodeBounds 设回false了bitmap = BitmapFactory.decodeFile(srcPath, newOpts);return compressImageToBase64(bitmap);//压缩好比例大小后再进行质量压缩 }private String compressImageToBase64(Bitmap image) {ByteArrayOutputStream baos = new ByteArrayOutputStream();image.compress(Bitmap.CompressFormat.JPEG, 100, baos);//质量压缩方法,这里100表示不压缩,把压缩后的数据存放到baos中int options = 100;while ( baos.toByteArray().length / 1024>100) { //循环判断如果压缩后图片是否大于100kb,大于继续压缩baos.reset();//重置baos即清空baosimage.compress(Bitmap.CompressFormat.JPEG, options, baos);//这里压缩options%,把压缩后的数据存放到baos中options -= 10;//每次都减少10}byte[] bytes = baos.toByteArray();Log.e("preencode", new String(bytes));String url = new String(Base64Coder.encode(bytes));return url;}
2.Base64Coder
package com.ifonyo.door.utils; /*** 下面这些注释是下载这个类的时候本来就有的,本来要删除的,但看了下竟然是license,吼吼,* 好东西,留在注释里,以备不时之用,大家有需要加license的可以到下面的网址找哦*///EPL, Eclipse Public License, V1.0 or later, http://www.eclipse.org/legal //LGPL, GNU Lesser General Public License, V2.1 or later, http://www.gnu.org/licenses/lgpl.html //GPL, GNU General Public License, V2 or later, http://www.gnu.org/licenses/gpl.html //AL, Apache License, V2.0 or later, http://www.apache.org/licenses //BSD, BSD License, http://www.opensource.org/licenses/bsd-license.php /*** A Base64 encoder/decoder.** <p>* This class is used to encode and decode data in Base64 format as described in RFC 1521.** <p>* Project home page: <a href="http://www.source-code.biz/base64coder/java/">www.source-code.biz/base64coder/java</a>* Author: Christian d'Heureuse, Inventec Informatik AG, Zurich, Switzerland* Multi-licensed: EPL / LGPL / GPL / AL / BSD.*//*** 这个类在上面注释的网址中有,大家可以自行下载下,也可以直接用这个,* 公开的Base64Coder类(不用深究它是怎么实现的,* 还是那句话,有轮子直接用轮子),好用的要死人了...* 小马也很无耻的引用了这个网址下的东东,吼吼...* @Title: Base64Coder.java* @Package com.xiaoma.piccut.demo* @Description: TODO* @author XiaoMa*/public class Base64Coder {//The line separator string of the operating system.private static final String systemLineSeparator = System.getProperty("line.separator");//Mapping table from 6-bit nibbles to Base64 characters.private static char[] map1 = new char[64];static {int i=0;for (char c='A'; c<='Z'; c++) map1[i++] = c;for (char c='a'; c<='z'; c++) map1[i++] = c;for (char c='0'; c<='9'; c++) map1[i++] = c;map1[i++] = '+'; map1[i++] = '/'; }//Mapping table from Base64 characters to 6-bit nibbles.private static byte[] map2 = new byte[128];static {for (int i=0; i<map2.length; i++) map2[i] = -1;for (int i=0; i<64; i++) map2[map1[i]] = (byte)i; }/*** Encodes a string into Base64 format.* No blanks or line breaks are inserted.* @param s A String to be encoded.* @return A String containing the Base64 encoded data.*/public static String encodeString (String s) {return new String(encode(s.getBytes())); }/*** Encodes a byte array into Base 64 format and breaks the output into lines of 76 characters.* This method is compatible with <code>sun.misc.BASE64Encoder.encodeBuffer(byte[])</code>.* @param in An array containing the data bytes to be encoded.* @return A String containing the Base64 encoded data, broken into lines.*/public static String encodeLines (byte[] in) {return encodeLines(in, 0, in.length, 76, systemLineSeparator); }/*** Encodes a byte array into Base 64 format and breaks the output into lines.* @param in An array containing the data bytes to be encoded.* @param iOff Offset of the first byte in <code>in</code> to be processed.* @param iLen Number of bytes to be processed in <code>in</code>, starting at <code>iOff</code>.* @param lineLen Line length for the output data. Should be a multiple of 4.* @param lineSeparator The line separator to be used to separate the output lines.* @return A String containing the Base64 encoded data, broken into lines.*/public static String encodeLines (byte[] in, int iOff, int iLen, int lineLen, String lineSeparator) {int blockLen = (lineLen*3) / 4;if (blockLen <= 0) throw new IllegalArgumentException();int lines = (iLen+blockLen-1) / blockLen;int bufLen = ((iLen+2)/3)*4 + lines*lineSeparator.length();StringBuilder buf = new StringBuilder(bufLen);int ip = 0;while (ip < iLen) {int l = Math.min(iLen-ip, blockLen);buf.append (encode(in, iOff+ip, l));buf.append (lineSeparator);ip += l; }return buf.toString(); }/*** Encodes a byte array into Base64 format.* No blanks or line breaks are inserted in the output.* @param in An array containing the data bytes to be encoded.* @return A character array containing the Base64 encoded data.*/public static char[] encode (byte[] in) {return encode(in, 0, in.length); }/*** Encodes a byte array into Base64 format.* No blanks or line breaks are inserted in the output.* @param in An array containing the data bytes to be encoded.* @param iLen Number of bytes to process in <code>in</code>.* @return A character array containing the Base64 encoded data.*/public static char[] encode (byte[] in, int iLen) {return encode(in, 0, iLen); }/*** Encodes a byte array into Base64 format.* No blanks or line breaks are inserted in the output.* @param in An array containing the data bytes to be encoded.* @param iOff Offset of the first byte in <code>in</code> to be processed.* @param iLen Number of bytes to process in <code>in</code>, starting at <code>iOff</code>.* @return A character array containing the Base64 encoded data.*/public static char[] encode (byte[] in, int iOff, int iLen) {int oDataLen = (iLen*4+2)/3; // output length without paddingint oLen = ((iLen+2)/3)*4; // output length including paddingchar[] out = new char[oLen];int ip = iOff;int iEnd = iOff + iLen;int op = 0;while (ip < iEnd) {int i0 = in[ip++] & 0xff;int i1 = ip < iEnd ? in[ip++] & 0xff : 0;int i2 = ip < iEnd ? in[ip++] & 0xff : 0;int o0 = i0 >>> 2;int o1 = ((i0 & 3) << 4) | (i1 >>> 4);int o2 = ((i1 & 0xf) << 2) | (i2 >>> 6);int o3 = i2 & 0x3F;out[op++] = map1[o0];out[op++] = map1[o1];out[op] = op < oDataLen ? map1[o2] : '='; op++;out[op] = op < oDataLen ? map1[o3] : '='; op++; }return out; }/*** Decodes a string from Base64 format.* No blanks or line breaks are allowed within the Base64 encoded input data.* @param s A Base64 String to be decoded.* @return A String containing the decoded data.* @throws IllegalArgumentException If the input is not valid Base64 encoded data.*/public static String decodeString (String s) {return new String(decode(s)); }/*** Decodes a byte array from Base64 format and ignores line separators, tabs and blanks.* CR, LF, Tab and Space characters are ignored in the input data.* This method is compatible with <code>sun.misc.BASE64Decoder.decodeBuffer(String)</code>.* @param s A Base64 String to be decoded.* @return An array containing the decoded data bytes.* @throws IllegalArgumentException If the input is not valid Base64 encoded data.*/public static byte[] decodeLines (String s) {char[] buf = new char[s.length()+3];int p = 0;for (int ip = 0; ip < s.length(); ip++) {char c = s.charAt(ip);if (c != ' ' && c != '\r' && c != '\n' && c != '\t')buf[p++] = c; }while ((p % 4) != 0)buf[p++] = '0';return decode(buf, 0, p); }/*** Decodes a byte array from Base64 format.* No blanks or line breaks are allowed within the Base64 encoded input data.* @param s A Base64 String to be decoded.* @return An array containing the decoded data bytes.* @throws IllegalArgumentException If the input is not valid Base64 encoded data.*/public static byte[] decode (String s) {return decode(s.toCharArray()); }/*** Decodes a byte array from Base64 format.* No blanks or line breaks are allowed within the Base64 encoded input data.* @param in A character array containing the Base64 encoded data.* @return An array containing the decoded data bytes.* @throws IllegalArgumentException If the input is not valid Base64 encoded data.*/public static byte[] decode (char[] in) {return decode(in, 0, in.length); }/*** Decodes a byte array from Base64 format.* No blanks or line breaks are allowed within the Base64 encoded input data.* @param in A character array containing the Base64 encoded data.* @param iOff Offset of the first character in <code>in</code> to be processed.* @param iLen Number of characters to process in <code>in</code>, starting at <code>iOff</code>.* @return An array containing the decoded data bytes.* @throws IllegalArgumentException If the input is not valid Base64 encoded data.*/public static byte[] decode (char[] in, int iOff, int iLen) {if (iLen%4 != 0) throw new IllegalArgumentException ("Length of Base64 encoded input string is not a multiple of 4.");while (iLen > 0 && in[iOff+iLen-1] == '=') iLen--;int oLen = (iLen*3) / 4;byte[] out = new byte[oLen];int ip = iOff;int iEnd = iOff + iLen;int op = 0;while (ip < iEnd) {int i0 = in[ip++];int i1 = in[ip++];int i2 = ip < iEnd ? in[ip++] : 'A';int i3 = ip < iEnd ? in[ip++] : 'A';if (i0 > 127 || i1 > 127 || i2 > 127 || i3 > 127)throw new IllegalArgumentException ("Illegal character in Base64 encoded data.");int b0 = map2[i0];int b1 = map2[i1];int b2 = map2[i2];int b3 = map2[i3];if (b0 < 0 || b1 < 0 || b2 < 0 || b3 < 0)throw new IllegalArgumentException ("Illegal character in Base64 encoded data.");int o0 = ( b0 <<2) | (b1>>>4);int o1 = ((b1 & 0xf)<<4) | (b2>>>2);int o2 = ((b2 & 3)<<6) | b3;out[op++] = (byte)o0;if (op<oLen) out[op++] = (byte)o1;if (op<oLen) out[op++] = (byte)o2; }return out; }//Dummy constructor.private Base64Coder() {}} // end class Base64Coder
转载于:https://my.oschina.net/handlermsg/blog/806416
android压缩图片,并将其转化成base64提交到服务器相关推荐
- Android压缩图片到100K以下并保持不失真的高效方法
在开发Android企业应用时,会经常上传图片到服务器,而我们公司目前维护的一个项目便是如此.该项目是通过私有apn与服务器进行交互的,联通的还好,但移动的速度实在太慢,客户在使用软件的过程中,由于上 ...
- Android压缩图片和libjpeg库
前言 Fjpeg使用 Fjpeg 注意 如何使用 如何压缩图片只改变在硬盘的存储大小 如何改变图片分辨率让其Bitmap对象可以加载到内存中 关于重载版本 开始学习之旅 补充知识的结论 修改图片分辨率 ...
- Android压缩图片并且保存到本地内存卡中
亲爱的同学们,相信我们在软件开发中经常会遇到拍照,选择相册上传的功能.前几天遇到一个这样的一个问题.后台要求我上传的图片不能超过500KB,大家知道我们现在的智能手机像素越来越高,拍照的照片一般都是几 ...
- android压缩图片不失真,Android压缩图片到100K以下并保持不失真的高效方法 - feicien的博客 - eoe移动开发者社区...
1.获取原始图片的长和宽 1 2 3 4 5 BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDe ...
- android字节流压缩,Android压缩图片,程序崩溃
压缩图片时,内存增加的很厉害,以致于在RAM不是很大的手机上,程序会直接崩溃. private Bitmap comp(Bitmap image) { ByteArrayOutputStream ba ...
- Android 高清加载巨图方案 拒绝压缩图片
Android 高清加载巨图方案 拒绝压缩图片 转载请标明出处: http://blog.csdn.net/lmj623565791/article/details/49300989: 本文出自: ...
- LocalResizeIMG前端HTML5本地压缩图片上传,兼容移动设备IOS,android
LocalResizeIMG前端HTML5本地压缩图片上传,兼容移动设备IOS,android jincon 发表于 2015-02-26 18:31:01 发表在: php开发 localresiz ...
- 【Android 内存优化】Android 工程中使用 libjpeg-turbo 压缩图片 ( 初始化压缩对象 | 打开文件 | 设置压缩参数 | 写入压缩图像数据 | 完成压缩 | 释放资源 )
文章目录 一.使用 libjpeg-turbo 压缩图片流程 二.初始化 JPEG 压缩对象 三.打开文件 四.设置压缩参数 五.开始压缩 六.循环写入压缩数据 七.完成图片压缩及收尾 八.libjp ...
- 【Android 内存优化】Android 工程中使用 libjpeg-turbo 压缩图片 ( JNI 传递 Bitmap | 获取位图信息 | 获取图像数据 | 图像数据过滤 | 释放资源 )
文章目录 一.Bitmap 图像数据处理 二.Java 层 Bitmap 对象转为 JNI 层 bitmap 对象 三.获取 bitmap 中的图像数据 四.过滤 bitmap 中的图像数据 ( 获取 ...
最新文章
- 在js中加html_在HTML文档中嵌入JavaScript的四种方法
- H.264专利费不受FRAND限制 Android设备商受影响
- java parallelstream_关于Java8 parallelStream并发安全的深入讲解
- Spring DI模式 小样例
- 更改临时文件夹的路径
- 类型转换一种处理方式
- cms php上传失败,PHPCMSV9无法上传附件,提示空信息的超级bug
- struts2自定义标签_Struts 2 –没有为动作和结果输入定义结果
- 产品经理面试习题大汇总
- 财务oracle ap模块,Oracle ERP EBS R12财务应付AP模块基本业务操作
- 三维扫描仪[10]——如何设计一台云台式扫描仪(代码详解)
- 第025篇:诚意回归CSDN博客之旅(内含SPM惊喜消息)
- 用python进行因式分解_Python实现的质因式分解算法示例
- VGG多种网络结构的搭建以及感受野的计算
- 【Panoramic stitching】并查集(disjoint set)结构及源码
- 什么是预付卡及预付卡发展前景
- javascript音乐播放器
- win10桌面版outlook邮箱配置流程
- vim 删除文件单行或多行内容
- android adhoc,android手机怎么连接adhoc共享电脑网络
热门文章
- java与数据库连接的几个步骤
- 在 ASP.NET MVC 中创建自定义 HtmlHelper
- 非客观书评(三)——《ARM Cortex-M3 权威指南》
- g2o图优化简介与基本使用方法
- Oracle数据库中dml提交,奇怪,ORACLE的触发器的DML操作,没有COMMIT,居然也能真正的提交掉???...
- mysql用来干嘛的_CPU占用又爆了?MySQL到底在干什么
- android 分享qq微信朋友圈,H5微信JS-SDK实现分享朋友 朋友圈以及QQ自定义分享
- python智能化推荐_windows python flask
- python编程特点_Python基础(1)--Python编程习惯与特点
- 磁盘不足 导致内存 linux,为什么 Linux 需要 Swapping,仅仅是内存不够用?