ImageView ivQrCode = (ImageView)findViewById(R.id.iv_qrcode);
Bitmap bitmap = strTo2DCode(“aabbbb”, 800, 800);
ivQrCode.setImageBitmap(addLogo(bitmap,
BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher)));//有logo
// ivQrCode.setImageBitmap(bitmap);//无logo
1、生成二维码方法:
/**
-
字符串生成二维码
-
@param strEncode 要生成二维码的字符串
-
@param width 要生成图片的宽度
-
@param height 要生成图片的高度
-
@return
*/
public static Bitmap strTo2DCode(String strEncode, int width, int height) {
Bitmap bitmap = null;
try {
BitMatrix bitMatrix = new MultiFormatWriter().encode(new String(strEncode.getBytes(“UTF-8”), “iso-8859-1”), BarcodeFormat.QR_CODE, width, height);
// 二维矩阵转为一维像素数组,也就是一直横着排了
int[] pixels = new int[width * height];
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
if (bitMatrix.get(x, y)) {
pixels[y * width + x] = 0xff000000;
} else {
pixels[y * width + x] = 0x00000000;
}
}
}
bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
// 通过像素数组生成bitmap,具体参考api
bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (WriterException e) {
e.printStackTrace();
}
return bitmap;
}
2、向二维码中心添加logo方法:
/**
- 在二维码中间添加Logo图案
*/
private static Bitmap addLogo(Bitmap src, Bitmap logo) {
if (src == null) {
return null;
}
if (logo == null) {
return src;
}
//获取图片的宽高
int srcWidth = src.getWidth();
int srcHeight = src.getHeight();
int logoWidth = logo.getWidth();
int logoHeight = logo.getHeight();
if (srcWidth == 0 || srcHeight == 0) {
return null;
}
if (logoWidth == 0 || logoHeight == 0) {
return src;
}
//logo大小为二维码整体大小的1/5
float scaleFactor = srcWidth * 1.0f / 5/ logoWidth;
Bitmap bitmap = Bitmap.createBitmap(srcWidth, srcHeight, Bitmap.Config.ARGB_8888);
try {
Canvas canvas = new Canvas(bitmap);
canvas.drawBitmap(src, 0, 0, null);
canvas.scale(scaleFactor, scaleFactor, srcWidth / 2, srcHeight / 2);
canvas.drawBitmap(logo, (srcWidth - logoWidth) / 2, (srcHeight - logoHeight) / 2, null);
canvas.save();
canvas.restore();
} catch (Exception e) {
bitmap = null;
e.getStackTrace();
}
return bitmap;
}
3、条形码
=====
添加依赖
implementation “com.google.zxing:core:3.3.1”
工具方法
public class CodeUtils {
/**
-
生成条形码(不支持中文)
-
@param content
-
@return
*/
public static Bitmap createBarcode(String content) {
try {
BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.CODE_128, 3000, 700);
int width = bitMatrix.getWidth();
int height = bitMatrix.getHeight();
int[] pixels = new int[width * height];
for (int y = 0; y < height; y++) {
int offset = y * width;
for (int x = 0; x < width; x++) {
pixels[offset + x] = bitMatrix.get(x, y) ? 0xff000000 : 0xFFFFFFFF;
}
}
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;
}
/**
-
生成二维码
-
@param content
-
@return
*/
public static Bitmap createQrcode(String content) {
Map<EncodeHintType, Object> hints = new HashMap<>();
// 支持中文配置
hints.put(EncodeHintType.CHARACTER_SET, “UTF-8”);
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
try {
BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, 1000, 1000
, hints);
int width = bitMatrix.getWidth();
int height = bitMatrix.getHeight();
int[] pixels = new int[width * height];
for (int y = 0; y < height; y++) {
int offset = y * width;
for (int x = 0; x < width; x++) {
pixels[offset + x] = bitMatrix.get(x, y) ? 0xff000000 : 0xFFFFFFFF;
}
}
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
新的开始
改变人生,没有什么捷径可言,这条路需要自己亲自去走一走,只有深入思考,不断反思总结,保持学习的热情,一步一步构建自己完整的知识体系,才是最终的制胜之道,也是程序员应该承担的使命。
《系列学习视频》
《系列学习文档》
《我的大厂面试之旅》
参考docs.qq.com/doc/DSkNLaERkbnFoS0ZF
s(pixels, 0, width, 0, 0, width, height);
新的开始
改变人生,没有什么捷径可言,这条路需要自己亲自去走一走,只有深入思考,不断反思总结,保持学习的热情,一步一步构建自己完整的知识体系,才是最终的制胜之道,也是程序员应该承担的使命。
《系列学习视频》
[外链图片转存中…(img-G7KpM1uT-1724338737979)]
《系列学习文档》
[外链图片转存中…(img-6dk0s59h-1724338737980)]
《我的大厂面试之旅》
[外链图片转存中…(img-M9itYBbw-1724338737980)]
参考docs.qq.com/doc/DSkNLaERkbnFoS0ZF