最近有个需求,给一个模板图片,生成每个店铺自己的收款码,模板大小固定
图片长这样

生成这样

步骤
1.加载模板图片,id
是图片id
Bitmap bgBitmap = decodeResource(getResources(), id);
2.用模板生长一张画布
Canvas canvas = new Canvas(bgBitmap);
2.创建一个画笔画上店铺名称
Paint textPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
textPaint.setColor(Color.BLACK);
textPaint.setStyle(Paint.Style.FILL);
textPaint.setTextSize(txtSize);
textPaint.setAntiAlias(true);
textPaint.setTextAlign(Paint.Align.CENTER);
canvas.drawText("店铺名称", x, y,textPaint);
3.画上二维码
Bitmap qrBitmap =QRCodeUtil.createQRCodeBitmapMargin(s, qrCodeSize, 0);
Rect mSrcRect = new Rect(left, imgMarginTop, left + qrCodeWidth, bottom);
canvas.drawBitmap(qrBitmap, null, mSrcRect, new Paint(Paint.ANTI_ALIAS_FLAG));
注意这里要用canvas
的drawBitmap(@NonNull Bitmap bitmap, @Nullable Rect src, @NonNull Rect dst, @Nullable Paint paint)
方法,单纯的用drawBitmap(@NonNull Bitmap bitmap, float left, float top, @Nullable Paint paint)
方法画出的二维码在不同的机型上大小不同,原因不知,希望有知道的大神指点下
4.大功告成,保存到本地吧
FileUtils.saveFile(mContext, bgBitmap, "Order365收款码.jpg", filePath);
网友评论