总感觉这东西比较鸡肋,因为当下很多扫码框架都是基于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));
}
}