init
This commit is contained in:
Vendored
BIN
Binary file not shown.
@@ -0,0 +1,6 @@
|
||||
## 1.0.2(2025-03-04)
|
||||
优化
|
||||
## 1.0.1(2025-02-10)
|
||||
初始化
|
||||
## 1.0.0(2025-02-10)
|
||||
初始化
|
||||
@@ -0,0 +1,91 @@
|
||||
{
|
||||
"id": "xtf-qrcode",
|
||||
"displayName": "二维码生成 qrcode uts ",
|
||||
"version": "1.0.2",
|
||||
"description": "android/ios/web 通过平台sdk 生成二维码返回base64格式数据,可自行设置显示,动态二维码",
|
||||
"keywords": [
|
||||
"二维码",
|
||||
"生成二维码",
|
||||
"动态二维码",
|
||||
"二维码生成",
|
||||
"qrcode"
|
||||
],
|
||||
"repository": "",
|
||||
"engines": {
|
||||
"HBuilderX": "^3.6.8"
|
||||
},
|
||||
"dcloudext": {
|
||||
"type": "uts",
|
||||
"sale": {
|
||||
"regular": {
|
||||
"price": "1.99"
|
||||
},
|
||||
"sourcecode": {
|
||||
"price": "10.00"
|
||||
}
|
||||
},
|
||||
"contact": {
|
||||
"qq": ""
|
||||
},
|
||||
"declaration": {
|
||||
"ads": "无",
|
||||
"data": "无",
|
||||
"permissions": "无"
|
||||
},
|
||||
"npmurl": ""
|
||||
},
|
||||
"uni_modules": {
|
||||
"dependencies": [],
|
||||
"encrypt": [],
|
||||
"platforms": {
|
||||
"cloud": {
|
||||
"tcb": "y",
|
||||
"aliyun": "y",
|
||||
"alipay": "y"
|
||||
},
|
||||
"client": {
|
||||
"Vue": {
|
||||
"vue2": "y",
|
||||
"vue3": "y"
|
||||
},
|
||||
"App": {
|
||||
"app-android": "y",
|
||||
"app-ios": "y",
|
||||
"app-harmony": "n"
|
||||
},
|
||||
"H5-mobile": {
|
||||
"Safari": "y",
|
||||
"Android Browser": "y",
|
||||
"微信浏览器(Android)": "y",
|
||||
"QQ浏览器(Android)": "y"
|
||||
},
|
||||
"H5-pc": {
|
||||
"Chrome": "y",
|
||||
"IE": "y",
|
||||
"Edge": "y",
|
||||
"Firefox": "y",
|
||||
"Safari": "y"
|
||||
},
|
||||
"小程序": {
|
||||
"微信": "n",
|
||||
"阿里": "u",
|
||||
"百度": "u",
|
||||
"字节跳动": "u",
|
||||
"QQ": "u",
|
||||
"钉钉": "u",
|
||||
"快手": "u",
|
||||
"飞书": "u",
|
||||
"京东": "u"
|
||||
},
|
||||
"快应用": {
|
||||
"华为": "u",
|
||||
"联盟": "u"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"dependencies": {
|
||||
"qrcode": "^1.5.4",
|
||||
"weapp-qrcode": "^1.0.0"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
# xtf-qrcode
|
||||
#### 若web 端报错,可用解压插件目录web.zip覆盖web文件夹文件
|
||||
### uniappx
|
||||
```
|
||||
import {QrOpt,getQrData} from "@/uni_modules/xtf-qrcode"
|
||||
|
||||
getQrData({
|
||||
content:"xtf",
|
||||
logo:"/static/logo.png",
|
||||
logoW:80,
|
||||
logoH:80,
|
||||
qrSize:300,
|
||||
callback:function(data:string){
|
||||
// that.img=data;
|
||||
//data 为base64图片
|
||||
}
|
||||
} as QrOpt);
|
||||
|
||||
```
|
||||
### uniapp
|
||||
```
|
||||
import {getQrData} from "@/uni_modules/xtf-qrcode"
|
||||
|
||||
getQrData({
|
||||
content:"xtf",
|
||||
logo:"/static/logo.png",
|
||||
logoW:80,
|
||||
logoH:80,
|
||||
qrSize:300,
|
||||
callback:function(data:string){
|
||||
// that.img=data;
|
||||
//data 为base64图片
|
||||
}
|
||||
});
|
||||
|
||||
```
|
||||
二维码logo 宽高 建议为二维码大小的1/5 左右
|
||||
### 开发文档
|
||||
[UTS 语法](https://uniapp.dcloud.net.cn/tutorial/syntax-uts.html)
|
||||
[UTS API插件](https://uniapp.dcloud.net.cn/plugin/uts-plugin.html)
|
||||
[UTS uni-app兼容模式组件](https://uniapp.dcloud.net.cn/plugin/uts-component.html)
|
||||
[UTS 标准模式组件](https://doc.dcloud.net.cn/uni-app-x/plugin/uts-vue-component.html)
|
||||
[Hello UTS](https://gitcode.net/dcloud/hello-uts)
|
||||
BIN
Binary file not shown.
@@ -0,0 +1,171 @@
|
||||
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.Canvas;
|
||||
import android.graphics.Color;
|
||||
import android.graphics.pdf.PdfRenderer;
|
||||
import android.os.ParcelFileDescriptor;
|
||||
import android.text.TextUtils;
|
||||
import android.util.Log;
|
||||
import android.util.Base64;
|
||||
|
||||
import com.google.zxing.BarcodeFormat;
|
||||
import com.google.zxing.BinaryBitmap;
|
||||
import com.google.zxing.ChecksumException;
|
||||
import com.google.zxing.EncodeHintType;
|
||||
import com.google.zxing.FormatException;
|
||||
import com.google.zxing.MultiFormatWriter;
|
||||
import com.google.zxing.NotFoundException;
|
||||
import com.google.zxing.RGBLuminanceSource;
|
||||
import com.google.zxing.Result;
|
||||
import com.google.zxing.WriterException;
|
||||
import com.google.zxing.common.BitMatrix;
|
||||
import com.google.zxing.common.CharacterSetECI;
|
||||
import com.google.zxing.common.HybridBinarizer;
|
||||
import com.google.zxing.pdf417.PDF417Writer;
|
||||
import com.google.zxing.pdf417.encoder.Compaction;
|
||||
import com.google.zxing.pdf417.encoder.Dimensions;
|
||||
import com.google.zxing.pdf417.encoder.PDF417;
|
||||
import com.google.zxing.qrcode.QRCodeReader;
|
||||
import com.google.zxing.qrcode.QRCodeWriter;
|
||||
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
|
||||
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.HashMap;
|
||||
import java.util.Hashtable;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 二维码生成工具类
|
||||
*/
|
||||
public class QRCodeGenerator {
|
||||
private static final String TAG = "ZxingUtil";
|
||||
|
||||
|
||||
/**
|
||||
* 在二维码中间添加Logo图案
|
||||
*/
|
||||
private static Bitmap addLogo(Bitmap src,int logoW,int logoH, Bitmap logo) {
|
||||
if (src == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (logo == null) {
|
||||
return src;
|
||||
}
|
||||
logo=resizeBitmap(logo,logoW,logoH);
|
||||
// 获取图片的宽高
|
||||
int srcWidth = src.getWidth();
|
||||
int srcHeight = src.getHeight();
|
||||
int logoWidth = logo.getWidth();
|
||||
int logoHeight = logo.getHeight();
|
||||
|
||||
if (srcWidth == 0 || srcHeight == 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
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.restore();
|
||||
} catch (Exception e) {
|
||||
bitmap = null;
|
||||
e.getStackTrace();
|
||||
}
|
||||
|
||||
return bitmap;
|
||||
}
|
||||
public static Bitmap generateQRCodeWithLogo(String content, int widthPix,
|
||||
int heightPix,int logoW,int logoH, Bitmap logoBm) {
|
||||
|
||||
if (content == null || "".equals(content)) {
|
||||
return null;
|
||||
}
|
||||
Bitmap bitmap = null;
|
||||
// 配置参数
|
||||
Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>();
|
||||
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
|
||||
// 容错级别
|
||||
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
|
||||
// 设置空白边距的宽度
|
||||
hints.put(EncodeHintType.MARGIN, 0); //default is 4
|
||||
|
||||
|
||||
// 图像数据转换,使用了矩阵转换
|
||||
BitMatrix bitMatrix;
|
||||
try {
|
||||
bitMatrix = new QRCodeWriter().encode(content,
|
||||
BarcodeFormat.QR_CODE, widthPix, heightPix, hints);
|
||||
|
||||
int[] pixels = new int[widthPix * heightPix];
|
||||
// 下面这里按照二维码的算法,逐个生成二维码的图片,
|
||||
// 两个for循环是图片横列扫描的结果
|
||||
for (int y = 0; y < heightPix; y++) {
|
||||
for (int x = 0; x < widthPix; x++) {
|
||||
if (bitMatrix.get(x, y)) {
|
||||
pixels[y * widthPix + x] = 0xff000000;
|
||||
} else {
|
||||
pixels[y * widthPix + x] = 0xffffffff;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 生成二维码图片的格式,使用ARGB_8888
|
||||
bitmap = Bitmap.createBitmap(widthPix, heightPix,
|
||||
Bitmap.Config.ARGB_8888);
|
||||
bitmap.setPixels(pixels, 0, widthPix, 0, 0, widthPix, heightPix);
|
||||
|
||||
if (logoBm != null) {
|
||||
bitmap = addLogo(bitmap, logoW,logoH,logoBm);
|
||||
}
|
||||
// if(!TextUtils.isEmpty(filePath)){
|
||||
|
||||
|
||||
// bitmap.compress(Bitmap.CompressFormat.JPEG, 100,
|
||||
// new FileOutputStream(filePath));}
|
||||
} catch (WriterException e) {
|
||||
//
|
||||
e.printStackTrace();
|
||||
}
|
||||
return bitmap;
|
||||
|
||||
}
|
||||
|
||||
|
||||
public static Bitmap resizeBitmap(Bitmap originalBitmap, int newWidth, int newHeight) {
|
||||
// 创建一个新的Bitmap对象,指定缩放后的宽度、高度和配置(这里使用与原Bitmap相同的配置)
|
||||
Bitmap resizedBitmap = Bitmap.createScaledBitmap(originalBitmap, newWidth, newHeight, true);
|
||||
return resizedBitmap;
|
||||
}
|
||||
|
||||
|
||||
public static String convert(Bitmap bitmap) {
|
||||
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
|
||||
// 压缩Bitmap到字节数组中,格式为PNG,质量为100%
|
||||
bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
|
||||
byte[] byteArray = byteArrayOutputStream.toByteArray();
|
||||
// 将字节数组编码为Base64字符串
|
||||
String encoded = Base64.encodeToString(byteArray, Base64.DEFAULT);
|
||||
return encoded;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
public class Test{
|
||||
public static String test(){
|
||||
return "xtf";
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
{
|
||||
"minSdkVersion": "19",
|
||||
"dependencies": [
|
||||
"com.google.zxing:core:3.4.1",
|
||||
"com.journeyapps:zxing-android-embedded:4.3.0"
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
import BitmapFactory from 'android.graphics.BitmapFactory';
|
||||
|
||||
export type QrOpt = {
|
||||
content:string,
|
||||
logo?:string,
|
||||
qrSize?:number,
|
||||
logoW?:number,
|
||||
logoH?:number,
|
||||
marign?:number,
|
||||
callback:(data:string)=>void
|
||||
}
|
||||
|
||||
export function getQrData (opt:QrOpt):void{
|
||||
if(opt.qrSize==null){
|
||||
opt.qrSize=300;
|
||||
}
|
||||
console.log("--------------")
|
||||
if(opt.logo==null){
|
||||
opt.callback("data:image/jpg;base64,"+QRCodeGenerator.convert(QRCodeGenerator.generateQRCodeWithLogo(opt.content,opt.qrSize!.toInt(),opt.qrSize!.toInt(),(0).toInt(),(0).toInt(),null)));
|
||||
}else{
|
||||
try{
|
||||
var logo= UTSAndroid.getResourcePath(opt.logo!);
|
||||
if(opt.logoW==null){
|
||||
opt.logoW=50;
|
||||
}
|
||||
if(opt.logoH==null){
|
||||
opt.logoW=50;
|
||||
}
|
||||
var bitmap= BitmapFactory.decodeFile(logo);
|
||||
|
||||
opt.callback("data:image/jpg;base64,"+QRCodeGenerator.convert(QRCodeGenerator.generateQRCodeWithLogo(opt.content,opt.qrSize!.toInt(),opt.qrSize!.toInt(),opt.logoW!.toInt(),opt.logoH!.toInt(),bitmap)));
|
||||
|
||||
}catch(e:Exception){
|
||||
opt.callback("data:image/jpg;base64,"+QRCodeGenerator.convert(QRCodeGenerator.generateQRCodeWithLogo(opt.content,opt.qrSize!.toInt(),opt.qrSize!.toInt(),(0).toInt(),(0).toInt(),null)));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
{
|
||||
"deploymentTarget": "12"
|
||||
|
||||
// implementation 'com.google.zxing:core:3.4.1'
|
||||
// implementation 'com.journeyapps:zxing-android-embedded:4.3.0'
|
||||
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
|
||||
export type QrOpt = {
|
||||
content:string,
|
||||
logo?:string,
|
||||
qrSize?:number,
|
||||
logoW?:number,
|
||||
logoH?:number,
|
||||
marign?:number,
|
||||
callback:(data:string)=>void
|
||||
|
||||
|
||||
}
|
||||
|
||||
export function getQrData (opt:QrOpt):void{
|
||||
console.log(opt.content)
|
||||
if(opt.qrSize==null){
|
||||
opt.qrSize=300;
|
||||
}
|
||||
|
||||
if(opt.logo==null){
|
||||
var b=QR.createQrCode(opt.content,opt.qrSize!.toInt());
|
||||
console.log(b);
|
||||
opt.callback("data:image/jpg;base64,"+b)
|
||||
//return "data:image/jpg;base64,"+b;
|
||||
}else{
|
||||
var l=UTSiOS.getResourcePath(opt.logo!)
|
||||
if(opt.logoW==null){
|
||||
opt.logoW=50;
|
||||
|
||||
}
|
||||
if(opt.logoH==null){
|
||||
opt.logoH=50;
|
||||
}
|
||||
var b=QR.createQrCodeWithLogo(opt.content,opt.qrSize!.toInt(),l,opt.logoW!.toInt(),opt.logoH!.toInt());
|
||||
console.log(b);
|
||||
opt.callback("data:image/jpg;base64,"+b)
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
import CoreImage
|
||||
import UIKit
|
||||
import Foundation
|
||||
import SwiftUICore
|
||||
|
||||
public class QR{
|
||||
|
||||
|
||||
|
||||
static func imageToBase64(_ image: UIImage) -> String {
|
||||
if let imageData = image.jpegData(compressionQuality: 1.0) {
|
||||
// 使用imageData进行你的操作
|
||||
let base64String = imageData.base64EncodedString(options: [])
|
||||
return base64String
|
||||
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
static func createQrCode(_ data:String,_ size:Int)->String{
|
||||
// 使用示例
|
||||
var b:CGFloat=CGFloat(size);
|
||||
guard let qrCodeImage = QR.generateQRCode(with:data, qrSize: b)
|
||||
else{
|
||||
return "";
|
||||
}
|
||||
let q = QR.imageToBase64(qrCodeImage)
|
||||
return q
|
||||
}
|
||||
|
||||
|
||||
static func createQrCodeWithLogo(_ data:String,_ size:Int,_ logo: String,_ logoW:Int,_ logoH:Int)->String{
|
||||
// 使用示例
|
||||
var b:CGFloat=CGFloat(size);
|
||||
var w:CGFloat=CGFloat(logoW);
|
||||
var h:CGFloat=CGFloat(logoH);
|
||||
var logoSize:CGSize=CGSize(width: w, height: h)
|
||||
guard let qrCodeImage = QR.generateQRCode(with:data,logoName:logo, logoSize:logoSize, qrSize: b)
|
||||
else{
|
||||
return "";
|
||||
}
|
||||
let q = QR.imageToBase64(qrCodeImage)
|
||||
return q
|
||||
}
|
||||
// 生成二维码图像,并可选地添加logo
|
||||
static func generateQRCode(with content: String, logoName: String? = nil, logoSize: CGSize = CGSize(width: 50, height: 50), qrSize: CGFloat = 200) -> UIImage? {
|
||||
// 1. 生成二维码
|
||||
guard let qrFilter = CIFilter(name: "CIQRCodeGenerator") else { return nil }
|
||||
let data = content.data(using: .utf8)
|
||||
qrFilter.setValue(data, forKey: "inputMessage")
|
||||
qrFilter.setValue("H", forKey: "inputCorrectionLevel") // 高纠错级别
|
||||
guard let qrImage = qrFilter.outputImage else { return nil }
|
||||
|
||||
// 2. 调整二维码尺寸
|
||||
let scaleTransform = CGAffineTransform(scaleX: qrSize / qrImage.extent.size.width, y: qrSize / qrImage.extent.size.height)
|
||||
let scaledQrImage = qrImage.transformed(by: scaleTransform)
|
||||
|
||||
// 3. 创建UIImage对象
|
||||
guard let qrUIImage:UIImage? = UIImage(ciImage: scaledQrImage) else { return nil }
|
||||
|
||||
|
||||
// if let image: UIImage? = someFunctionThatReturnsUIImage() {
|
||||
// // 这里image已经被解包,并且是非空的
|
||||
// } else {
|
||||
// // 处理image为nil的情况
|
||||
// }
|
||||
// 4. 添加logo(如果有)
|
||||
if let logoName = logoName, let logoImage = UIImage(named: logoName) {
|
||||
let logoFrame = CGRect(x: (qrUIImage!.size.width - logoSize.width) / 2,
|
||||
y: (qrUIImage!.size.height - logoSize.height) / 2,
|
||||
width: logoSize.width,
|
||||
height: logoSize.height)
|
||||
|
||||
UIGraphicsBeginImageContextWithOptions(qrUIImage!.size, false, 0.0)
|
||||
qrUIImage?.draw(in: CGRect(origin: .zero, size: qrUIImage!.size))
|
||||
logoImage.draw(in: logoFrame)
|
||||
let finalImage = UIGraphicsGetImageFromCurrentImageContext()
|
||||
UIGraphicsEndImageContext()
|
||||
|
||||
return finalImage
|
||||
}
|
||||
|
||||
// 如果没有logo,直接返回二维码图像
|
||||
return qrUIImage
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
/**
|
||||
* interface.uts
|
||||
* uts插件接口定义文件,按规范定义接口文件可以在HBuilderX中更好的做到语法提示
|
||||
*/
|
||||
|
||||
/**
|
||||
* myApi 异步函数的参数,在type里定义函数需要的参数以及api成功、失败的相关回调函数。
|
||||
*/
|
||||
export type MyApiOptions = {
|
||||
paramA : boolean
|
||||
success ?: (res : MyApiResult) => void
|
||||
fail ?: (res : MyApiFail) => void
|
||||
complete ?: (res : any) => void
|
||||
}
|
||||
|
||||
/**
|
||||
* 函数返回结果
|
||||
* 可以是void, 基本数据类型,自定义type, 或者其他类型。
|
||||
* [可选实现]
|
||||
*/
|
||||
export type MyApiResult = {
|
||||
fieldA : number,
|
||||
fieldB : boolean,
|
||||
fieldC : string
|
||||
}
|
||||
|
||||
/**
|
||||
* 错误码
|
||||
* 根据uni错误码规范要求,建议错误码以90开头,以下是错误码示例:
|
||||
* - 9010001 错误信息1
|
||||
* - 9010002 错误信息2
|
||||
*/
|
||||
export type MyApiErrorCode = 9010001 | 9010002;
|
||||
/**
|
||||
* myApi 的错误回调参数
|
||||
*/
|
||||
export interface MyApiFail extends IUniError {
|
||||
errCode : MyApiErrorCode
|
||||
};
|
||||
|
||||
/* 异步函数定义 */
|
||||
export type MyApi = (options : MyApiOptions) => void
|
||||
|
||||
/* 同步函数定义 */
|
||||
export type MyApiSync = (paramA : boolean) => MyApiResult
|
||||
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
/* 此规范为 uni 规范,可以按照自己的需要选择是否实现 */
|
||||
import { MyApiErrorCode, MyApiFail } from "./interface.uts"
|
||||
/**
|
||||
* 错误主题
|
||||
* 注意:错误主题一般为插件名称,每个组件不同,需要使用时请更改。
|
||||
* [可选实现]
|
||||
*/
|
||||
export const UniErrorSubject = 'uts-api';
|
||||
|
||||
|
||||
/**
|
||||
* 错误信息
|
||||
* @UniError
|
||||
* [可选实现]
|
||||
*/
|
||||
export const MyAPIErrors : Map<MyApiErrorCode, string> = new Map([
|
||||
/**
|
||||
* 错误码及对应的错误信息
|
||||
*/
|
||||
[9010001, 'custom error mseeage1'],
|
||||
[9010002, 'custom error mseeage2'],
|
||||
]);
|
||||
|
||||
|
||||
/**
|
||||
* 错误对象实现
|
||||
*/
|
||||
export class MyApiFailImpl extends UniError implements MyApiFail {
|
||||
|
||||
/**
|
||||
* 错误对象构造函数
|
||||
*/
|
||||
constructor(errCode : MyApiErrorCode) {
|
||||
super();
|
||||
this.errSubject = UniErrorSubject;
|
||||
this.errCode = errCode;
|
||||
this.errMsg = MyAPIErrors.get(errCode) ?? "";
|
||||
}
|
||||
}
|
||||
Binary file not shown.
@@ -0,0 +1,97 @@
|
||||
|
||||
import { QRCode } from './qrcode.min.js';
|
||||
|
||||
export type QrOpt = {
|
||||
content:string,
|
||||
logo?:string,
|
||||
qrSize?:number,
|
||||
logoW?:number,
|
||||
logoH?:number,
|
||||
marign?:number,
|
||||
callback:(data:string)=>void
|
||||
}
|
||||
|
||||
export const getQrData = async function (opt:QrOpt){
|
||||
console.log(opt.content)
|
||||
if(opt.qrSize==null){
|
||||
opt.qrSize=300;
|
||||
}
|
||||
|
||||
if(opt.marign==null){
|
||||
opt.marign=1;
|
||||
}
|
||||
// 要生成的二维码内容
|
||||
// var qrContent = "https://www.example.com";
|
||||
|
||||
//使用QRCode.toDataURL()方法生成base64字符串
|
||||
// QRCode.toDataURL(opt.content, {
|
||||
// //errorCorrectionLevel: QRCode.CorrectLevel.H, // 容错级别
|
||||
// margin: 1, // 边距
|
||||
// width: opt.qrSize!, // 二维码宽度
|
||||
// height: opt.qrSize!, // 二维码高度
|
||||
// color: {
|
||||
// dark: "#000000", // 前景色(二维码颜色)
|
||||
// light: "#ffffff" // 背景色(空白区域颜色)
|
||||
// }
|
||||
// }).then(function(base64) {
|
||||
// // 在这里,base64是包含二维码图片的base64字符串
|
||||
// // console.log(base64);
|
||||
// opt.callback(base64);
|
||||
// // return base64;
|
||||
// // 你可以将base64字符串设置为img元素的src属性来显示二维码
|
||||
// // var img = document.createElement('img');
|
||||
// // img.src = base64;
|
||||
// // document.body.appendChild(img);
|
||||
|
||||
// // 你也可以将base64字符串用于其他目的,比如下载或传输
|
||||
// }).catch(function(error) {
|
||||
// console.error("生成二维码时出错:", error);
|
||||
// });
|
||||
|
||||
//return "";
|
||||
|
||||
|
||||
const qrcodeCanvas = document.createElement('canvas');
|
||||
qrcodeCanvas.width = opt.qrSize!;
|
||||
qrcodeCanvas.height = opt.qrSize!;
|
||||
|
||||
// 使用QRCode对象生成二维码
|
||||
await QRCode.toCanvas(qrcodeCanvas, opt.content, { width: opt.qrSize!,height: opt.qrSize!,margin: opt.marign!}, (error) => {
|
||||
if (error) console.error(error);
|
||||
});
|
||||
if(opt.logo==null){
|
||||
opt.callback(qrcodeCanvas.toDataURL('image/png'))
|
||||
return;
|
||||
}
|
||||
|
||||
// 获取logo图片
|
||||
const logoImage = new Image();
|
||||
logoImage.src = opt.logo!; // 替换为你的logo图片路径
|
||||
logoImage.onload = () => {
|
||||
// 创建一个用于合成带有logo的二维码的canvas元素
|
||||
const logoQRCodeCanvas = document.createElement('canvas');
|
||||
logoQRCodeCanvas.width = qrcodeCanvas.width;
|
||||
logoQRCodeCanvas.height = qrcodeCanvas.height;
|
||||
const ctx = logoQRCodeCanvas.getContext('2d');
|
||||
|
||||
// 将基本的二维码绘制到新的canvas上
|
||||
ctx.drawImage(qrcodeCanvas, 0, 0);
|
||||
|
||||
// 计算logo的大小和位置
|
||||
const logoSize = opt.logoW!; // logo的显示大小
|
||||
const logoX = (logoQRCodeCanvas.width - opt.logoW!) / 2;
|
||||
const logoY = (logoQRCodeCanvas.height - opt.logoH!) / 2;
|
||||
|
||||
// 在二维码上绘制logo
|
||||
ctx.drawImage(logoImage, logoX, logoY, logoSize, opt.logoH);
|
||||
|
||||
// 将带有logo的二维码canvas转换为base64图片
|
||||
const base64Image = logoQRCodeCanvas.toDataURL('image/png');
|
||||
|
||||
// 将base64图片存储到组件的数据中
|
||||
// this.qrcodeWithLogo = base64Image;
|
||||
opt.callback(base64Image)
|
||||
};
|
||||
|
||||
|
||||
}
|
||||
+11
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user