依赖
implementation 'com.github.yuzhiqiang1993:zxing:2.1.4' implementation 'com.android.support:design:28.0.0'
切记15 一定改16
报错是正常的!
private String contentEtString = "小明";
private ImageView image;
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.frag_2, container, false);
initView(v);
Bitmap logo = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
try {
//生成二维码需要用到类是CodeCreator
Bitmap bitmap = CodeCreator.createQRCode(contentEtString, 400, 400, logo);
image.setImageBitmap(bitmap);
} catch (WriterException e) {
e.printStackTrace();
}
return v;
}
private void initView(View v) {
image = (ImageView) v.findViewById(R.id.image);
}