android Bitmap转化成Base64 String 人脸识别 身份证识别 驾照识别 图片转化成String
程序员文章站
2022-05-10 08:22:14
最近项目集成阿里云上面的人脸识别 身份证识别 驾照识别功能 需要把拍的照片转化成Base64 的String作文参数上传,一下是根据图片路径imgPath转化的代码段:...
最近项目集成阿里云上面的人脸识别 身份证识别 驾照识别功能 需要把拍的照片转化成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; } }
下一篇: 3月手机集中发布,哪一款撩动你的心?