左晓为主开发手持机充值管理机
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
package com.kernal.passportreader.sdk;
 
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.Service;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Vibrator;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.Toast;
 
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
 
import com.kernal.passportreader.sdk.utils.CardScreenUtil;
import com.kernal.passportreader.sdk.view.ScanCardsView;
import com.kernal.passportreader.sdk.view.ViewfinderView;
 
import kernal.idcard.android.ResultMessage;
import kernal.idcard.camera.IScanReturnMessage;
import kernal.idcard.camera.UritoPathUtil;
 
/**
 * @author A@H
 * @describle 相机界面,主要识别结果的获取以及识别完成之后的跳转
 */
public class CardsCameraActivity extends AppCompatActivity implements IScanReturnMessage, View.OnClickListener {
    RelativeLayout relativeLayout;
    RelativeLayout.LayoutParams imageButton_flash_params, imageButton_camera_params, imageButton_back_params, imageButton_spot_dection_params, imageButton_ejct_params;
    ImageButton imageButton_flash, imageButton_camera, imageButton_back, imageButton_spot_dection, imageButton_ejct;
    ScanCardsView scanICamera;
    private boolean isOpenFlash = false;
    private int width, height;
    private boolean isOpendetectLightspot = false;
 
 
    /**
     * 扫描识别成功,界面的跳转(回调接口)
     *
     * @param resultMessage 识别结果
     * @param picPath       图片路径数组,picPath[0]: 全图路径;picPath[1]: 裁切图;picPath[2]: 证件头像
     */
    @Override
    public void scanOCRIdCardSuccess(ResultMessage resultMessage, String[] picPath) {
        Vibrator mVibrator = (Vibrator) getApplication().getSystemService(
                Service.VIBRATOR_SERVICE);
        mVibrator.vibrate(200);
        Intent intent = new Intent();
        Bundle bundle = new Bundle();
        bundle.putSerializable("resultMessage", resultMessage);
        bundle.putStringArray("picpath", picPath);
        intent.putExtra("resultbundle", bundle);
        this.setResult(Activity.RESULT_OK, intent);
        this.finish();
    }
 
    /**
     * 扫描识别失败,界面的跳转(回调接口)
     *
     * @param error   -10601 开发码错误,把该文件中的{@link com.kernal.passportreader.sdk.utils.Devcode}中的devcode替换
     *                -10602 applicationId错误,把build.gradle文件中的 applicationId修改为授权文件中绑定的信息
     *                -10603 授权到期,请从新申请授权
     *                -10605  string.xml中的app_name字段属性和授权文件中绑定的不一致
     *                -10606  string.xml中company_name字段属性和授权文件中绑定的不一致
     *                -10608  string.xml中缺少company_name字段,请添加该字段
     * @param picPath 该数组存储的是拍照识别失败时的全图路径
     */
    @Override
    public void scanOCRIdCardError(String error, String[] picPath) {
        Intent intent = new Intent();
        intent.putExtra("error", error);
        intent.putExtra("strpicpath", picPath[0]);
        this.setResult(Activity.RESULT_OK, intent);
        this.finish();
    }
 
 
    @Override
    public void authOCRIdCardSuccess(String result) {
 
    }
 
    /**
     * 授权失败
     *
     * @param error
     * @see
     */
    @Override
    public void authOCRIdCardError(String error) {
        Intent intent = new Intent();
        intent.putExtra("error", error);
        this.setResult(Activity.RESULT_OK, intent);
        this.finish();
    }
 
 
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 9 && resultCode == Activity.RESULT_OK) {
            Uri uri = data.getData();
            scanICamera.importPicRecog(UritoPathUtil.getImageAbsolutePath(getApplicationContext(), uri));
        }
    }
 
    @Override
    public void onClick(View v) {
        //返回事件
        if (v == imageButton_back) {
            this.finish();
            //拍照事件
        } else if (v == imageButton_camera) {
            scanICamera.takePicRecog();
            //闪光灯
        } else if (v == imageButton_flash) {
            if (isOpenFlash) {
                isOpenFlash = false;
                scanICamera.managerFlashLight(isOpenFlash);
                imageButton_flash.setBackgroundResource(R.mipmap.flash_off);
            } else {
                isOpenFlash = true;
                scanICamera.managerFlashLight(isOpenFlash);
                imageButton_flash.setBackgroundResource(R.mipmap.flash_on);
            }
            //隐藏、显示拍照事件
        } else if (v == imageButton_ejct) {
            imageButton_ejct.setVisibility(View.GONE);
            imageButton_camera.setVisibility(View.VISIBLE);
            // 光斑检测事件
        } else if (v == imageButton_spot_dection) {
            if (isOpendetectLightspot) {
                Toast.makeText(this, getString(R.string.closeddetectLightspot),
                        Toast.LENGTH_SHORT).show();
                isOpendetectLightspot = false;
                imageButton_spot_dection.setBackgroundResource(R.mipmap.spot_dection_off);
                scanICamera.managerSpotDection(isOpendetectLightspot);
            } else {
                Toast.makeText(this, getString(R.string.opendetectLightspot),
                        Toast.LENGTH_SHORT).show();
                isOpendetectLightspot = true;
                imageButton_spot_dection.setBackgroundResource(R.mipmap.spot_dection_on);
                scanICamera.managerSpotDection(isOpendetectLightspot);
            }
        }
    }
 
    @Override
    @TargetApi(16)
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        hideBottomUIMenu();
        setContentView(R.layout.activity_idcardscan);
        width = CardScreenUtil.getScreenResolution(this).x;
        height = CardScreenUtil.getScreenResolution(this).y;
        // WriteUtil.writeLog("该设备的屏幕分辨率为:"+width+"X"+height);
        relativeLayout = findViewById(R.id.camera_layout);
        scanICamera = new ScanCardsView(this);
        if (CardScreenUtil.getScreenOrientation(this) == CardScreenUtil.ORIENTATION_LANDSCAPE) {
            //闪光灯
            imageButton_flash = new ImageButton(this);
            imageButton_flash.setBackground(getResources().getDrawable(R.mipmap.flash_off));
            imageButton_flash_params = new RelativeLayout.LayoutParams((int) (width * 0.05), (int) (width * 0.05));
            imageButton_flash_params.addRule(RelativeLayout.ALIGN_PARENT_TOP, R.id.camera_layout);
            imageButton_flash_params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, R.id.camera_layout);
            imageButton_flash_params.leftMargin = (int) (width * 0.02);
            imageButton_flash_params.topMargin = (int) (width * 0.02);
 
            //拍照按钮
            imageButton_camera = new ImageButton(this);
            imageButton_camera.setBackground(getResources().getDrawable(R.mipmap.tack_pic_btn));
            imageButton_camera_params = new RelativeLayout.LayoutParams((int) (width * 0.1),
                    (int) (width * 0.1));
            imageButton_camera_params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, R.id.camera_layout);
            imageButton_camera_params.addRule(RelativeLayout.CENTER_VERTICAL);
            imageButton_camera_params.rightMargin = (int) (width * 0.02);
            imageButton_camera.setVisibility(View.GONE);
 
            //返回按钮
            imageButton_back = new ImageButton(this);
            imageButton_back.setBackground(getResources().getDrawable(R.mipmap.camera_back_nomal));
            imageButton_back_params = new RelativeLayout.LayoutParams((int) (width * 0.05), (int) (width * 0.05));
            imageButton_back_params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, R.id.camera_layout);
            imageButton_back_params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, R.id.camera_layout);
            imageButton_back_params.leftMargin = (int) (width * 0.02);
            imageButton_back_params.bottomMargin = (int) (width * 0.02);
            //光斑检测按钮
            imageButton_spot_dection = new ImageButton(this);
            imageButton_spot_dection.setBackgroundResource(R.mipmap.spot_dection_off);
            imageButton_spot_dection_params = new RelativeLayout.LayoutParams((int) (width * 0.08), (int) (width * 0.08));
            imageButton_spot_dection_params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, R.id.camera_layout);
            imageButton_spot_dection_params.addRule(RelativeLayout.CENTER_VERTICAL);
            imageButton_spot_dection_params.leftMargin = (int) (width * 0.02);
 
            //隐藏拍照按钮
            imageButton_ejct = new ImageButton(this);
            imageButton_ejct.setBackground(getResources().getDrawable(R.mipmap.locker_btn_def));
            imageButton_ejct_params = new RelativeLayout.LayoutParams((int) (height * 0.05), (int) (height * 0.5));
            imageButton_ejct_params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, R.id.camera_layout);
            imageButton_ejct_params.addRule(RelativeLayout.CENTER_VERTICAL);
 
        } else {
            //竖屏状态下框的方向 true:width>height; false:height>width
            //ViewfinderView.isSameScreen = false;
            //闪光灯
            imageButton_flash = new ImageButton(this);
            imageButton_flash.setBackground(getResources().getDrawable(R.mipmap.flash_off));
            imageButton_flash_params = new RelativeLayout.LayoutParams((int) (height * 0.05), (int) (height * 0.05));
            imageButton_flash_params.addRule(RelativeLayout.ALIGN_PARENT_TOP, R.id.camera_layout);
            imageButton_flash_params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, R.id.camera_layout);
            imageButton_flash_params.rightMargin = (int) (height * 0.02);
            imageButton_flash_params.topMargin = (int) (height * 0.02);
 
            //拍照按钮
            imageButton_camera = new ImageButton(this);
            imageButton_camera.setBackground(getResources().getDrawable(R.mipmap.tack_pic_btn));
            imageButton_camera_params = new RelativeLayout.LayoutParams((int) (height * 0.1),
                    (int) (height * 0.1));
            imageButton_camera_params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, R.id.camera_layout);
            imageButton_camera_params.addRule(RelativeLayout.CENTER_HORIZONTAL);
            imageButton_camera_params.bottomMargin = (int) (height * 0.02);
            imageButton_camera.setVisibility(View.GONE);
 
            //返回按钮
            imageButton_back = new ImageButton(this);
            imageButton_back.setBackground(getResources().getDrawable(R.mipmap.camera_back_nomal));
            imageButton_back_params = new RelativeLayout.LayoutParams((int) (height * 0.05), (int) (height * 0.05));
            imageButton_back_params.addRule(RelativeLayout.ALIGN_PARENT_TOP, R.id.camera_layout);
            imageButton_back_params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, R.id.camera_layout);
            imageButton_back_params.leftMargin = (int) (height * 0.02);
            imageButton_back_params.topMargin = (int) (height * 0.02);
            //光斑检测按钮
            imageButton_spot_dection = new ImageButton(this);
            imageButton_spot_dection.setBackgroundResource(R.mipmap.spot_dection_off);
            imageButton_spot_dection_params = new RelativeLayout.LayoutParams((int) (height * 0.08), (int) (height * 0.08));
            imageButton_spot_dection_params.addRule(RelativeLayout.ALIGN_PARENT_TOP, R.id.camera_layout);
            imageButton_spot_dection_params.addRule(RelativeLayout.CENTER_HORIZONTAL);
            imageButton_spot_dection_params.topMargin = (int) (height * 0.02);
 
            //隐藏拍照按钮
            imageButton_ejct = new ImageButton(this);
            imageButton_ejct.setBackground(getResources().getDrawable(R.mipmap.locker_btn_def_p));
            imageButton_ejct_params = new RelativeLayout.LayoutParams((int) (width * 0.5), (int) (width * 0.05));
            imageButton_ejct_params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, R.id.camera_layout);
            imageButton_ejct_params.addRule(RelativeLayout.CENTER_HORIZONTAL);
        }
        relativeLayout.addView(scanICamera, new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        relativeLayout.addView(imageButton_flash, imageButton_flash_params);
        relativeLayout.addView(imageButton_camera, imageButton_camera_params);
        relativeLayout.addView(imageButton_back, imageButton_back_params);
        relativeLayout.addView(imageButton_spot_dection, imageButton_spot_dection_params);
        relativeLayout.addView(imageButton_ejct, imageButton_ejct_params);
        imageButton_camera.setOnClickListener(this);
        imageButton_back.setOnClickListener(this);
        imageButton_flash.setOnClickListener(this);
        imageButton_ejct.setOnClickListener(this);
        imageButton_spot_dection.setOnClickListener(this);
    }
 
    @Override
    protected void onResume() {
        super.onResume();
        scanICamera.setIScan(this);
        scanICamera.startCamera();
 
    }
 
    @Override
    protected void onPause() {
        super.onPause();
 
        scanICamera.stopCamera();
 
    }
 
    @Override
    protected void onDestroy() {
        super.onDestroy();
        scanICamera.destroyService();
    }
 
    @Override
    public void openCameraError(String error) {
        Log.i("string", "失败的信息" + error);
 
    }
 
 
    /**
     * 隐藏虚拟按键,并且全屏
     */
    protected void hideBottomUIMenu() {
        //隐藏虚拟按键,并且全屏
        if (Build.VERSION.SDK_INT > 11 && Build.VERSION.SDK_INT < 19) { // lower api
            View v = this.getWindow().getDecorView();
            v.setSystemUiVisibility(View.GONE);
        } else if (Build.VERSION.SDK_INT >= 19) {
            //for new api versions.
            View decorView = getWindow().getDecorView();
            int uiOptions = View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION // hide nav bar
//                    | View.SYSTEM_UI_FLAG_FULLSCREEN // hide status bar
                    | View.SYSTEM_UI_FLAG_IMMERSIVE;
            decorView.setSystemUiVisibility(uiOptions);
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        }
    }
 
}