导包:
build:
implementation 'cn.bingoogolapple:bga-qrcode-zxing:1.3.4'
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/sao"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="扫描二维码"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintRight_toRightOf="parent"
/>
<EditText
android:id="@+id/edit"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="请输入"
android:layout_marginTop="10dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/sao"
/>
<Button
android:id="@+id/sheng"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="生成二维码"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/edit"
/>
<ImageView
android:id="@+id/image"
android:layout_width="150dp"
android:layout_height="150dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@id/sheng"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginTop="10dp"
android:src="@mipmap/ic_launcher"
/>
</android.support.constraint.ConstraintLayout>
MainActivity:
public class MainActivity extends AppCompatActivity {
private Button sao;
private Button sheng;
private EditText editText;
private ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
//获取资源ID
sao = findViewById(R.id.sao);
sheng = findViewById(R.id.sheng);
editText = findViewById(R.id.edit);
imageView = findViewById(R.id.image);
sao.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
panduan();
}
});
sheng.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//生成二维码
createQRCode();
}
});
}
private void createQRCode() {
QRTask qrTask = new QRTask(this, imageView, editText);
qrTask.execute(editText.getText().toString());
}
//生成二维码
static class QRTask extends AsyncTask<String,Void,Bitmap>{
private WeakReference<Context> mcontext;
private WeakReference<ImageView> mimageview;
public QRTask(Context context,ImageView imageView,EditText ed){
mcontext=new WeakReference<>(context);
mimageview=new WeakReference<>(imageView);
}
@Override
protected Bitmap doInBackground(String... strings) {
String str=strings[0];
if (TextUtils.isEmpty(str)){
return null;
}
int size = mcontext.get().getResources().getDimensionPixelOffset(R.dimen.ss);
return QRCodeEncoder.syncEncodeQRCode(str,size);
}
@Override
protected void onPostExecute(Bitmap bitmap) {
if (bitmap!=null){
mimageview.get().setImageBitmap(bitmap);
}else{
Toast.makeText(mcontext.get(), "生成失败", Toast.LENGTH_SHORT).show();
}
}
}
//动态权限判断
private void panduan() {
//第一步,判断系统版本是否Wie6.0
if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.M){
//第二步:checkSelfPermission判断有没有此权限
//第一个参数:上下文
//第二个参数:我们想要判断的权限
//PackageManager.PERMISSION_GRANTED 条件,权限有没有被授予
if (ContextCompat.checkSelfPermission(this,Manifest.permission.CAMERA)!=PackageManager.PERMISSION_GRANTED){
ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.CAMERA},100);
}else{
startActivity(new Intent(MainActivity.this,LoginActivity.class));
}
}
}
/**
* 接受权限请求结果
* @param requestCode 请求码
* @param permissions 我们请求的权限数组
* @param grantResults 返回结果数组
*/
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
//如果requestCode匹配,切权限申请通过
if (requestCode==100&&grantResults[0]==PackageManager.PERMISSION_GRANTED){
startActivity(new Intent(MainActivity.this,LoginActivity.class));
}
}
}
activity_login.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".LoginActivity">
<cn.bingoogolapple.qrcode.zxing.ZXingView
android:id="@+id/zxingview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
></cn.bingoogolapple.qrcode.zxing.ZXingView>
</android.support.constraint.ConstraintLayout>
LoginActivity:
public class LoginActivity extends AppCompatActivity implements QRCodeView.Delegate {
private ZXingView zXingView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
zXingView = findViewById(R.id.zxingview);
zXingView.setDelegate(this);
}
@Override
protected void onResume() {
super.onResume();
zXingView.startCamera();
zXingView.startSpotAndShowRect();
}
@Override
protected void onStop() {
super.onStop();
zXingView.stopCamera();
}
@Override
protected void onDestroy() {
super.onDestroy();
zXingView.onDestroy();
}
@Override
public void onScanQRCodeSuccess(String result) {
Toast.makeText(this, result+"", Toast.LENGTH_SHORT).show();
Intent intent=new Intent(LoginActivity.this,ThreeActivity.class);
intent.putExtra("aa",result);
startActivity(intent);
}
@Override
public void onCameraAmbientBrightnessChanged(boolean isDark) {
}
@Override
public void onScanQRCodeOpenCameraError() {
}
}
activity_three.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ThreeActivity">
<WebView
android:id="@+id/ccc"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
></WebView>
</android.support.constraint.ConstraintLayout>
ThreeActivity:
public class ThreeActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_three);
WebView webView = findViewById(R.id.ccc);
Intent intent = getIntent();
String zhi = intent.getStringExtra("aa");
webView.loadDataWithBaseURL(null, zhi, "text/html", "UTF-8", null);
}
}