左晓为主开发手持机充值管理机
zuoxiao
2024-08-09 9450bcb197c2de53982368d5c2059b31abed3b87
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
package com.kernal.passportreader.sdk;
 
import android.content.Context;
import android.graphics.ImageFormat;
import android.graphics.Rect;
import android.graphics.YuvImage;
 
import com.kernal.passportreader.sdk.utils.CardScreenUtil;
import com.kernal.passportreader.sdk.utils.Devcode;
 
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
 
import kernal.idcard.android.Frame;
import kernal.idcard.android.RecogParameterMessage;
import kernal.idcard.android.RecogService;
import kernal.idcard.android.ResultMessage;
import kernal.idcard.camera.CardOcrRecogConfigure;
import kernal.idcard.camera.OcrIdCardRecogParams;
 
/**
 * @author A@H
 *预览识别线程
 */
public class OcrRecogTask implements Runnable {
    private int LoadBufferImage = -1;
    private int ConfirmSideSuccess = -1;
    private int CheckPicIsClear = -1;
    private OcrIdCardRecogParams ocrIdCardRecogParams;
    private Context context;
    private int nMainIDX;
    private int nCropType;
    private int nSubID;
    private String picPathString = "";
    private String HeadJpgPath = "";
    private String cutPicPath = "";
    private Frame frame = new Frame();
    public byte[] data;
    private int[] NV21Point = new int[4];
    private boolean isTakePic = false;
 
 
    private volatile boolean isOpendetectLightspot = false;
    private int detectLightspot = -2;
    public static String importPicPath = "";
 
    public OcrRecogTask(Context context, OcrIdCardRecogParams ocrIdCardRecogParams) {
        this.context = context;
        this.ocrIdCardRecogParams = ocrIdCardRecogParams;
        nMainIDX = CardOcrRecogConfigure.getInstance().nMainId;
        nSubID = CardOcrRecogConfigure.getInstance().nSubID;
        nCropType = CardOcrRecogConfigure.getInstance().nCropType;
 
    }
 
    public void setnMainIDX(int nMainIDX) {
        this.nMainIDX = nMainIDX;
    }
 
    public OcrRecogTask setData(byte[] data) {
        this.data = data;
        return this;
    }
 
    public OcrRecogTask setNv21Point(int[] Nv21Point) {
        NV21Point = Nv21Point;
        return this;
    }
 
    public OcrRecogTask setTakePic(boolean takePic) {
        isTakePic = takePic;
        return this;
    }
 
    public OcrRecogTask setOpendetectLightspot(boolean opendetectLightspot) {
        isOpendetectLightspot = opendetectLightspot;
        return this;
    }
 
    @Override
    public void run() {
        if (ocrIdCardRecogParams.recogBinder != null) {
            //拍照识别流程
            if (isTakePic) {
                if (RecogService.isRecogByPath) {
                    picPathString = importPicPath;
                } else {
                    picPathString = CardOcrRecogConfigure.getInstance().savePath.getFullPicPath();
                    RecogService.isRecogByPath = true;
                    saveFullPic(picPathString);
                }
                cutPicPath = CardOcrRecogConfigure.getInstance().savePath.getCropPicPath();
                HeadJpgPath = CardOcrRecogConfigure.getInstance().savePath.getHeadPicPath();
                ocrIdCardRecogParams.scanICamera.stopPreview();
                getRecogResult();
            } else {
                if (nMainIDX != 3000) {
                    /**
                     * 识别普通证件,视频流识别流程
                     */
                    //ocrIdCardRecogParams.recogBinder.SetParameter(1, nMainIDX);
                    //设置敏感区域的坐标点
                    ocrIdCardRecogParams.recogBinder.SetROI(NV21Point[0], NV21Point[1], NV21Point[2], NV21Point[3]);
                    //设置图片的旋转的方向
                    if (nCropType == 0) {
                        ocrIdCardRecogParams.recogBinder.SetRotateType(ocrIdCardRecogParams.Orientation);
                    } else {
                        ocrIdCardRecogParams.recogBinder.SetRotateType(0);
                    }
                    //设置视频流识别的方式
                    ocrIdCardRecogParams.recogBinder.SetVideoStreamCropTypeEx(nCropType);
                    //加载图片至核心
                    if (data != null && data.length > 0) {
                        LoadBufferImage = ocrIdCardRecogParams.recogBinder.LoadBufferImageEx(data,
                                ocrIdCardRecogParams.preWidth, ocrIdCardRecogParams.preHeight, 24, 0);
                    }
                    //设置清晰度阈值
                    if (nCropType == 1) {
                        ocrIdCardRecogParams.recogBinder.SetPixClearEx(40);
                    }
                    if (CardOcrRecogConfigure.getInstance().flag != 0) {
                        if (nMainIDX == 5 || nMainIDX == 6) {
                            ocrIdCardRecogParams.recogBinder.SetConfirmSideMethod(1);
                        } else if (nMainIDX == 13 || nMainIDX == 9
                                || nMainIDX == 10 || nMainIDX == 11 || nMainIDX == 12) {
                            ocrIdCardRecogParams.recogBinder.SetConfirmSideMethod(2);
                            ocrIdCardRecogParams.recogBinder.IsDetectRegionValid(1);
                        } else {
                            if (nMainIDX == 3 || nMainIDX == 2) {
                                ocrIdCardRecogParams.recogBinder.SetConfirmSideMethod(0);
                                ocrIdCardRecogParams.recogBinder.IsDetectRegionValid(1);
                                ocrIdCardRecogParams.recogBinder.IsDetect180Rotate(1);
                                ocrIdCardRecogParams.recogBinder.SetDetectIDCardType(CardOcrRecogConfigure.getInstance().flag);
                            } else {
                                ocrIdCardRecogParams.recogBinder.SetConfirmSideMethod(4);
                            }
                        }
                    }
                    if (LoadBufferImage == 0) {
                        //检测图片的边线
                        ConfirmSideSuccess = ocrIdCardRecogParams.recogBinder
                                .ConfirmSideLineEx(0);
                        if (ConfirmSideSuccess >= 0) {
                            detectLightspot=-2;
                            if (isOpendetectLightspot) {
                                //检测光斑
                                detectLightspot = ocrIdCardRecogParams.recogBinder.DetectLightspot();
                            }
                               //检测图片的清晰度
                            CheckPicIsClear = ocrIdCardRecogParams.recogBinder
                                    .CheckPicIsClearEx();
                        }
                    }
                    if (nCropType == 1) {
                        //智能检测边线,获取图片上的四个坐标点
                        ocrIdCardRecogParams.recogBinder.GetFourSideLines(frame);
                        //根据返回的图片的四个坐标点,换算成屏幕上的坐标点(注:屏幕上坐标点=图片上的坐标点*(屏幕的分辨率/图片的分辨率))
                    //    WriteUtil.writeLog("竖屏的坐标点,左("+frame.ltStartX+","+frame.ltStartY+")"+
                      //          "上("+frame.lbStartX+","+frame.lbStartY+")"+
                      //          "右("+frame.rtStartX+","+frame.rtStartY+")"+
                        //        "下("+frame.rbStartX+","+frame.rbStartY+")");
 
                            frame.ltStartX = (int) (frame.ltStartX * ocrIdCardRecogParams.scale);
                            frame.ltStartY = (int) (frame.ltStartY * ocrIdCardRecogParams.scale);
                            frame.lbStartX = (int) (frame.lbStartX * ocrIdCardRecogParams.scale);
                            frame.lbStartY = (int) (frame.lbStartY * ocrIdCardRecogParams.scale);
                            frame.rtStartX = (int) (frame.rtStartX * ocrIdCardRecogParams.scale);
                            frame.rtStartY = (int) (frame.rtStartY * ocrIdCardRecogParams.scale);
                            frame.rbStartX = (int) (frame.rbStartX * ocrIdCardRecogParams.scale);
                            frame.rbStartY = (int) (frame.rbStartY * ocrIdCardRecogParams.scale);
 
                    }
                    ocrIdCardRecogParams.scanICamera.UpdateFrame(frame, ConfirmSideSuccess,detectLightspot);
                    //图片加载成功&&检测边线成功&&检测清晰度成功&&没有光斑,进行识别
                    if (LoadBufferImage == 0 && ConfirmSideSuccess >=0
                            && CheckPicIsClear == 0 && detectLightspot != 0) {
                        picPathString = CardOcrRecogConfigure.getInstance().savePath.getFullPicPath();
                        cutPicPath = CardOcrRecogConfigure.getInstance().savePath.getCropPicPath();
                        if (CardOcrRecogConfigure.getInstance().isSaveHeadPic) {
                            HeadJpgPath = CardOcrRecogConfigure.getInstance().savePath.getHeadPicPath();
                        }
                        RecogService.isRecogByPath = false;
                        getRecogResult();
                    } else {
                        ocrIdCardRecogParams.scanICamera.addPreviewCallBack();
                    }
                } else {
                    /**
                     * 识别机读码
                     */
                    ocrIdCardRecogParams.recogBinder.SetROI(NV21Point[0], NV21Point[1], NV21Point[2], NV21Point[3]);
                    if (CardScreenUtil.getScreenOrientation(context) == CardScreenUtil.ORIENTATION_PORTRAIT) {
                        ocrIdCardRecogParams.recogBinder.SetRotateType(1);
                    } else {
                        ocrIdCardRecogParams.recogBinder.SetRotateType(0);
                    }
                    LoadBufferImage = ocrIdCardRecogParams.recogBinder.LoadBufferImageEx(data,
                            ocrIdCardRecogParams.preWidth, ocrIdCardRecogParams.preHeight, 24, 0);
                    if (LoadBufferImage == 0) {
                        ConfirmSideSuccess = ocrIdCardRecogParams.recogBinder.ConfirmSideLineEx(0);
                        if (ConfirmSideSuccess == 1034
                                || ConfirmSideSuccess == 1033
                                || ConfirmSideSuccess == 1036) {
                            CheckPicIsClear = ocrIdCardRecogParams.recogBinder.CheckPicIsClearEx();
                        }
                    }
                    boolean isConfirmSuccess = (ConfirmSideSuccess == 1034 || ConfirmSideSuccess == 1033 || ConfirmSideSuccess == 1036)
                            && CheckPicIsClear == 0;
                    if (isConfirmSuccess) {
                        nMainIDX = ConfirmSideSuccess;
                        picPathString = CardOcrRecogConfigure.getInstance().savePath.getFullPicPath();
                        cutPicPath = CardOcrRecogConfigure.getInstance().savePath.getCropPicPath();
                        getRecogResult();
                    } else {
                        ocrIdCardRecogParams.scanICamera.addPreviewCallBack();
                    }
                }
            }
        } else {
            ocrIdCardRecogParams.scanICamera.addPreviewCallBack();
        }
    }
 
    public void getRecogResult() {
        ResultMessage resultMessage;
        RecogParameterMessage rpm = new RecogParameterMessage();
        rpm.nTypeLoadImageToMemory = 0;
        rpm.nMainID = nMainIDX;
        rpm.nSubID[0] = nSubID;
        rpm.GetSubID = true;
        rpm.GetVersionInfo = true;
        rpm.logo = "";
        rpm.userdata = "";
        rpm.sn = "";
        rpm.authfile = "";
        rpm.isSaveCut = CardOcrRecogConfigure.getInstance().isSaveCut;
        rpm.triggertype = 0;
        rpm.devcode = Devcode.devcode;
        rpm.isOnlyClassIDCard = true;
        rpm.isOpenGetThaiFeatureFuction = CardOcrRecogConfigure.getInstance().isOpenGetThaiFeatureFuction;
        rpm.isOpenIDCopyFuction = CardOcrRecogConfigure.getInstance().isOpenIDCopyFuction;
        rpm.isSetIDCardRejectType = CardOcrRecogConfigure.getInstance().isSetIDCardRejectType;
        // rpm.idcardRotateDegree=3;
        if (nMainIDX == 2) {
            rpm.isAutoClassify = true;
            rpm.lpHeadFileName = HeadJpgPath;//  save document protrait
            rpm.lpFileName = picPathString; //  If rpm.lpFileName is null, automatic recognition fuction will be executed
            rpm.cutSavePath = cutPicPath;
        } else {
            rpm.lpHeadFileName = HeadJpgPath;// save document protrait
            rpm.lpFileName = picPathString; //  If rpm.lpFileName is null, automatic recognition fuction will be executed.
            rpm.cutSavePath = cutPicPath;
        }
        rpm.isCut = false;
        try {
            resultMessage = ocrIdCardRecogParams.recogBinder.getRecogResult(rpm);
            if (resultMessage.ReturnAuthority == 0
                    && resultMessage.ReturnInitIDCard == 0
                    && resultMessage.ReturnLoadImageToMemory == 0
                    && resultMessage.ReturnRecogIDCard > 0) {
 
                if (!isTakePic&&CardOcrRecogConfigure.getInstance().isSaveFullPic) {
                    saveFullPic(picPathString);
                }else if(!CardOcrRecogConfigure.getInstance().isSaveFullPic){
                    deleteFullPic(picPathString);
                }
                String[] picPath = {picPathString, cutPicPath, HeadJpgPath};
                ocrIdCardRecogParams.scanICamera.recogSucessUpdateUi(resultMessage, picPath);
            } else {
                //识别返回错误值 非拍照-6 继续预览,拍照-6,finish()掉相机
                     String[] picPath={""};
                    if(!CardOcrRecogConfigure.getInstance().isSaveFullPic){
                        deleteFullPic(picPathString);
                    }else{
                        saveFullPic(picPathString);
                        picPath[0]=picPathString;
                    }
                    ocrIdCardRecogParams.scanICamera.recogErrorUpdateUi(resultMessage,picPath);
            }
 
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            RecogService.isRecogByPath = false;
        }
    }
 
 
    private void deleteFullPic(String picPathString){
        if(picPathString!=null&&!picPathString.equals("")){
            File file=new File(picPathString);
            if(file.exists()){
                file.delete();
            }
        }
 
 
    }
    /**
     * store the full picture
     *
     * @param picPathString1
     */
    private void saveFullPic(String picPathString1) {
        // TODO Auto-generated method stub
        //store the full picture start
        YuvImage yuvimage = new YuvImage(data, ImageFormat.NV21, ocrIdCardRecogParams.preWidth,
                ocrIdCardRecogParams.preHeight, null);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        yuvimage.compressToJpeg(new Rect(0, 0, ocrIdCardRecogParams.preWidth,
                        ocrIdCardRecogParams.preHeight),
                100, baos);
 
        FileOutputStream outStream = null;
        try {
            outStream = new FileOutputStream(picPathString1);
            outStream.write(baos.toByteArray());
        } catch (IOException e) {
            // TODO Auto-generated catch
            // block
            e.printStackTrace();
        } finally {
            try {
                outStream.close();
                baos.close();
            } catch (Exception e) {
            }
        }
    }
}