【Android】不跳转直接在当前页面扫描二维码(前置、后置摄像)
1、在build.gradle添加依赖
2、activity_main布局文件
3、MainActivity.java
? ?相机动态权限申请(? ? getPermission();? ? ?)
? ?扫描二维码(? ??????startScan();????????)
4、前置摄像扫描?
1、添加依赖
//zxing
implementation "com.google.zxing:core:3.3.1"
implementation "com.journeyapps:zxing-android-embedded:3.5.0"
如果出现这个错误(因为混合支持库),在gradle.properties添加两行代码
android.useAndroidX=true
android.enableJetifier=true
?
2、activity_main
重要代码:
<com.journeyapps.barcodescanner.DecoratedBarcodeView
android:id="@+id/scan_view"
android:layout_width="200dp"
android:layout_height="200dp" />
3、MainActivity.java?
相机权限申请(权限申请可参考? 多权限申请)
扫描二维码(? ??????startScan();????????)
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
private TextView tv_result;
private DecoratedBarcodeView mScanView;
private BeepManager mBeepManager;
private DefaultDecoderFactory mDefaultDecoderFactory;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
supportRequestWindowFeature(Window.FEATURE_NO_TITLE); //去除系统自带标题
setContentView(R.layout.activity_main);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); //全屏显示
mScanView =findViewById(R.id.scan_view);
tv_result=findViewById(R.id.tv_result);
getPermission(); //相机权限申请
mBeepManager = new BeepManager(this);
mDefaultDecoderFactory = new DefaultDecoderFactory(Arrays.asList(BarcodeFormat.QR_CODE,
BarcodeFormat.CODE_128), null, null, false);
mScanView.getBarcodeView().setDecoderFactory(mDefaultDecoderFactory);
startScan(); //扫描
}
//扫描
private void startScan() {
mScanView.decodeSingle(new BarcodeCallback() {
@Override
public void barcodeResult(BarcodeResult result) {
String res = result.getText();
if (null != res) {
doScanResult(res);
}
mScanView.getBarcodeView().stopDecoding();
mScanView.postDelayed(new Runnable() {
@Override
public void run() {
startScan();
}
}, 1500L);
}
@Override
public void possibleResultPoints(List<ResultPoint> resultPoints) {
}
});
}
private void doScanResult(String res) {
mBeepManager.playBeepSound();
tv_result.setText(res);
}
@Override
protected void onResume() {
super.onResume();
mScanView.resume();
}
@Override
protected void onPause() {
super.onPause();
mScanView.pause();
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
return mScanView.onKeyDown(keyCode, event) || super.onKeyDown(keyCode, event);
}
private final int ACCESS_LOCATION=1;
/**
* 获取权限,可以与其他项目的权限进行统一处理
*/
private void getPermission() {
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M) {
int permissionCheck = 0;
permissionCheck += this.checkSelfPermission(Manifest.permission.CAMERA); //请求访问使用照相设备
if (permissionCheck != PackageManager.PERMISSION_GRANTED) { //未获得权限
this.requestPermissions( // 请求授权
new String[]{Manifest.permission.CAMERA},ACCESS_LOCATION);// 自定义常量,任意整型
}
}
}
/**
* 请求权限的结果回调。每次调用 requestpermissions(string[],int)时都会调用此方法。
* @param requestCode 传入的请求代码
* @param permissions 传入permissions的要求
* @param grantResults 相应权限的授予结果:PERMISSION_GRANTED 或 PERMISSION_DENIED
*/
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
switch (requestCode) {
case ACCESS_LOCATION:
if (hasAllPermissionGranted(grantResults)) {
Log.i(TAG, "onRequestPermissionsResult: 用户允许权限");
} else {
Log.i(TAG, "onRequestPermissionsResult: 拒绝搜索设备权限");
}
break;
}
}
private boolean hasAllPermissionGranted(int[] grantResults) {
for (int grantResult : grantResults) {
if (grantResult == PackageManager.PERMISSION_DENIED) {
return false;
}
}
return true;
}
}
在AndroidManifest.xml文件里配置权限
<!-- 请求访问使用照相设备-->
<uses-permission android:name="android.permission.CAMERA" />
<!-- 访问闪光灯-->
<uses-permission android:name="android.permission.FLASHLIGHT" />
?到这里就实现了我们想要的功能,但这里用的摄像头是后置的,有些时候我们可能需要使用前置的摄像头,那么可以吗,当然可以。
4、前置摄像头扫描
只需要在扫描的方法里添加三行代码
Intent intent=new Intent();
intent.putExtra(Intents.Scan.CAMERA_ID,1);
mScanView.initializeFromIntent(intent);
?
?
?
?
|