1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
| public static Bitmap genereateBitmap(PrintLabelModel model) {
int width = model.width; int height = model.height;
//最终生成的图片 Bitmap result = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_4444); Paint paint = new Paint(); paint.setAntiAlias(true); paint.setColor(Color.WHITE); Canvas canvas = new Canvas(result); //先画一整块白色矩形块 canvas.drawRect(0, 0, result.getWidth(), result.getHeight(), paint);
Paint fontPaint = new Paint(); fontPaint.setAntiAlias(true); fontPaint.setStyle(Paint.Style.FILL);
for (PrintLabelModel.BasePrint info : model.infos) { //8 dot=1mm. 1英寸=25.4mm.热 if (info instanceof PrintLabelModel.BarCode) { PrintLabelModel.BarCode current = (PrintLabelModel.BarCode) info; try { Bitmap bitmap = PrintBitmapUtil.encodeAsBitmap(current.content, BarcodeFormat.CODE_128, current.width, current.height); canvas.drawBitmap(bitmap, info.x, info.y, fontPaint); } catch (WriterException e) { throw new RuntimeException(e); }
} else if (info instanceof PrintLabelModel.QRCode) { PrintLabelModel.QRCode current = (PrintLabelModel.QRCode) info; Bitmap qrCodeBitmap = com.example.print_sdk.util.BitmapUtils.encode2dAsBitmap(current.content, current.size, current.size, 2); canvas.drawBitmap(qrCodeBitmap, info.x, info.y, fontPaint); if (current.showText) { }
} else if (info instanceof PrintLabelModel.Box) { PrintLabelModel.Box current = (PrintLabelModel.Box) info; Paint mypaint = new Paint(); mypaint.setAntiAlias(true); mypaint.setStyle(Paint.Style.FILL); mypaint.setColor(Color.BLACK); mypaint.setStyle(Paint.Style.STROKE); mypaint.setStrokeWidth(current.border); float strokeWidth = paint.getStrokeWidth(); canvas.drawRect(current.x - (strokeWidth / 2), current.y - (strokeWidth / 2), current.right - (strokeWidth / 2), current.bottom - (strokeWidth / 2), mypaint); } else if (info instanceof PrintLabelModel.Text) { PrintLabelModel.Text current = (PrintLabelModel.Text) info; fontPaint.setTextSize(current.fontsize); fontPaint.setColor(Color.BLACK); if (current.limitRight > 0) {
float x = current.x; float y = current.y; String line = ""; for (int i = 0; i < current.content.length(); i++) { char c = current.content.charAt(i); Rect bounds = new Rect(); fontPaint.getTextBounds(c + "", 0, 1, bounds);//230 test_box_1234的宽度 float charWidth = bounds.width();// paint.measureText(String.valueOf(c)); if (x + charWidth > current.limitRight) { canvas.drawText(line, current.x, y, fontPaint); line = String.valueOf(c); int fontHeight = bounds.bottom - bounds.top; y += fontHeight + current.newLineGap; // y += fontPaint.getFontSpacing()+ current.newLineGap; x = current.x + charWidth; } else { line += c; x += charWidth; } } canvas.drawText(line, current.x, y, fontPaint); } else { canvas.drawText(current.content, info.x, info.y, fontPaint);
}
} else if (info instanceof PrintLabelModel.TextLeftRight) { PrintLabelModel.TextLeftRight current = (PrintLabelModel.TextLeftRight) info; fontPaint.setTextSize(current.fontSize); fontPaint.setColor(Color.BLACK); String content = current.label + ":" + current.content;
if (current.limitRight > 0) {
float x = current.x; float y = current.y; String line = ""; for (int i = 0; i < content.length(); i++) { char c = content.charAt(i); float charWidth = fontPaint.measureText(String.valueOf(c)); if (x + charWidth > current.limitRight) { canvas.drawText(line, current.x, y, fontPaint);//把之前的给绘制掉 line = String.valueOf(c); y += fontPaint.getFontSpacing() + current.newLineGap; x = current.x + charWidth;//第二行 } else { line += c; x += charWidth; } } //没超过就是绘制第一行,超过就是绘制末尾的 canvas.drawText(line, current.x, y, fontPaint); } else { canvas.drawText(content , info.x, info.y, fontPaint);
}
} else {
} }
paint.setColor(Color.WHITE); canvas.save(); canvas.restore(); return result; }
|