赞
踩
用的是zxing库:是google推出的一个开源的二维码框架,可以实现使用手机的摄像头完成二维码的扫描和解码
android.Manifest.permission.CAMERA
//AndroidX 版本
implementation 'com.king.zxing:zxing-lite:1.1.7-androidx'
<activity
android:name="com.king.zxing.CaptureActivity"
android:screenOrientation="portrait"
android:theme="@style/CaptureTheme"/>
注意
public static final int REQUEST_CODE_SCAN = 0X02;
//点击事件
mineScanCode.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//显示扫描二维码框
ActivityOptionsCompat optionsCompat = ActivityOptionsCompat.makeCustomAnimation(this, R.anim.empty, R.anim.empty);
Intent intent = new Intent(this, CaptureActivity.class);
ActivityCompat.startActivityForResult(this, intent, REQUEST_CODE_SCAN, optionsCompat.toBundle());
}
});
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
</set>
@Override public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (resultCode == RESULT_OK && data != null) { switch (requestCode) { case REQUEST_CODE_SCAN: String result = data.getStringExtra(Intents.Scan.RESULT); //implementation 'com.alibaba:fastjson:1.2.31' //json 用的是阿里巴巴的 //判断json格式 if (isJSON(result)){//是否是json格式 JSONObject object = JSONObject.parseObject(result); }else { Toast.makeText(mContext,"不是JSON格式",Toast.LENGTH_SHORT).show(); return; } break; } } } //判断json格式 public static boolean isJSON(String str) { boolean result = false; try { Object obj= JSON.parse(str); result = true; } catch (Exception e) { result=false; } return result; }
jcenter()
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。