/**
* 用于将给定的内容生成成一维码 注:目前生成内容为中文的话将直接报错,要修改底层jar包的内容
*
* @param content 将要生成一维码的内容
* @return 返回生成好的一维码bitmap
* @throws WriterException WriterException异常
*/
public static Bitmap CreateOneDCode(String content, int w, int h) throws WriterException {
BitMatrix matrix = new MultiFormatWriter().encode(content, BarcodeFormat.CODE_128, w, h);
int width = matrix.getWidth();
int height = matrix.getHeight();
int[] pixels = new int[width * height];
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
if (matrix.get(x, 0)) {
pixels[y * width + x] = 0xff000000;
}
else pixels[y * width + x] = 0xffffffff;
}
}
Bitmap bitmap = Bitmap.createBitmap(width, height,
Bitmap.Config.ARGB_8888);
bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
return bitmap;
}
/**
* 将字符串转成图片
* @param text
* @param width
* @param height
* @return
*/
private static final int TXT_SIZE = 24;
private static final int y_offset = 10;
public static Bitmap StringToBitmap(String text, int width, int height) {
Bitmap newBitmap = Bitmap.createBitmap(width,height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(newBitmap);
canvas.drawBitmap(newBitmap, 0, 0, null);
TextPaint textPaint = new TextPaint();
textPaint.setAntiAlias(true);
textPaint.setTextSize(TXT_SIZE);
textPaint.setColor(Color.BLACK);
StaticLayout sl= new StaticLayout(text, textPaint, newBitmap.getWidth()-8, Layout.Alignment.ALIGN_CENTER, 1.0f, 0.0f, false);
canvas.translate(0, y_offset);
canvas.drawColor(Color.WHITE);
sl.draw(canvas);
return newBitmap;
}
/**
* 连接两个bitmap
* 以第一个图为准
* 优化算法 1.图片不需要铺满,只需要以统一合适的宽度。然后让imageview自己去铺满,不然长图合成长图会崩溃,这里以第一张图为例
*2.只缩放不相等宽度的图片。已经缩放过的不需要再次缩放
* @param bit1
* @param bit2
* @return
*/
public static Bitmap CombBitmap(Bitmap bit1, Bitmap bit2) {
Bitmap newBit = null;
int width = bit1.getWidth();
if (bit2.getWidth() != width) {
int h2 = bit2.getHeight() * width / bit2.getWidth();
newBit = Bitmap.createBitmap(width, bit1.getHeight() + h2, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(newBit);
Bitmap newSizeBitmap2 = getNewSizeBitmap(bit2, width, h2);
canvas.drawBitmap(bit1, 0, 0, null);
canvas.drawBitmap(newSizeBitmap2, 0, bit1.getHeight(), null);
} else {
newBit = Bitmap.createBitmap(width, bit1.getHeight() + bit2.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(newBit);
canvas.drawBitmap(bit1, 0, 0, null);
canvas.drawBitmap(bit2, 0, bit1.getHeight(), null);
}
return newBit;
}
/**
* 获取新的bitmap
* @param bitmap
* @param newWidth
* @param newHeight
* @return
*/
public static Bitmap getNewSizeBitmap(Bitmap bitmap, int newWidth, int newHeight) {
float scaleWidth = ((float) newWidth) / bitmap.getWidth();
float scaleHeight = ((float) newHeight) / bitmap.getHeight();
// 取得想要缩放的matrix参数
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
// 得到新的图片
Bitmap bit1Scale = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix,
true);
return bit1Scale;
}
/**
* 创建带文字的一维码
* @param content
* @param w
* @param h
* @param stringPrefix
* @return
* @throws WriterException
*/
public static Bitmap CreateOneDCodeAndString(String content, int w, int h, String stringPrefix) throws WriterException {
return CombBitmap(CreateOneDCode(content, w, h-TXT_SIZE-y_offset), StringToBitmap(stringPrefix + content, w, TXT_SIZE + y_offset));
}
Android Zxing创建带文字的一维码(条形码)
最新推荐文章于 2025-10-21 11:56:03 发布
本文介绍了一维码的生成方法,包括处理中文字符的底层优化,以及如何将一维码与文字结合生成带有说明的条形码图片。同时,涵盖了图片缩放和拼接的技术实现,确保不同尺寸的图片能完美融合。
3125

被折叠的 条评论
为什么被折叠?



