左晓为主开发手持机充值管理机
zuoxiao
2025-03-06 2e139af1d444c02efe248660174409abd2b713db
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
package com.kernal.passportreader.sdk.utils;
 
import android.app.Service;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.IBinder;
 
import java.io.File;
import java.io.FileNotFoundException;
 
import kernal.idcard.android.RecogParameterMessage;
import kernal.idcard.android.RecogService;
import kernal.idcard.android.ResultMessage;
import kernal.idcard.camera.CardOcrRecogConfigure;
import kernal.idcard.camera.IBaseReturnMessage;
 
/**
 * Time:2018/12/20
 * Author:A@H
 * Description: 和相机不在同一界面的导入图片识别
 */
public class ImportRecog {
 
    private Context context;
    private int ReturnAuthority;
    public RecogService.recogBinder recogBinder;
    private int nMainId=2;
    private int nSubId=0;
    private volatile IBaseReturnMessage iBaseReturnMessage;
    private String selectPath="";
    private String cutSavePath="";
    private String HeadPicPath="";
    private String fullPicPath="";
    private Bitmap bitmap;
    public ImportRecog(Context context,IBaseReturnMessage iBaseReturnMessage){
        this.context=context;
        this.iBaseReturnMessage=iBaseReturnMessage;
    }
    public void setiBaseReturnMessage(IBaseReturnMessage iBaseReturnMessage) {
        this.iBaseReturnMessage = iBaseReturnMessage;
    }
 
 
    public ServiceConnection recogConn = new ServiceConnection() {
 
        @Override
        public void onServiceDisconnected(ComponentName name) {
            recogBinder = null;
        }
 
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
 
            recogBinder = (RecogService.recogBinder) service;
            if(recogBinder!=null) {
                RecogParameterMessage rpm = new RecogParameterMessage();
                rpm.nTypeLoadImageToMemory = 0;
                rpm.nMainID =nMainId;
                rpm.nSubID[0] = nSubId;
                rpm.GetSubID = true;
                rpm.GetVersionInfo = true;
                rpm.logo = "";
                rpm.userdata = "";
                rpm.sn = "";
                rpm.authfile = "";
                rpm.isCut = true;
                rpm.triggertype = 0;
                rpm.devcode = Devcode.devcode;
                rpm.isSetIDCardRejectType=CardOcrRecogConfigure.getInstance().isSetIDCardRejectType;
                //nProcessType:0- deleting all instructions 1- cropping 2- rotation  3- cropping and rotation
                // 4- tilt correction 5- cropping+ tilt correction 6- rotation+tile correct
                // 7- cropping+rotation+tilt correction.
                rpm.nProcessType = 7;
                rpm.isOpenIDCopyFuction = CardOcrRecogConfigure.getInstance().isOpenIDCopyFuction;
                rpm.nSetType = 1;// nSetType: 0-Deleting operations,1-setting operations
                if(selectPath!=null&&!selectPath.equals("")){
                    rpm.lpFileName = selectPath; // If rpm.lpFileName is null, auomatic recognition function will be executed.
                }else if(bitmap!=null){
                    rpm.bitmap=bitmap;//Reserve
                    selectPath=fullPicPath;
                }
                rpm.lpHeadFileName =HeadPicPath ;//Save portrait of identity document
                rpm.isSaveCut = CardOcrRecogConfigure.getInstance().isSaveCut;// Save cropping picture false=not saving  true=saving
                rpm.cutSavePath=cutSavePath;
                if (nMainId== 2) {
                    rpm.isAutoClassify = true;
                    rpm.isOnlyClassIDCard = true;
                } else if (nMainId== 3000) {
                    rpm.nMainID = 1034;
                }
                // end
                try {
                    ResultMessage resultMessage;
                    resultMessage = recogBinder.getRecogResult(rpm);
                    if (resultMessage.ReturnAuthority == 0
                            && resultMessage.ReturnInitIDCard == 0
                            && resultMessage.ReturnLoadImageToMemory == 0
                            && resultMessage.ReturnRecogIDCard > 0) {
                        if(iBaseReturnMessage!=null){
                            String[] picPath={selectPath,cutSavePath,HeadPicPath};
                            iBaseReturnMessage.scanOCRIdCardSuccess(resultMessage,picPath);
                        }
                    } else {
                        if(iBaseReturnMessage!=null){
                            String error=ManageIDCardRecogResult.managerErrorRecogResult(context,resultMessage);
                            String[] picPath={selectPath};
                            iBaseReturnMessage.scanOCRIdCardError(error,picPath);
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    if(bitmap!=null&&!bitmap.isRecycled()){
                        bitmap.isRecycled();
                        bitmap=null;
                    }
                    if (recogBinder != null) {
                        context.unbindService(recogConn);
                    }
                }
 
            }
        }
    };
 
    public void startImportRecogService(String selectPath){
        this.selectPath=selectPath;
        if(CardOcrRecogConfigure.getInstance().isSaveCut){
            cutSavePath=CardOcrRecogConfigure.getInstance().savePath.getCropPicPath();
        }
        if(CardOcrRecogConfigure.getInstance().isSaveHeadPic){
            HeadPicPath=CardOcrRecogConfigure.getInstance().savePath.getHeadPicPath();
        }
        nMainId=CardOcrRecogConfigure.getInstance().nMainId;
        nSubId=CardOcrRecogConfigure.getInstance().nSubID;
        RecogService.nMainID =CardOcrRecogConfigure.getInstance().nMainId;
        RecogService.isRecogByPath = true;
        Intent recogIntent = new Intent(context,
                RecogService.class);
        context.bindService(recogIntent, recogConn, Service.BIND_AUTO_CREATE);
    }
    public void startImportRecogService(Uri uri,Context context) throws FileNotFoundException {
        this.bitmap= BitmapFactory.decodeFileDescriptor(context.getContentResolver().openFileDescriptor(uri,"r").getFileDescriptor());
        if(CardOcrRecogConfigure.getInstance().isSaveCut){
            cutSavePath=CardOcrRecogConfigure.getInstance().savePath.getCropPicPath();
        }
        if(CardOcrRecogConfigure.getInstance().isSaveHeadPic){
            HeadPicPath=CardOcrRecogConfigure.getInstance().savePath.getHeadPicPath();
        }
        if(CardOcrRecogConfigure.getInstance().isSaveFullPic){
            fullPicPath=CardOcrRecogConfigure.getInstance().savePath.getFullPicPath();
            ActivityRecogUtils.copyFile(context,uri,new File(fullPicPath));
        }
        nMainId=CardOcrRecogConfigure.getInstance().nMainId;
        nSubId=CardOcrRecogConfigure.getInstance().nSubID;
        RecogService.nMainID =CardOcrRecogConfigure.getInstance().nMainId;
        RecogService.isRecogByPath = true;
        Intent recogIntent = new Intent(context,
                RecogService.class);
        context.bindService(recogIntent, recogConn, Service.BIND_AUTO_CREATE);
    }
}