總感覺這東西比較雞肋,因爲當下很多掃碼框架都是基於Zxing的封裝,而在此基礎之下,自然都具備生成二維碼的功能 ~ 但 ~~~ 接手項目中看到了通過工具類直接生成的方式,所以就順手記錄下
jar包導入
導入jar包即可,關於zxing的jar包,自行百度下載即可 ~
使用方式
DisplayMetrics dm = getResources().getDisplayMetrics();
int width = dm.widthPixels;
/**content 轉碼內容
*R.color.white #ffffff 背景底色
*R.color.mainTextColor #333333 二維碼繪製色
*上方的倆種顏色設置之後,是我們常見的白色黑線
* ivQrCode 承載二維碼的Imageview
*/
QRCodeUtils.builder(SPUtils.getString(mContext, "content")).
backColor(getResources().getColor(R.color.white)).
codeColor(getResources().getColor(R.color.mainTextColor)).
codeSide(width/2).
into(ivQrCode);
工具類 QRCodeUtils (直接copy)
package com.survey.ddchainclub.utils;
import android.graphics.Bitmap;
import android.support.annotation.NonNull;
import android.widget.ImageView;
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 QRCodeUtils {
public static QRCodeUtils.Builder builder(@NonNull CharSequence text) {
return new QRCodeUtils.Builder(text);
}
public static class Builder {
private int backgroundColor = 0xffffffff;
private int codeColor = 0xff000000;
private int codeSide = 800;
private CharSequence content;
public Builder backColor(int backgroundColor) {
this.backgroundColor = backgroundColor;
return this;
}
public Builder codeColor(int codeColor) {
this.codeColor = codeColor;
return this;
}
public Builder codeSide(int codeSide) {
this.codeSide = codeSide;
return this;
}
public Builder(@NonNull CharSequence text) {
this.content = text;
}
public Bitmap into(ImageView imageView) {
Bitmap bitmap = QRCodeUtils.creatQRCode(content, codeSide, codeSide, backgroundColor, codeColor);
if (imageView != null) {
imageView.setImageBitmap(bitmap);
}
return bitmap;
}
}
//----------------------------------------------------------------------------------------------以下爲生成二維碼算法
public static Bitmap creatQRCode(CharSequence content, int QR_WIDTH, int QR_HEIGHT, int backgroundColor, int codeColor) {
Bitmap bitmap = null;
try {
// 判斷URL合法性
if (content == null || "".equals(content) || content.length() < 1) {
return null;
}
Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
// 圖像數據轉換,使用了矩陣轉換
BitMatrix bitMatrix = new QRCodeWriter().encode(content + "", BarcodeFormat.QR_CODE, QR_WIDTH, QR_HEIGHT, hints);
int[] pixels = new int[QR_WIDTH * QR_HEIGHT];
// 下面這裏按照二維碼的算法,逐個生成二維碼的圖片,
// 兩個for循環是圖片橫列掃描的結果
for (int y = 0; y < QR_HEIGHT; y++) {
for (int x = 0; x < QR_WIDTH; x++) {
if (bitMatrix.get(x, y)) {
pixels[y * QR_WIDTH + x] = codeColor;
} else {
pixels[y * QR_WIDTH + x] = backgroundColor;
}
}
}
// 生成二維碼圖片的格式,使用ARGB_8888
bitmap = Bitmap.createBitmap(QR_WIDTH, QR_HEIGHT, Bitmap.Config.ARGB_8888);
bitmap.setPixels(pixels, 0, QR_WIDTH, 0, 0, QR_WIDTH, QR_HEIGHT);
} catch (WriterException e) {
e.printStackTrace();
}
return bitmap;
}
public static Bitmap creatQRCode(CharSequence content, int QR_WIDTH, int QR_HEIGHT) {
return creatQRCode(content, QR_WIDTH, QR_HEIGHT, 0xffffffff, 0xff000000);
}
public static Bitmap creatQRCode(CharSequence content) {
return creatQRCode(content, 700, 700);
}
//==============================================================================================二維碼算法結束
/**
* @param content 需要轉換的字符串
* @param QR_WIDTH 二維碼的寬度
* @param QR_HEIGHT 二維碼的高度
* @param iv_code 圖片空間
*/
public static void createQRCode(String content, int QR_WIDTH, int QR_HEIGHT, ImageView iv_code) {
iv_code.setImageBitmap(creatQRCode(content, QR_WIDTH, QR_HEIGHT));
}
/**
* QR_WIDTH 二維碼的寬度
* QR_HEIGHT 二維碼的高度
*
* @param content 需要轉換的字符串
* @param iv_code 圖片空間
*/
public static void createQRCode(String content, ImageView iv_code) {
iv_code.setImageBitmap(creatQRCode(content));
}
}