生成二维码需要导入的jar包: core-3.2.1.jar
这里提供了一个写好的方法,只要在content附上你想要的句子,则会帮你生成相对应的二维码。
private Bitmap generateBitmap(String content,int width, int height){
QRCodeWriter qrCodeWriter = new QRCodeWriter();
Map<EncodeHintType,String> hints = new HashMap<>();
hints.put(EncodeHintType.CHARACTER_SET,"utf-8");
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;
}