> 1:清单文件权限
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.VIBRATE" />
2:依赖
implementation 'cn.bingoogolapple:bga-qrcode-zxing:1.2.1'
布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context=".TwoActivity"
android:layout_centerVertical="true"
>
<ImageView
android:id="@+id/myimg"
android:layout_width="250dp"
android:layout_gravity="center"
android:layout_height="250dp"
/>
</LinearLayout>
2:MainActivity层
package com.example.erweima;
import android.graphics.Bitmap;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;
import cn.bingoogolapple.qrcode.zxing.QRCodeEncoder;
public class TwoActivity extends AppCompatActivity {
private ImageView img;
Handler handler = new Handler(Looper.getMainLooper()){
@Override
public void handleMessage(Message msg) {
Bitmap v = (Bitmap) msg.obj; //强转
// 将集成后的二维码设置到ImageView上进行展示
img.setImageBitmap(v);
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_two);
img = findViewById(R.id.myimg);
// 集成的代码必须在子线程里
new Thread(new Runnable() {
@Override
public void run() {
// 第一个也可以是图片的网址展示图片,也可以是网址进行跳转,如果是图片的话size大一些就可以了
Bitmap bitmap = QRCodeEncoder.syncEncodeQRCode("这里写要Toast的", 50);
Message message = handler.obtainMessage();
message.obj = bitmap;
handler.sendMessage(message);// 发送Handler
}
}).start();
}
}