Recently doing related applications a two-dimensional code, incidentally relevant knowledge gathering information. Today we look to generate two-dimensional code and two-dimensional code beautification (add a logo, color, background, text, watermark can be found here).
Third-party libraries
Now the most widely used two-dimensional code used in practical applications is Zxing library research paper Zxing-2.0 versions do analysis generation tools.
Download mode: https://blog.csdn.net/twk121109281/article/details/93600672
Method to load third-party libraries: https://blog.csdn.net/twk121109281/article/details/93601300
Generate two-dimensional code
After loading Zxing library, you can directly use the library directly generate two-dimensional code
1, obtains the encoded data Bitmatrix
BitMatrix Zxing library is a two-dimensional code defined in the data classes. This method is mainly generated two-dimensional code BitMatrix, it is actually a matrix, a two-dimensional array. And fault tolerance must be provided: fault tolerance rate of M, L, Q, H several levels, the higher fault tolerance, the more effective pixel two-dimensional code. As used herein, lowercase utf-8 encoded.
//生成二维码
public static Bitmap generateQRCode(String content, int width, int height) {
try {
HashMap<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>();
// 设置编码方式utf-8
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
//设置二维码的纠错级别为h
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
BitMatrix matrix = new QRCodeWriter().encode(content,BarcodeFormat.QR_CODE, width, height, hints);
//BitMatrix matrix = new MultiFormatWriter().encode(content,BarcodeFormat.QR_CODE, width, height, hints);
return bitMatrix2Bitmap(matrix);
} catch (WriterException e) {
e.printStackTrace();
}
return null;
}
2, converts the data into a Bitmap Bitmatrix
After this method Bitmatrix eligible converted into Bitmap, they can freely show.
private static Bitmap bitMatrix2Bitmap(BitMatrix matrix) {
int WIDTH = matrix.getWidth();
int HEIGHT = matrix.getHeight();
int[] pixels = new int[WIDTH * HEIGHT];
for (int y = 0; y < WIDTH; y++) {
for (int x = 0; x < HEIGHT; x++) {
int color = Color.WHITE;
if (matrix.get(x, y)) {
// 有内容的部分,颜色设置为渐变,当然这里可以自己修改成喜欢的颜色
color = 0xFF0094FF + y/2;// 蓝色
// if (x < WIDTH / 2 && y < HEIGHT / 2) {
// color = 0xFF0094FF;// 蓝色
// //Integer.toHexString(new Random().nextInt());
// } else if (x < WIDTH / 2 && y > HEIGHT / 2) {
// color = 0xFFFED545;// 黄色
// } else if (x > WIDTH / 2 && y > HEIGHT / 2) {
// color = 0xFF5ACF00;// 绿色
// } else {
// color = 0xFF000000;// 黑色
// }
}
pixels[x + (y * WIDTH)] = color;
}
}
Bitmap bitmap = Bitmap.createBitmap(WIDTH, HEIGHT, Bitmap.Config.RGB_565);
bitmap.setPixels(pixels, 0, WIDTH, 0, 0, WIDTH, HEIGHT);
return bitmap;
}
This will generate a color two-dimensional code. Required to generate a modified portion below the other colors. (Shielding portion is four colors)
3. Results
Add logo
The following code can be seen in the middle of the picture to give two-dimensional code logo, but images can only occupy a small part of the overall picture of the two-dimensional code, here it is 1/5 the size.
1, upload pictures
The above drawable picture copied to the directory in android (FIG test2 image is on)
Android convert resource files into Bitmap
MakeQRCodeUtil.gainBitmap(this, R.drawable.test2);
public static Bitmap gainBitmap(Context context, int drawableId) {
Bitmap bmp = BitmapFactory.decodeResource(context.getResources(),
drawableId);
return bmp;
}
2, add a logo
Incoming Bittmp generate two-dimensional code and logo of
//添加logo
private static Bitmap addLogo(Bitmap src, Bitmap logo) {
if (logo == null) {
return src;
}
// 获取图片的宽高
int srcWidth = src.getWidth();
int srcHeight = src.getHeight();
int logoWidth = logo.getWidth();
int logoHeight = logo.getHeight();
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.ALL_SAVE_FLAG);
canvas.restore();
} catch (Exception e) {
bitmap = null;
e.getStackTrace();
}
return bitmap;
}
3. Results
Add a background
1, upload pictures
As the same logo image converted into Bittmp
2, add background
传入生成二维码和背景图的Bittmp(背景图不要小于二维码图)
/**
* 给二维码图片加背景
*/
public static Bitmap addBackground(Bitmap foreground, Bitmap background) {
int bgWidth = background.getWidth();
int bgHeight = background.getHeight();
int fgWidth = foreground.getWidth();
int fgHeight = foreground.getHeight();
Bitmap newmap = Bitmap
.createBitmap(bgWidth, bgHeight, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(newmap);
canvas.drawBitmap(background, 0, 0, null);
canvas.drawBitmap(foreground, (bgWidth - fgWidth) / 2,
(bgHeight - fgHeight) * 2 / 5 + 70, null);
canvas.save(Canvas.ALL_SAVE_FLAG);
canvas.restore();
return newmap;
}
3、结果
位置就没有调整。可以自己在addBackground方法中调整下。
添加文字
直接添加文字即可
public static Bitmap addTextToBitmap(Bitmap bmpSrc, String text) {
int srcWidth = bmpSrc.getWidth();
int srcHeight = bmpSrc.getHeight();
// 先计算text所需要的height
int textSize = 20;
int padding = 3;
int textLinePadding = 1;
// 每行的文字
int perLineWords = (srcWidth - 2 * padding) / textSize;
int lineNum = text.length() / perLineWords;
lineNum = text.length() % perLineWords == 0 ? lineNum : lineNum + 1;
int textTotalHeight = lineNum * (textSize + textLinePadding) + 2 * padding;
Bitmap bitmap = Bitmap.createBitmap(srcWidth, srcHeight + textTotalHeight,
Bitmap.Config.ARGB_8888);
try {
Canvas canvas = new Canvas(bitmap);
canvas.drawColor(Color.WHITE);
canvas.drawBitmap(bmpSrc, 0, 0, null);
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setColor(Color.BLACK);
paint.setTextSize(textSize);
String lineText;
for (int i = 0, startY = srcHeight + textSize, start, end; i < lineNum; i++) {
start = i * perLineWords;
end = start + perLineWords;
lineText = text.substring(start, end > text.length() ? text.length() : end);
canvas.drawText(lineText, padding, startY, paint);
startY += textSize + textLinePadding;
}
canvas.save(Canvas.ALL_SAVE_FLAG);
canvas.restore();
} catch (Exception e) {
bitmap = null;
e.getStackTrace();
}
return bitmap;
}
结果
添加水印
水印图
代码
public static Bitmap composeWatermark(Bitmap srcBMP, Bitmap markBMP) {
if (srcBMP == null) {
return null;
}
// 创建一个新的和SRC长度宽度一样的位图
Bitmap newb = Bitmap.createBitmap(srcBMP.getWidth(),
srcBMP.getHeight(), Bitmap.Config.ARGB_8888);
Canvas cv = new Canvas(newb);
// 在 0,0坐标开始画入原图
cv.drawBitmap(srcBMP, 0, 0, null);
// 在原图的右下角画入水印
cv.drawBitmap(markBMP, srcBMP.getWidth() - markBMP.getWidth() * 2 / 5,
srcBMP.getHeight() * 6 / 7, null);
// 保存
cv.save(Canvas.ALL_SAVE_FLAG);
// 存储
cv.restore();
return newb;
}
结果
只有一个头头,为了让你发现它我还特意弄了个箭头,当然正常的水印也不会有这么大。
源码
地址:【源码】二维码生成及美化
结束语
以上就是跟二维码生成以及美化相关接口的总结,希望对你有用。欢迎大家关注我们微信公众号,来交流程序员的技术。如果能留言或者点个赞,我也是很开心的,非常感谢!
参考:
https://blog.csdn.net/u013749540/article/details/65937320
侵删,谢谢