1. 程式人生 > >android Bitmap轉化成Base64 String 人臉識別 身份證識別 駕照識別 圖片轉化成String

android Bitmap轉化成Base64 String 人臉識別 身份證識別 駕照識別 圖片轉化成String

最近專案整合阿里雲上面的人臉識別 身份證識別 駕照識別功能 需要把拍的照片轉化成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;
    }
}