package com.basic.security.utils; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.Environment; import android.util.Log; import com.basic.security.base.BaseApplication; import com.basic.security.manager.IdCardManager; import com.basic.security.manager.PersonIdentityManager; import com.basic.security.manager.PersonManager; import com.basic.security.manager.impl.cblite.DeviceManager; import com.basic.security.model.ModelAdapter; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.IOException; // 批量导入人脸到数据库 public class SaveTestData { public static void savePicToDatabase() { BaseApplication.getApplication().executorService.execute(() -> { File f = new File(Environment.getExternalStorageDirectory() + "/APicData/data/"); if (!f.exists()) {//判断路径是否存在 return; } File[] files = f.listFiles(); if (files == null) {//判断权限 return; } int i = 0; for (File _file : files) {//遍历目录 if (_file.isFile()) { String _name = _file.getName(); String filePath = _file.getAbsolutePath();//获取文件路径 String fileName = _file.getName().substring(0, _name.lastIndexOf("."));//获取文件名 Log.e("savePicToDatabase", "第" + i + "个" + "name = " + _name + "filePath = " + filePath + "fileName = " + fileName); i += 1; Bitmap bitmap = BitmapFactory.decodeFile(filePath); if (bitmap == null) { continue; } Log.e("savePicToDatabase", "file_name = " + fileName + " height = " + bitmap.getHeight() + " weight = " + bitmap.getWidth()); BaseApplication.getApplication().detectLock.lock(); byte[] featureData = FaceId.instance.extractFeature1(IdCardManager.getPixelsBGR(bitmap), bitmap.getWidth(), bitmap.getHeight()); BaseApplication.getApplication().detectLock.unlock(); ByteArrayOutputStream stream = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream); byte[] jpgData = stream.toByteArray(); try { stream.close(); } catch (IOException e) { Log.e("savePicToDatabase", "stream.close = error = error = error = error = error = error = " + e.getMessage()); e.printStackTrace(); } ModelAdapter person = new ModelAdapter() .setString("name", fileName) .setString("sign_up_status", "已注册") .setString("verify_status", "不需要验证身份证") .setString("sign_up_method", "统一导入") .setString("sign_up_time", System.currentTimeMillis() + "") .setString("device_id", DeviceManager.getDeviceId()) .setString("company_id", DeviceManager.getCompareId()) .setString("table", "person"); try { byte[] feature = featureData;//new Blob("image/jpeg", featureData); person.setBlob("camera_image_feature", feature); byte[] jpg = jpgData;// new Blob("image/jpeg", jpgData); person.setBlob("camera_image_path", jpg); } catch (Exception e) { Log.e("savePicToDatabase", "setBlob setBlob setBlob = error = error = error = error = error = error = " + e.getMessage()); e.printStackTrace(); } try { PersonManager.savePerson(person); } catch (Exception e) { Log.e("savePicToDatabase", "CouchbaseUtil.database.save = error = error = error = error = error = error = " + e.getMessage()); e.printStackTrace(); } ModelAdapter person_identity = new ModelAdapter() .setString("person_id", person.getId()) // .setString("identity_id", normal_person.getId()) .setString("table", "person_identity") .setString("device_id", DeviceManager.getDeviceId()) .setString("company_id", DeviceManager.getCompareId()); PersonIdentityManager.savePersonIdentity(person_identity); FaceId.instance.addFeatureToDb(person.getId(), featureData); } } }); } }