package com.basic.security.manager; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.SystemClock; import android.util.Log; import android.widget.Toast; import com.alfeye.readcardlib.entity.CardInfo; import com.alfeye.readcardlib.readcard.ReadCardUtil; import com.basic.security.activity.MainActivity; import com.basic.security.base.BaseApplication; import com.basic.security.base.BaseFragment; import com.basic.security.utils.Constants; import com.basic.security.utils.DateUtil; import com.basic.security.utils.FaceId; import com.basic.security.utils.IdCard; import com.basic.security.utils.ToastUtil; import java.io.ByteArrayOutputStream; import java.nio.ByteBuffer; import static com.basic.security.utils.Constants.isNewIDCardReader; public class IdCardManager { private static IntentFilter filter; private static BroadcastReceiver receiver; private volatile static boolean inRead = false; private static boolean receiverRegisted = false; private static int second = 0; public static byte[] getPixelsBGR(Bitmap image) { // calculate how many bytes our image consists of int bytes = image.getByteCount(); ByteBuffer buffer = ByteBuffer.allocate(bytes); // Create a new buffer image.copyPixelsToBuffer(buffer); // Move the byte data to the buffer byte[] temp = buffer.array(); // Get the underlying array containing the data. byte[] pixels = new byte[(temp.length / 4) * 3]; // Allocate for BGR // Copy pixels into place for (int i = 0; i < temp.length / 4; i++) { pixels[i * 3] = temp[i * 4 + 2]; //B pixels[i * 3 + 1] = temp[i * 4 + 1]; //G pixels[i * 3 + 2] = temp[i * 4]; //R } return pixels; } public static void restart(Context activity) { BaseApplication.getApplication().executorService.execute(() -> { try { second += 2000; Thread.sleep(second); } catch (InterruptedException e) { e.printStackTrace(); } Intent intent = new Intent(); intent.setPackage("com.basic.project.idcardservice"); intent.setAction("com.basic.read.IDCard"); intent.putExtra("type", "start_service"); intent.setFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES); activity.startService(intent); }); } public static void startReadIdCard() { if (!Constants.needIdCardModule) { return; } MainActivity activity = BaseApplication.getApplication().activity; // Log.e("startReadIdCard", "startReadIdCard 只能出现一次。如果多次 要检查代码"); // if (isNewIDCardReader) { if (activity == null) { ToastUtil.show("程序异常, 请重新启动"); return; } activity.readCardUtil = new ReadCardUtil(activity, new ReadCardUtil.OnReadCardListener() { @Override public void onReadCardSucceed(String s, CardInfo cardInfo, Intent intent) { Log.e("OnReadCardListener", "onReadCardSucceed"); if (activity != null) { if (activity.currentFragment == activity.fragment_su_auto_ic_wait_idcard || activity.currentFragment == activity.fragment_su_logged_ic_wait_idcard || (activity.currentFragment == activity.fragment_person_manage && activity.fragment_person_manage.needIdCardInfo())) { IdCard c = new IdCard(); c.birthday = DateUtil.getTimeStamp(cardInfo.getBirthday(), "yyyyMMdd"); c.cardNumber = cardInfo.getCardNum(); c.gender = cardInfo.getGender(); c.name = cardInfo.getName(); c.headBitmap = cardInfo.getPhoto(); ByteArrayOutputStream stream = new ByteArrayOutputStream(); cardInfo.getPhoto().compress(Bitmap.CompressFormat.JPEG, 100, stream); c.jpgData = stream.toByteArray(); BaseApplication.getApplication().detectLock.lock(); c.featureData = FaceId.instance.extractFeature1(getPixelsBGR(c.headBitmap), c.headBitmap.getWidth(), c.headBitmap.getHeight()); BaseApplication.getApplication().detectLock.unlock(); // sendResult(c, activity); BaseFragment.idCardReadTime = System.currentTimeMillis(); BaseFragment.idCard = c; } } } @Override public void onReadCardFail(int i, String s) { Log.e("OnReadCardListener", "onReadCardFail"); } }); filter = new IntentFilter(); filter.addAction("read.id.card.data"); receiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { Log.e("project.id.card", "receive id card data " + Thread.currentThread().getId()); if (activity != null) { if (activity.currentFragment == activity.fragment_su_auto_ic_wait_idcard // || activity.currentFragment == activity.fragment_home || activity.currentFragment == activity.fragment_su_logged_ic_wait_idcard || (activity.currentFragment == activity.fragment_person_manage && activity.fragment_person_manage.needIdCardInfo())) { if (intent.getBooleanExtra("close", false)) { Toast.makeText(activity, "身份证连接失败", Toast.LENGTH_SHORT).show(); restart(activity); } else { IdCard c = new IdCard(); c.birthday = intent.getLongExtra("birthday", 0); c.cardNumber = intent.getStringExtra("cardNumber"); c.gender = intent.getStringExtra("gender"); c.name = intent.getStringExtra("name"); c.jpgData = intent.getByteArrayExtra("jpgData"); c.headBitmap = BitmapFactory.decodeByteArray(c.jpgData, 0, c.jpgData.length); BaseApplication.getApplication().detectLock.lock(); c.featureData = FaceId.instance.extractFeature1(getPixelsBGR(c.headBitmap), c.headBitmap.getWidth(), c.headBitmap.getHeight()); BaseApplication.getApplication().detectLock.unlock(); BaseFragment.idCardReadTime = System.currentTimeMillis(); Log.e("MaFragments", "read card "); BaseFragment.idCard = c; } } } } }; activity.registerReceiver(receiver, filter); receiverRegisted = true; // activity.readCardUtil.startReadCard(); // } else { BaseApplication.getApplication().executorService.execute(() -> { SystemClock.sleep(5 * 1000); while (true) { try { // Log.e("read_id_card", "score 33333333333333333333333333333333333333"); // Thread.sleep(300); // if (activity != null) { if (activity.currentFragment == activity.fragment_su_auto_ic_wait_idcard // || activity.currentFragment == activity.fragment_home || activity.currentFragment == activity.fragment_su_logged_ic_wait_idcard || (activity.currentFragment == activity.fragment_person_manage && activity.fragment_person_manage.needIdCardInfo())) { if (!inRead) { inRead = true; if (isNewIDCardReader) { activity.readCardUtil.startReadCard(); } else { Intent intent = new Intent(); intent.setPackage("com.basic.project.idcardservice"); intent.setAction("com.basic.read.IDCard"); intent.putExtra("type", "start_service"); intent.setFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES); activity.startService(intent); } } // else { // if (activity.readUtil.isPort()) { // // IDCard idCard = activity.readUtil.readCard(); // if (idCard != null) { // // Bitmap headBitmap = BitmapFactory.decodeFile(idCard.getIdPhoto()); // // IdCard c = new IdCard(); // c.birthday = DateUtil.getTimeStamp(idCard.getIdDate(), "yyyyMMdd"); // c.cardNumber = idCard.getIdNum(); // c.gender = idCard.getIdSex(); // c.name = idCard.getIdName(); // c.headBitmap = headBitmap; //// c.featureData = featureData; // ByteArrayOutputStream stream = new ByteArrayOutputStream(); // headBitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream); // c.jpgData = stream.toByteArray(); // // // sendResult(c, activity); // // } // } else { // ToastUtil.show("身份证设备未连接"); // SystemClock.sleep(5000); // } // } } else { if (inRead) { inRead = false; if (isNewIDCardReader) { activity.readCardUtil.stopReadCard(); } else { Intent intent = new Intent(); intent.setPackage("com.basic.project.idcardservice"); intent.setAction("com.basic.read.IDCard"); intent.putExtra("type", "stop_service"); intent.setFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES); activity.startService(intent); } } } } catch (Exception e) { e.printStackTrace(); } SystemClock.sleep(200); } }); } public static void unRegisterBroadcast(Context context) { Intent intent = new Intent(); intent.setPackage("com.basic.project.idcardservice"); intent.setAction("com.basic.read.IDCard"); intent.putExtra("type", "close_service"); // intent.setFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES); context.startService(intent); try { if (IdCardManager.receiverRegisted) { context.unregisterReceiver(receiver); } } catch (Exception e) { e.printStackTrace(); } } }