我试图在片段中获取QR扫描器,因此在扫描时会显示一个框,以便用户访问或取消链接。到目前为止,页面已加载,但其空白白色未显示任何QR阅读器/扫描器
我尝试了ZXingScannerView和多个更改以及不同的网站,但都没有成功,但是我一直想念一些东西,我在android中很安静,所以我不太确定。
public class QRScannerFragment extends Fragment implements ZXingScannerView.ResultHandler {
private ZXingScannerView mScannerView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_qr_scanner, container, false);
mScannerView = new ZXingScannerView(getActivity());
mScannerView.setResultHandler(QRScannerFragment.this); // Register ourselves as a
mScannerView.startCamera();
return mScannerView;
}
@Override
public void onResume() {
super.onResume();
mScannerView.setResultHandler(this);
mScannerView.startCamera();
}
@Override
public void handleResult(Result rawResult) {
Toast.makeText(getActivity(), "Contents = " + rawResult.getText() +
", Format = " + rawResult.getBarcodeFormat().toString(), Toast.LENGTH_SHORT).show();
// Note:
// * Wait 2 seconds to resume the preview.
// * On older devices continuously stopping and resuming camera preview can result in freezing the app.
// * I don't know why this is the case but I don't have the time to figure out.
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
mScannerView.resumeCameraPreview(QRScannerFragment.this);
}
}, 2000);
}
@Override
public void onPause() {
super.onPause();
mScannerView.stopCamera();
}
}
XML文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
</LinearLayout>
答案 0 :(得分:0)
对于您的扫描仪片段来说有点晚了
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (LOG_DEBUG) Log.w(TAG, "onCreateView: ");
initScannerView();
return mScannerView;
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
Log.e(TAG, "onViewCreated: ");
mScannerView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.w(TAG, "onClick: DO NOTHING");
}
});
mScannerView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
Log.w(TAG, "onLongCLick: DO NOTHING");
return true;
}
});
}
@Override
public void handleResult(Result rawResult) {
Log.d(TAG, "handleResult: ");
handleScanResult(rawResult);
}
@Override
public void onStart() {
super.onStart();
initScannerView();
}
@Override
public void onResume() {
super.onResume();
Log.e(TAG, "onResume: ");
if (mScannerView != null) {
mScannerView.setResultHandler(this);
mScannerView.startCamera();
}
}
@Override
public void onStop() {
super.onStop();
if (mScannerView != null) {
mScannerView.setResultHandler(null);
mScannerView.stopCamera();
}
}
private void initScannerView() {
if (mScannerView == null) {
mScannerView = new ZXingScannerView(this.getContext());
}
}
private void handleScanResult(Result rawResult) {
if (rawResult != null) {
mScanStringResult = rawResult.toString();
Log.d(TAG, "handleResult - string result : " + mScanStringResult);
Log.d(TAG, "handleResult - CODE FORMAT : " + rawResult.getBarcodeFormat().toString());
}
}
您需要授予相机权限,在执行Scan片段的事务之前,请使用initRequest()方法。
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
@NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (LOG_DEBUG) Log.e(TAG, " onRequestPermissionsResult");
if (requestCode == CommonConstants.PERMISSION_CODE_REQUEST_ACCESS_CAMERA) {
if (shouldShowRequestPermissionRationale(Manifest.permission.CAMERA)) {
// rationale
if (LOG_DEBUG) Log.w(TAG, "denied nope");
dialogRational();
} else {
if (checkSelfPermission()) {
//allowed
if (LOG_DEBUG) Log.v(TAG, "onGranted: Permission");
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
if (getFragmentManager() != null) {
//GO to your scan fragment
}
}
}, 200);
} else {
//set to never ask again
if (LOG_DEBUG) Log.w(TAG, "set to never ask again never");
//TODO DIALOG TO GO TO SETTINGS
UtilDialog.settingDialog(mInflatedView.getContext());
}
}
}
}
private void dialogRational() {
if (LOG_DEBUG) Log.d(TAG, "dialogRational: ");
new AlertDialog.Builder(mInflatedView.getContext())
.setCancelable(false).setTitle("Warning")
.setMessage("You must grant the permission, " +
"if denied, then go to Setting to activate manually ")
.setPositiveButton("GRANT ", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
requestPermission();
}
})
.setNeutralButton("DISMISS", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
}
}).show();
}
//init
public void initPermission() {
if (LOG_DEBUG) Log.d(TAG, "initPermission: ");
requestPermission();
}
//request
private void requestPermission() {
if (LOG_DEBUG) Log.d(TAG, " requestPermission");
requestPermissions(new String[]{Manifest.permission.CAMERA}
, CommonConstants.PERMISSION_CODE_REQUEST_ACCESS_CAMERA);
}
// this one : never ask again
private boolean checkSelfPermission() {
if (LOG_DEBUG) Log.d(TAG, " checkSelfPermission");
return ContextCompat.checkSelfPermission(mInflatedView.getContext(), Manifest.permission.CAMERA)
== PackageManager.PERMISSION_GRANTED;
}
并在清单
中使用它们<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />