xuxiuxi
2017-06-30 87573f233c30ad88495d62d39474dc1893484c0f
VisitFace/DemoForBsk/app/src/main/java/cn/com/basic/face/fragment/RegisterFragment.java
@@ -1,6 +1,7 @@
package cn.com.basic.face.fragment;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
@@ -21,6 +22,8 @@
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.bit.rfid.RFIDReader;
import com.bit.rfid.ReaderExtra;
import com.bsk.zhangbo.demoforbsk.R;
import cn.com.basic.face.adapter.SurveillanceFragment;
@@ -50,13 +53,19 @@
import cn.com.basic.face.widget.register.RegisterRightFieldTextView;
import cn.com.basic.face.dialog.SelectDialog;
import com.ivsign.android.IDCReader.IDCReaderSDK;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;
import org.xutils.common.util.FileUtil;
import org.xutils.http.RequestParams;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.Reader;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
@@ -138,9 +147,32 @@
        resetForm();
    }
    private File writeToFile(String fileName, byte[] fileBytes) {
        try {
            String dir = MainActivity.getInstance().getFilesDir().getAbsolutePath();
            File file = new File(dir, fileName);
            if (file.exists()) {
                file.delete();
            }
            file.createNewFile();
            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File(dir, fileName)));
            bos.write(fileBytes);
            bos.flush();
            bos.close();
            return new File(dir, fileName);
        }catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    @OnClick(R.id.fragment_register_save)
    public void fragment_register_save_click(View v) {
//        RegisterMng.getInstance().add(null);
//        if (1==1) {
//            return;
//        }
        if (!BaseApplication.deviceAvailable) {
            return;
@@ -226,6 +258,17 @@
        pd.addValue(Register.FieldNames.birthday, birthday);
        params.addBodyParameter(Register.FieldNames.username, name);
        params.addBodyParameter(Register.FieldNames.mobilePhone, phone);
        params.addBodyParameter(Register.FieldNames.genderId, DictionaryMng.getInstance().getId(gender));
        params.addBodyParameter(Register.FieldNames.identityTypeId, DictionaryMng.getInstance().getId(id_class));
        params.addBodyParameter(Register.FieldNames.countryId, DictionaryMng.getInstance().getId(country));
        params.addBodyParameter(Register.FieldNames.visitorTypeId, DictionaryMng.getInstance().getId(visitor_class));
        params.addBodyParameter(Register.FieldNames.identifyNum, id_num);
        params.addBodyParameter(Register.FieldNames.companyId, company);
        params.addBodyParameter(Register.FieldNames.birthday, birthday);
        if (surveillanceQueryItem_selectPhoto != null) {
            params.addBodyParameter("width", surveillanceQueryItem_selectPhoto.getWidth());
            params.addBodyParameter("height", surveillanceQueryItem_selectPhoto.getHeight());
@@ -237,6 +280,10 @@
                imageBytes[i] = nativeImg.image[i];
            }
            pd.addData("rawSurveillancePhoto", "application/octet-stream", imageBytes);
            params.addBodyParameter("rawSurveillancePhoto", writeToFile("rawSurveillancePhoto", imageBytes));
        } else {
            params.addBodyParameter("rawSurveillancePhoto", writeToFile("rawSurveillancePhoto", new byte[]{}));
        }
        if (fragment_register_surveillance_photo_img != null) {
@@ -244,6 +291,10 @@
            fragment_register_surveillance_photo_img.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream);
            byte[] imageBytes = byteArrayOutputStream.toByteArray();
            pd.addData(Register.FieldNames.surveillancePhoto, "application/octet-stream", imageBytes);
            params.addBodyParameter("surveillancePhoto", writeToFile("surveillancePhoto", imageBytes));
        } else {
            params.addBodyParameter("surveillancePhoto", writeToFile("surveillancePhoto", new byte[]{}));
        }
        if (fragment_register_upload_custom_photo_img != null) {
@@ -251,6 +302,10 @@
            fragment_register_upload_custom_photo_img.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream);
            byte[] imageBytes = byteArrayOutputStream.toByteArray();
            pd.addData(Register.FieldNames.uploadCustomPhoto, "application/octet-stream", imageBytes);
            params.addBodyParameter("uploadCustomPhoto", writeToFile("uploadCustomPhoto", imageBytes));
        } else {
            params.addBodyParameter("uploadCustomPhoto", writeToFile("uploadCustomPhoto", new byte[]{}));
        }
        if (fragment_register_id_card_photo_img != null) {
@@ -258,6 +313,10 @@
            fragment_register_id_card_photo_img.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream);
            byte[] imageBytes = byteArrayOutputStream.toByteArray();
            pd.addData(Register.FieldNames.idCardPhoto, "application/octet-stream", imageBytes);
            params.addBodyParameter("idCardPhoto", writeToFile("idCardPhoto", imageBytes));
        } else {
            params.addBodyParameter("idCardPhoto", writeToFile("idCardPhoto", new byte[]{}));
        }
@@ -266,8 +325,8 @@
            @Override
            protected String doInBackground(String... urls) {
                try {
                    Uploader uploader = new Uploader("", AppApi.BASE_URL + AppApi.REGISTER_ADD);
                    json = uploader.upload("", pd);
//                    Uploader uploader = new Uploader("", AppApi.BASE_URL + AppApi.REGISTER_ADD);
//                    json = uploader.upload("", pd);
                    return json;
                } catch (Exception e) {
                    e.printStackTrace();
@@ -286,9 +345,11 @@
            }
        }
        ConnectionTask task = new ConnectionTask();
        String[] params1 = new String[2];
        task.execute(params1);
//        ConnectionTask task = new ConnectionTask();
//        String[] params1 = new String[2];
//        task.execute(params1);
        RegisterMng.getInstance().add(params);
    }
    public void set_fragment_register_surveillance_photo(SurveillanceQueryItem item) {
@@ -557,6 +618,93 @@
    @Override
    protected void initData() {
        resetForm();
        //readIdCard();
    }
    public void readIdCard() {
        try {
            Thread.sleep(100);
            RFIDReader idCardReader = null; //RFIDReader.getInstance(MainActivity.getInstance(), "com");
            ByteArrayInputStream type = new ByteArrayInputStream("uart".getBytes());
            idCardReader.control(ReaderExtra.CONNECT, type);
            idCardReader.setUartDeviceName("/dev/S0");
            idCardReader.open("id_card");
            String cardId = byteArrayToHexString(idCardReader.getUid());
            idCardReader.open("id_sam");
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            idCardReader.control(ReaderExtra.ID_READ_TEXT_PHOTO, baos);
            String textPhoto = byteArrayToHexString(baos.toByteArray());
            if (textPhoto != null) {
                String[] decodeInfo = new String[10];
                StringBuilder imagePath = new StringBuilder();
                String path = MainActivity.getInstance().getFilesDir().getAbsolutePath();
                IDCReaderSDK.initialize(path);
                IDCReaderSDK.decodeSamAck(textPhoto, decodeInfo, imagePath);
                String name = decodeInfo[0];
                String sex = decodeInfo[1];
                String nation = decodeInfo[2];
                String birth = decodeInfo[3];
                String address = decodeInfo[4];
                String idNumber = decodeInfo[5];
                String office = decodeInfo[6];
                String validDate = decodeInfo[7] + "-" + decodeInfo[8];
                String photo = imagePath.toString();
                comparePictures();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    private void comparePictures() {
        RequestParams params = new RequestParams();
        if (fragment_register_surveillance_photo_img != null) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            fragment_register_surveillance_photo_img.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream);
            byte[] imageBytes = byteArrayOutputStream.toByteArray();
            params.addBodyParameter("surveillancePhoto", writeToFile("surveillancePhoto", imageBytes));
        } else {
            params.addBodyParameter("surveillancePhoto", writeToFile("surveillancePhoto", new byte[]{}));
        }
        if (fragment_register_id_card_photo_img != null) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            fragment_register_id_card_photo_img.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream);
            byte[] imageBytes = byteArrayOutputStream.toByteArray();
            params.addBodyParameter("idCardPhoto", writeToFile("idCardPhoto", imageBytes));
        } else {
            params.addBodyParameter("idCardPhoto", writeToFile("idCardPhoto", new byte[]{}));
        }
        RegisterMng.getInstance().compareSurveillancePhotoAndIdCardPhoto(params);
    }
    protected static final char[] a = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
    public static String byteArrayToHexString(byte[] bytes) {
        if(bytes == null) {
            return null;
        } else {
            char[] hexChars = new char[bytes.length * 2];
            for(int j = 0; j < bytes.length; ++j) {
                int v = bytes[j] & 255;
                hexChars[j * 2] = a[v >>> 4];
                hexChars[j * 2 + 1] = a[v & 15];
            }
            return new String(hexChars);
        }
    }
}