绘制二维码
前几天接到项目说要改版的消息,果然事情马上来了。打开蓝湖一看——从左到右渐变色的二维码想了一下确实没搞过,就懒得翻以前的代码了,自己动手丰衣足食!
话不多说,上效果图
image.pngimage.png image.png
上代码,我们来看代码实现
public static Bitmap createQRGradientImage(String url, final int width, final int height){
try {
// 判断URL合法性
if (url == null || "".equals(url) || url.length() < 1){
return null;
}
Hashtable<EncodeHintType, Object> hints = new Hashtable();
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
hints.put(EncodeHintType.MARGIN, 0);
// 图像数据转换,使用了矩阵转换
BitMatrix bitMatrix = new QRCodeWriter().encode(url,
BarcodeFormat.QR_CODE, width, height, hints);
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] = 0xffffffff;
}
}
}
*/
// 渐变色 从上到下绘制
for (int y = 0; y < height; y++){
for (int x = 0; x < width; x++) {
if (bitMatrix.get(x, y)) {// 二维码颜色
int red = (int)(56 - (56.0 - 14.0) / height * (y + 1));
int green = (int)(247 - (247.0 - 145.0) / height * (y + 1));
int blue = (int)(195 - (195.0-79.0) / height * (y + 1));
Color color = new Color();
int colorInt = color.argb(255, red, green, blue);
// 修改二维码的颜色,可以分别制定二维码和背景的颜色
pixels[y * width + x] = bitMatrix.get(x, y) ? colorInt: 16777215;// 0x000000:0xffffff
} else {
pixels[y * width + x] = 0x00ffffff;// 背景颜色
}
}
}
// 生成二维码图片的格式,使用ARGB_8888
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;
}
咦,不是说是从左到右吗——别着急,从左到右只是改一下绘制方向就行了
//渐变色从左到右
for (int x = 0;x<width; x++){
for (int y = 0;y<height;y++){
if (bitMatrix.get(x,y)){
int red = (int)(56 - (56.0 - 14.0) / height * (y + 1));
int green = (int)(247 - (247.0 - 145.0) / height * (y + 1));
int blue = (int)(195 - (195.0-79.0) / height * (y + 1));
Color color = new Color();
int colorInt = color.argb(255, red, green, blue);
// 修改二维码的颜色,可以分别制定二维码和背景的颜色
pixels[x * height + y] = bitMatrix.get(x, y) ? colorInt: 16777215;// 0x000000:0xffffff
}else {
pixels[x * height + y] = 0x00ffffff;// 背景颜色
}
}
}
最终效果图
image.pngkotlin版本
fun createQRGradientImage(url: String?, width: Int, height: Int): Bitmap? {
try {
// 判断URL合法性
if (url == null || "" == url || url.length < 1) {
return null
}
val hints = Hashtable<EncodeHintType, Any>()
hints.put(EncodeHintType.CHARACTER_SET, "utf-8")
hints.put(EncodeHintType.MARGIN, 0)
// 图像数据转换,使用了矩阵转换
val bitMatrix = QRCodeWriter().encode(
url,
BarcodeFormat.QR_CODE, width, height, hints
)
val pixels = IntArray(width * height)
// 渐变色 从左到右绘制
for (x in 0 until width) {
for (y in 0 until height) {
if (bitMatrix.get(x, y)) {
val red = (56 - (56.0 - 14.0) / height * (y + 1)).toInt()
val green = (247 - (247.0 - 145.0) / height * (y + 1)).toInt()
val blue = (195 - (195.0 - 79.0) / height * (y + 1)).toInt()
val colorInt = argb(255, red, green, blue)
// 修改二维码的颜色,可以分别制定二维码和背景的颜色
pixels[x * height + y] = if (bitMatrix.get(x, y)) colorInt else 16777215// 0x000000:0xffffff
} else {
pixels[x * height + y] = 0x00ffffff// 背景颜色
}
}
}
// 生成二维码图片的格式,使用ARGB_8888
val bitmap = Bitmap.createBitmap(
width, height,
Bitmap.Config.ARGB_8888
)
bitmap.setPixels(pixels, 0, width, 0, 0, width, height)
return bitmap
} catch (e: WriterException) {
e.printStackTrace()
}
return null
}
本文经过授权,转发自“小泽玛莉嗷”https://juejin.im/post/5c3c2c40e51d45522851f1d1,欢迎学习进步
网友评论