首先在工程库
allprojects {
repositories {
google()
jcenter()
maven { url 'https://jitpack.io' }
}
}
导入依赖
implementation 'com.github.jwkj:LibZXing:v1.0.4'
<?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"
tools:context=".MainActivity"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_height="wrap_content">
<Button
android:id="@+id/bt_scan"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="扫描" />
<Button
android:id="@+id/bt_create_one"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="生成不带logo的二维码" />
<Button
android:id="@+id/bt_create_two"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="生成带logo的二维码" />
</LinearLayout>
<ImageView
android:layout_width="wrap_content"
android:id="@+id/iv"
android:layout_height="wrap_content" />
</LinearLayout>
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
bt_scan.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
QRCodeManager.getInstance().with(MainActivity.this).setReqeustType(0).scanningQRCode(new OnQRCodeScanCallback() {
@Override
public void onCompleted(String s) {
Toast.makeText(MainActivity.this, "结果:"+s, Toast.LENGTH_SHORT).show();
}
@Override
public void onError(Throwable throwable) {
}
@Override
public void onCancel() {
}
});
}
});
bt_create_one.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Bitmap bitmap = QRCodeManager.getInstance().createQRCode("http://www.baidu.com", 400, 400);
iv.setImageBitmap(bitmap);
}
});
bt_create_two.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Bitmap resource = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher_round);
Bitmap bt = QRCodeManager.getInstance().createQRCode("http://www.baidu.com", 400, 400,resource);
iv.setImageBitmap(bt);
}
});
}
private void initView() {
bt_scan = (Button) findViewById(R.id.bt_scan);
bt_create_one = (Button) findViewById(R.id.bt_create_one);
bt_create_two = (Button) findViewById(R.id.bt_create_two);
iv = (ImageView) findViewById(R.id.iv);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Toast.makeText(this, "走了:", Toast.LENGTH_SHORT).show();
QRCodeManager.getInstance().with(this).onActivityResult(requestCode, resultCode, data);
}