android Bitmap转化成Base64 String 人脸识别 身份证识别 驾照识别 图片转化成String
2018-03-16 09:20
639 查看
最近项目集成阿里云上面的人脸识别 身份证识别 驾照识别功能 需要把拍的照片转化成Base64 的String作文参数上传,一下是根据图片路径imgPath转化的代码段:
public static String imgToBase64String(String imgPath) { System.out.println("fileLen = "+new File(imgPath).length()); Bitmap bitmap = BitmapFactory.decodeFile(imgPath); Bitmap bitmap1 = ImageUtils.comp(bitmap); Log.e("2018324565", "转化成功"); return bitmapToBase64(bitmap1); } /** * bitmap转为base64 * * @param bitmap * @return */ public static String bitmapToBase64(Bitmap bitmap) { String result = null; ByteArrayOutputStream baos = null; try { if (bitmap != null) { baos = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.JPEG, 80, baos); baos.flush(); baos.close(); byte[] bitmapBytes = baos.toByteArray(); result = Base64.encodeToString(bitmapBytes, Base64.DEFAULT); } } catch (IOException e) { e.printStackTrace(); } finally { try { if (baos != null) { baos.flush(); baos.close(); } } catch (IOException e) { e.printStackTrace(); } } return result; }
###写个工具类把照片回传的照片路径扔进去即可
另外图片不得大于1.5M 可以先压缩下图片的质量
import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.util.Log; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; /** * Created by lilin on 2016/12/7. * func : bitmap压缩方法 */ public class ImageUtils { public static Bitmap comp(Bitmap image) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); image.compress(Bitmap.CompressFormat.JPEG, 100, baos); Log.e("wechat", "质量压缩前baos.toByteArray().length" + baos.toByteArray().length / 1024 + "字节"); if (baos.toByteArray().length / 1024 > 1024) {//判断如果图片大于1M,进行压缩避免在生成图片(BitmapFactory.decodeStream)时溢出 baos.reset();//重置baos即清空baos image.compress(Bitmap.CompressFormat.JPEG, 10, baos);//这里压缩30%,把压缩后的数据存放到baos中 } Log.e("wechat", "baos.toByteArray().length" + baos.toByteArray().length / 1024 + "字节"); BitmapFactory.Options newOpts = new BitmapFactory.Options(); int w = image.getWidth(); int h = image.getHeight(); //现在主流手机比较多是1280*720分辨率,所以高和宽我们设置为 float hh = 1280f;//这里设置高度为1280f float ww = 720f;//这里设置宽度为720f //缩放比。由于是固定比例缩放,只用高或者宽其中一个数据进行计算即可 int be = 1;//be=1表示不缩放 if (w > h && w > ww) {//如果宽度大的话根据宽度固定大小缩放 be = (int) (w / ww); } else if (w < h && h > hh) {//如果高度高的话根据宽度固定大小缩放 be = (int) (h / hh); } if (be <= 0) be = 1; newOpts.inSampleSize = be;//设置缩放比例 newOpts.inPreferredConfig = Bitmap.Config.RGB_565;//降低图片从ARGB888到RGB565 //重新读入图片,注意此时已经把options.inJustDecodeBounds 设回false了 ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray()); Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, newOpts); Log.e("wechat", "comp压缩bitmap图片的大小" + (bitmap.getByteCount() / 1024 / 1024) + "M宽度为" + bitmap.getWidth() + "高度为" + bitmap.getHeight()); return bitmap; } }
相关文章推荐
- android中bitmap图片与二进制,String间的转化
- Android图片的压缩和Bitmap转化为String传输
- Android 将View 转化为bitmap 图片
- 分享的是Android图像特效处理的小技巧,比如圆角、倒影、还有就是图片缩放、Drawable转化为Bitmap、Bitmap转化为Drawable等等
- 19_Android中图片处理原理篇,关于人脸识别网站,图片加载到内存,图片缩放,图片翻转倒置,网上撕衣服游戏案例编写
- Opencv3.3 人脸识别 图片拼接 Android
- Android 识别身份证号码(图片识别)
- Base64将图片转化成String
- Android Bitmap保存为.bmp格式,图像转化为黑白图片
- android 网络图片路径解析和本地图片路径解析的方法 ,base64加密报文解析成bitmap【代码示例】
- Android静态图片人脸识别的完整demo(附完整源码)
- Android图片操作-将Drawable转化为Bitmap
- 【Android】图片操作之 —— Bitmap,byte[],Drawable,InputStream相互转化工具类
- Android图片操作-将Drawable转化为Bitmap
- android处理图片工具类,图片压缩,把图片互换转换成base64和bitmap
- Android 上传图片到后台的一直方式Base64的String形式
- Android人脸识别Demo竖屏YUV方向调整和图片保存(分享)
- Android 把bitmap转换成String,计算图片的缩放值,根据路径获得突破并压缩返回bitmap用于显示,获取保存 隐患检查的图片文件夹名称,获取保存图片的目录,根据路径删除图片,添加到图库
- Android中图片转化为bitmap
- android中Bitmap(图片)和String类型直接的转换