Android 生成二维码
在根版本导入
allprojects {
repositories {
google()
jcenter()
maven { url 'https://jitpack.io' }
}
}
添加依赖 implementation ‘com.github.jwkj:LibZXing:v1.0.4’
布局
<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=".MainActivity">
<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>
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
import com.jwkj.libzxing.OnQRCodeScanCallback;
import com.jwkj.libzxing.QRCodeManager;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button bt_scan = findViewById(R.id.bt_scan);
Button bt_create_one = findViewById(R.id.bt_create_one);
Button bt_create_two = findViewById(R.id.bt_create_two);
imageView = findViewById(R.id.iv);
bt_scan.setOnClickListener(this);
bt_create_one.setOnClickListener(this);
bt_create_two.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.bt_scan:
QRCodeManager.getInstance().with(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() {
}
});
break;
case R.id.bt_create_one:
Bitmap bitmap=QRCodeManager.getInstance().createQRCode("https://user.qzone.qq.com/731177635/infocenter",400,400);
imageView.setImageBitmap(bitmap);
break;
case R.id.bt_create_two:
Bitmap bitmap1=BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher_round);
Bitmap bt=QRCodeManager.getInstance().createQRCode("http://www.baidu.com", 400, 400,bitmap1);
imageView.setImageBitmap(bt);
break;
}
}
@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);
}
}