1、Gradle引入
// 二維碼生成
implementation 'com.google.zxing:core:3.5.1'
2、新建工具類
ImageUtils
import android.graphics.Bitmap;
import android.graphics.Color;
import android.text.TextUtils;
import androidx.annotation.Nullable;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import java.util.Hashtable;
public class ImageUtils {
/**
* 創(chuàng)建二維碼位圖
*
* @param inputStr 字符串內(nèi)容(支持中文)
* @param width 位圖寬度(單位:px)
* @param height 位圖高度(單位:px)
* @return
*/
@Nullable
public static Bitmap createQRCodeBitmap(String inputStr, int width, int height) {
return createQRCodeBitmap(inputStr, width, height, "UTF-8", "H", "2", Color.BLACK, Color.WHITE);
}
/**
* 創(chuàng)建二維碼位圖 (支持自定義配置和自定義樣式)
*
* @param inputStr 字符串內(nèi)容
* @param width 位圖寬度,要求>=0(單位:px)
* @param height 位圖高度,要求>=0(單位:px)
* @param character_set 字符集/字符轉(zhuǎn)碼格式 (支持格式:{@link com.google.zxing.common.CharacterSetECI })。傳null時,zxing源碼默認使用 "ISO-8859-1"
* @param error_correction_level 容錯級別 (支持級別:{@link com.google.zxing.qrcode.decoder.ErrorCorrectionLevel })。傳null時,zxing源碼默認使用 "L"
* @param margin 空白邊距 (可修改,要求:整型且>=0), 傳null時,zxing源碼默認使用"4"。
* @param color_black 黑色色塊的自定義顏色值
* @param color_white 白色色塊的自定義顏色值
* @return
*/
public static Bitmap createQRCodeBitmap(String inputStr, int width, int height,
String character_set, String error_correction_level,
String margin, int color_black, int color_white) {
// 字符串內(nèi)容判空
if (TextUtils.isEmpty(inputStr)) {
return null;
}
// 寬和高>=0
if (width < 0 || height < 0) {
return null;
}
try {
/** 1.設置二維碼相關配置 */
Hashtable<EncodeHintType, String> hints = new Hashtable<>();
// 字符轉(zhuǎn)碼格式設置
if (!TextUtils.isEmpty(character_set)) {
hints.put(EncodeHintType.CHARACTER_SET, character_set);
}
// 容錯率設置
if (!TextUtils.isEmpty(error_correction_level)) {
hints.put(EncodeHintType.ERROR_CORRECTION, error_correction_level);
}
// 空白邊距設置
if (!TextUtils.isEmpty(margin)) {
hints.put(EncodeHintType.MARGIN, margin);
}
/** 2.將配置參數(shù)傳入到QRCodeWriter的encode方法生成BitMatrix(位矩陣)對象 */
BitMatrix bitMatrix = new QRCodeWriter().encode(inputStr, BarcodeFormat.QR_CODE, width, height, hints);
/** 3.創(chuàng)建像素數(shù)組,并根據(jù)BitMatrix(位矩陣)對象為數(shù)組元素賦顏色值 */
int[] pixels = new int[width * height];
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
//bitMatrix.get(x,y)方法返回true是黑色色塊,false是白色色塊
if (bitMatrix.get(x, y)) {
pixels[y * width + x] = color_black;//黑色色塊像素設置
} else {
pixels[y * width + x] = color_white;// 白色色塊像素設置
}
}
}
/** 4.創(chuàng)建Bitmap對象,根據(jù)像素數(shù)組設置Bitmap每個像素點的顏色值,并返回Bitmap對象 */
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
return bitmap;
} catch (WriterException e) {
e.printStackTrace();
return null;
}
}
}
3、在
Activity中使用
ImageView testImgView = findViewById(R.id.testImgView);
Bitmap tempBitmap = ImageUtils.createQRCodeBitmap("這是一段測試文本!",300,300);
testImgView.setImageBitmap(tempBitmap);