生成二维码并将logo图片合成到二维码上main
async createQrCodeLogo(content: string) {
let codePixelMap = await ScanUtil.generateBarcode(content, {
height: 400,
width: 400,
scanType: scanCore.ScanType.QR_CODE,
level: generateBarcode.ErrorCorrectionLevel.LEVEL_H
});
const logoPixelMap = await ImageUtil.getPixelMapFromMedia(getContext(), $r('app.media.app_img'))
const synthesisPixelMap = await ImageUtil.synthesis(logoPixelMap, codePixelMap, {
height: 60, width: 60
})
const qrCodeBase64 = await ImageUtil.pixelMapToBase64Str(synthesisPixelMap);
}
合成logo
static async synthesis(
logoPixelMap: PixelMap,
codePixelMap: PixelMap,
logoSize: Size): Promise<PixelMap> {
const imageInfo = codePixelMap.getImageInfoSync()
const settings: RenderingContextSettings = new RenderingContextSettings(true);
const offCanvas: OffscreenCanvas = new OffscreenCanvas(imageInfo.size.width, imageInfo.size.height)
const offContext = offCanvas.getContext("2d", settings)
offContext.drawImage(codePixelMap, 0, 0,
imageInfo.size.width, imageInfo.size.height, 0, 0,
imageInfo.size.width, imageInfo.size.height);
offContext.drawImage(
logoPixelMap, (imageInfo.size.width - logoSize.width) / 2,
(imageInfo.size.height - logoSize.height) / 2, logoSize.height, logoSize.width);
return Promise.resolve(offContext.getPixelMap(0, 0, offCanvas.width, offCanvas.height))
}