1、在根build.gradle中添加maven { url ‘https://jitpack.io’ }
(在整个大的build.gradle 中添加)
allprojects {
repositories {
google()
jcenter()
maven { url 'https://jitpack.io' }
}
}
2、app的build.gradle中添加依赖
implementation 'com.github.yuzhiqiang1993:zxing:2.1.4'
implementation 'com.android.support:design:28.0.0'
3、MainActivity
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button sheng;
private ImageView img;
private String content = "呵呵哈哈哈";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
sheng = findViewById(R.id.sheng);
sheng.setOnClickListener(this);
img = findViewById(R.id.img);
img.setOnClickListener(this);
}
//点击按钮 生成二维码
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.sheng:
Bitmap logo = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
try {
//生成二维码需要用到类是CodeCreator
Bitmap bitmap = CodeCreator.createQRCode(content, 300, 300, logo);
img.setImageBitmap(bitmap);
} catch (WriterException e) {
e.printStackTrace();
}
break;
}
}
}