android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:layout_alignParentEnd=“true”
android:text="@string/firstTest"
app:layout_constraintEnd_toEndOf=“parent”
app:layout_constraintStart_toStartOf=“parent”
app:layout_constraintTop_toTopOf=“parent” />
<Button
android:id="@+id/btn"
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:text="@string/generate_btn"
app:layout_constraintTop_toBottomOf="@+id/editText"
app:layout_constraintEnd_toEndOf=“parent”
app:layout_constraintStart_toStartOf=“parent”/>
<ImageView
android:id="@+id/imageView"
android:layout_width=“256dp”
android:layout_height=“256dp”
android:layout_centerInParent=“true”
app:layout_constraintBottom_toBottomOf=“parent”
app:layout_constraintEnd_toEndOf=“parent”
app:layout_constraintStart_toStartOf=“parent”
app:layout_constraintTop_toTopOf=“parent” />
</androidx.constraintlayout.widget.ConstraintLayout>
https://www.baidu.com
生成二维码
- 在AndroidManifest.xml中注册(Android Studio会自动生成)
<application
…
…
- 要实现二维码的生成,主要是靠zxing提供的接口和方法,在GenerateQRcode文件中新建一个
generateSimpleBitmap
方法用于生成二维码,参数为二维码内容、宽和高,并将参数传入到QRCodeWriter的encode
方法生成BitMatrix
对象,创建一个大小为二维码宽*高的数组,根据BitMatrix
对象,往其中放置黑白色块,最后使用createBitmap
返回一个Bitmap
对象
private Bitmap generateSimpleBitmap(String content, int width, int height) {
// 字符串内容判空
if (TextUtils.isEmpty(content)) {
Toast.makeText(getApplicationContext(),“输入为空!”,Toast.LENGTH_LONG).show();
return null;
}
QRCodeWriter qrCodeWriter = new QRCodeWriter();
Map<EncodeHintType, String> hints = new HashMap<>();
hints.put(EncodeHintType.CHARACTER_SET, “utf-8”);
try {
BitMatrix encode = qrCodeWriter.encode(content, BarcodeFormat.QR_CODE, width, height, hints);
int[] pixels = new int[width * height];
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
if (encode.get(x, y)) {
pixels[y * width + x] = 0x00000000;
} else {
pixels[y * width + x] = 0xFFFFFFFF;
}
}
}
return Bitmap.createBitmap(pixels, 0, width, width, height, Bitmap.Config.RGB_565);
} catch (WriterException e) {
e.printStackTrace();
}
return null;
}
- 在GenerateQRcode文件中的
onCreate
引用,将结果注入布局的ImageView组件中
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.generate_qrcode);
btn = (Button)findViewById(R.id.btn);
editText = (EditText)findViewById(R.id.editText);
imageView = (ImageView)findViewById(R.id.imageView);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String content = editText.getText().toString();
Bitmap qrBitmap = generateSimpleBitmap(content,256, 256);
imageView.setImageBitmap(qrBitmap);
}
});
}
- 实现结果
- 在以上基础上,在GenerateQRcode文件中新建一个
generateQRCodeBitmap
方法,相比generateSimpleBitmap
,参数多了两个颜色,一个为原二维码黑点的颜色,另一个为原来白色的背景颜色
public Bitmap generateQRCodeBitmap(String content, int width, int height,
int color_point, int color_back) {
//字符串内容判空
if (TextUtils.isEmpty(content)) {
Toast.makeText(getApplicationContext(),“输入为空!”,Toast.LENGTH_LONG).show();
return null;
}
Map<EncodeHintType, String> hints = new HashMap<>();
//格式utf-8
hints.put(EncodeHintType.CHARACTER_SET, “utf-8”);
//空白边距设置
hints.put(EncodeHintType.MARGIN, “1”);
try {
//生成BitMatrix对象encode
BitMatrix encode = new QRCodeWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints);
//生成宽*高的像素数组
int[] pixels = new int[width * height];
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
i