xuxiuxi
2017-08-01 db2c035c3406d310af30094ec2bbf55ab396bf83
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package cn.com.basic.face.thread;
 
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
 
import com.bit.rfid.RFIDReader;
import com.bit.rfid.ReaderExtra;
import com.ivsign.android.IDCReader.IDCReaderSDK;
 
import org.xutils.http.RequestParams;
 
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
 
import cn.com.basic.face.base.BaseApplication;
import cn.com.basic.face.base.MainActivity;
import cn.com.basic.face.fragment.VisitorRegisterFragment;
import cn.com.basic.face.service.RegisterMng;
import cn.com.basic.face.util.IdCard;
 
import static cn.com.basic.face.util.FileUtil.writeToFile;
 
public class IdCardReaderThread extends Thread {
 
 
    @Override
    public void run() {
        while(true) {
            try{
                Thread.sleep(2000);
                try {
                    if (!VisitorRegisterFragment.getInstance().isVisible()) {
                        return;
                    }
 
                    RFIDReader idCardReader = RFIDReader.getInstance(BaseApplication.getInstance(), "uart");
                    idCardReader.setUartDeviceName("/dev/ttyS4");
                    ByteArrayInputStream type = new ByteArrayInputStream("uart".getBytes());
                    idCardReader.control(ReaderExtra.CONNECT, type);
 
                    idCardReader.open("id_card");
 
                    idCardReader.open("id_sam");
                    ByteArrayOutputStream baos = new ByteArrayOutputStream();
                    idCardReader.control(ReaderExtra.ID_READ_TEXT_PHOTO, baos);
                    String textPhoto = byteArrayToHexString(baos.toByteArray());
 
 
 
                    if (textPhoto != null) {
                        final String[] decodeInfo = new String[10];
                        final StringBuilder imagePath = new StringBuilder();
                        String path = MainActivity.getInstance().getFilesDir().getAbsolutePath();
                        IDCReaderSDK.initialize(path);
                        IDCReaderSDK.decodeSamAck(textPhoto, decodeInfo, imagePath);
 
//                        if (fragment_register_surveillance_photo_img == null) {
//        //                    MainActivity.getInstance().runOnUiThread(new Runnable() {
//        //                        @Override
//        //                        public void run() {
//        //                            Toast.makeText(BaseApplication.getInstance(),"请先选择人物照片", Toast.LENGTH_SHORT).show();
//        //                        }
//        //                    });
//                            //return;
//                        }
                        testComparePicturesWithJpeg(new IdCard(decodeInfo[0], decodeInfo[5], decodeInfo[3], decodeInfo[1], imagePath.toString()));
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
 
 
    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);
        }
    }
 
 
    private void testComparePicturesWithJpeg(IdCard idCard) {
 
        RequestParams params = new RequestParams();
 
        if (idCard.getHeadPath() != null && !"".equals(idCard.getHeadPath())) {
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inPreferredConfig = Bitmap.Config.ARGB_8888;
            Bitmap bitmap = BitmapFactory.decodeFile(idCard.getHeadPath(), options);
 
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream);
            byte[] imageBytes = byteArrayOutputStream.toByteArray();
 
            params.addBodyParameter("surveillancePhotoW",bitmap.getWidth()+"");
            params.addBodyParameter("surveillancePhotoH",bitmap.getWidth()+"");
            params.addBodyParameter("surveillancePhoto", writeToFile("surveillancePhoto", imageBytes));
        } else {
            params.addBodyParameter("surveillancePhotoW", "0");
            params.addBodyParameter("surveillancePhotoH", "0");
            params.addBodyParameter("surveillancePhoto", writeToFile("surveillancePhoto", new byte[]{}));
        }
 
 
        if (idCard.getHeadPath() != null && !"".equals(idCard.getHeadPath())) {
 
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inPreferredConfig = Bitmap.Config.ARGB_8888;
            Bitmap bitmap = BitmapFactory.decodeFile(idCard.getHeadPath(), options);
 
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream);
            byte[] imageBytes = byteArrayOutputStream.toByteArray();
 
            params.addBodyParameter("idCardPhotoW",bitmap.getWidth()+"");
            params.addBodyParameter("idCardPhotoH",bitmap.getWidth()+"");
            params.addBodyParameter("idCardPhoto", writeToFile("idCardPhoto", imageBytes));
        } else {
            params.addBodyParameter("idCardPhotoW","0");
            params.addBodyParameter("idCardPhotoH","0");
            params.addBodyParameter("idCardPhoto", writeToFile("idCardHeadPhoto", new byte[]{}));
        }
 
        RegisterMng.getInstance().compareSurveillancePhotoAndIdCardPhoto(params, idCard);
    }
 
}