工具类
public class QRCodeUtils {
/**
* 生成固定大小的二维码(不需网络权限)
*
* @param content 需要生成的内容
* @param width 二维码宽度
* @param height 二维码高度
* @return
*/
public static Bitmap createRQCode(String content, int width, int height) {
if (TextUtils.isEmpty(content)) {
return null;
}
QRCodeWriter qrCodeWriter = new QRCodeWriter();
Map<EncodeHintType, String> hints = new HashMap<>();
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
hints.put(EncodeHintType.MARGIN, "0");
try {
BitMatrix encode = qrCodeWriter.encode(content, BarcodeFormat.QR_CODE, width, height, hints);
int[] pixels = new int[width * height];
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
if (encode.get(j, i)) {
pixels[i * width + j] = 0x00000000;
} else {
pixels[i * width + j] = 0xffffffff;
}
}
}
return Bitmap.createBitmap(pixels, 0, width, width, height, Bitmap.Config.RGB_565);
} catch (WriterException e) {
e.printStackTrace();
}
return null;
}
/**
* 生成条形码
*
* @param contents
* @param desiredWidth
* @param desiredHeight
* @return
*/
public static Bitmap createBarcode(String contents, int desiredWidth, int desiredHeight) {
if (TextUtils.isEmpty(contents)) {
return null;
}
BarcodeFormat barcodeFormat = BarcodeFormat.CODE_128;
Bitmap resultBitmap = encodeAsBitmap(contents, barcodeFormat, desiredWidth, desiredHeight);
return rotateBmp(resultBitmap, 90);
}
protected static Bitmap encodeAsBitmap(String contents,
BarcodeFormat format, int desiredWidth, int desiredHeight) {
final int WHITE = 0xFFFFFFFF;
final int BLACK = 0xFF000000;
MultiFormatWriter writer = new MultiFormatWriter();
BitMatrix result = null;
try {
Map<EncodeHintType, String> hints = new HashMap<>();
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
hints.put(EncodeHintType.MARGIN, "0");
result = writer.encode(contents, format, desiredWidth, desiredHeight, hints);
} catch (WriterException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
int width = result.getWidth();
int height = result.getHeight();
int[] pixels = new int[width * height];
// All are 0, or black, by default
for (int y = 0; y < height; y++) {
int offset = y * width;
for (int x = 0; x < width; x++) {
pixels[offset + x] = result.get(x, y) ? BLACK : WHITE;
}
}
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
return bitmap;
}
/**
* 旋转bitmap
*
* @param b
* @param degrees
* @return
*/
public static Bitmap rotateBmp(Bitmap b, int degrees) {
if (degrees != 0 && b != null) {
Matrix m = new Matrix();
m.setRotate(degrees, (float) b.getWidth() / 2, (float) b.getHeight() / 2);
try {
Bitmap b2 = Bitmap.createBitmap(b, 0, 0, b.getWidth(),
b.getHeight(), m, true);
if (b != b2) {
b.recycle();
b = b2;
}
} catch (OutOfMemoryError ex) {
}
}
return b;
}
}
使用
//生成二维码
Bitmap RQBitmap = QRCodeUtils.createRQCode("123456",100,100);
if (null != RQBitmap) {
imgRQCode.setImageBitmap(RQBitmap);
}
//生成条形码
Bitmap barBitmap = QRCodeUtils.createBarcode("123456",100,50);
if (null != barBitmap) {
imgBarCode.setImageBitmap(barBitmap);
}
注意事项
- 如果想改变生成bitmap的白色边框的话,需要修改以下参数。
Map<EncodeHintType, String> hints = new HashMap<>();
hints.put(EncodeHintType.MARGIN, "0");