Qt + QR-Code-generator 生成二维码

0.前言

之前使用 libgrencode 生成二维码,LGPL 协议实在不方便,所以需要找一个 github 星星多的,代码简单最好 header-only,协议最好是 MIT 或者兼容协议而不是 GPL 或者 LPGL。

QR-Code-generator 正好符合这个要求,而且还提供 C / C++ / Java / Python / JS / Rust 等 6 种语言实现,直接把 qrcodegen.hpp / cpp 类文件放到我们项目里就能使用。

库链接:https://github.com/nayuki/QR-Code-generator

库官网:https://www.nayuki.io/page/qr-code-generator-library

1.基本使用 

测试代码链接:https://github.com/gongjianbo/MyTestCode/tree/master/Qt/TestQt_20230711_QRCodeGen

效果展示:

 

测试代码:

void MainWindow::generate()
{
    // 使用utf8编码
    QByteArray str = ui->lineEdit->text().toUtf8();
    const char *text = str.constData();

    // 二维码有四个纠错等级,从低到高:L-%7/M-%15/Q-%25/H-%30
    const qrcodegen::QrCode::Ecc level = qrcodegen::QrCode::Ecc::MEDIUM;

    // 生成二维码
    try
    {
        // 使用简易接口,默认 Version 范围 min=1, max=40
        // const qrcodegen::QrCode qr = qrcodegen::QrCode::encodeText(text, level);
        // 也可以指定符号版本,越大可容纳的信息越多,如果内容超出容量范围会抛异常
        // 根据规范,Version1 是 21x21,Version2 是 25x25,每增加一个Version,就比前一版本每边增加 4 个模块
        std::vector<qrcodegen::QrSegment> segs = qrcodegen::QrSegment::makeSegments(text);
        const qrcodegen::QrCode qr = qrcodegen::QrCode::encodeSegments(segs, level, 1, 40);
        const int size = qr.getSize();
        // 填充位图
        QImage image = QImage(size, size, QImage::Format_Grayscale8);
        for (int row = 0; row < size; ++row)
        {
            uchar *line_ptr = image.scanLine(row);
            for (int col = 0; col < size; ++col)
            {
                line_ptr[col] = (uchar)(qr.getModule(row, col) ? 0x00 : 0xFF);
            }
        }
        // 放大一点看得更清楚
        image = image.scaled(image.width() * 5, image.height() * 5);

        // 生成后可以用手机扫一扫识别文字内容,注意内容为空可能扫不出来
        ui->label->setPixmap(QPixmap::fromImage(image));
    }
    catch(std::invalid_argument e)
    {
        // 参数异常,如 minVersion > maxVersion
        qDebug() << "catch invalid_argument" << e.what();
    }
    catch(qrcodegen::data_too_long e)
    {
        // 内容太长
        qDebug() << "catch data_too_long" << e.what();
    }
}

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Spring Boot整合MyBatis-Plus可以通过使用代码生成器来自动生成CRUD代码。下面是使用步骤: 1. 首先,在pom.xml文件中添加MyBatis-Plus和相关依赖: ```xml <!-- MyBatis-Plus依赖 --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>最新版本号</version> </dependency> <!-- MyBatis-Plus代码生成器依赖 --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-generator</artifactId> <version>最新版本号</version> </dependency> ``` 2. 创建一个配置类来配置代码生成器: ```java @Configuration public class MyBatisPlusConfig { @Value("${spring.datasource.url}") private String url; @Value("${spring.datasource.username}") private String username; @Value("${spring.datasource.password}") private String password; @Value("${spring.datasource.driver-class-name}") private String driverClassName; @Value("${mybatis-plus.package}") private String packageName; @Value("${mybatis-plus.author}") private String author; @Value("${mybatis-plus.table-prefix}") private String tablePrefix; @Bean public DataSource dataSource() { DruidDataSource dataSource = new DruidDataSource(); dataSource.setUrl(url); dataSource.setUsername(username); dataSource.setPassword(password); dataSource.setDriverClassName(driverClassName); return dataSource; } @Bean public GlobalConfig globalConfig() { GlobalConfig globalConfig = new GlobalConfig(); globalConfig.setAuthor(author); globalConfig.setOpen(false); return globalConfig; } @Bean public DataSourceConfig dataSourceConfig() { DataSourceConfig dataSourceConfig = new DataSourceConfig(); dataSourceConfig.setDbType(DbType.MYSQL); dataSourceConfig.setDriverName(driverClassName); dataSourceConfig.setUsername(username); dataSourceConfig.setPassword(password); dataSourceConfig.setUrl(url); return dataSourceConfig; } @Bean public PackageConfig packageConfig() { PackageConfig packageConfig = new PackageConfig(); packageConfig.setParent(packageName); packageConfig.setEntity("entity"); packageConfig.setMapper("mapper"); packageConfig.setService("service"); packageConfig.setServiceImpl("service.impl"); packageConfig.setController("controller"); return packageConfig; } @Bean public StrategyConfig strategyConfig() { StrategyConfig strategyConfig = new StrategyConfig(); strategyConfig.setEntityColumnConstant(true); strategyConfig.setEntityLombokModel(true); strategyConfig.setNaming(NamingStrategy.underline_to_camel); strategyConfig.setInclude("表名1", "表名2"); // 生成指定的表 strategyConfig.setRestControllerStyle(true); strategyConfig.setTablePrefix(tablePrefix); // 设置表前缀 return strategyConfig; } @Bean public TemplateConfig templateConfig() { TemplateConfig templateConfig = new TemplateConfig(); templateConfig.setController(null); // 不生成controller层 return templateConfig; } @Bean public InjectionConfig injectionConfig() { Map<String, Object> map = new HashMap<>(); return new InjectionConfig() { @Override public void initMap() { this.setMap(map); } }; } @Bean public AutoGenerator autoGenerator() { AutoGenerator autoGenerator = new AutoGenerator(); autoGenerator.setGlobalConfig(globalConfig()); autoGenerator.setDataSource(dataSourceConfig()); autoGenerator.setPackageInfo(packageConfig()); autoGenerator.setStrategy(strategyConfig()); autoGenerator.setTemplate(templateConfig()); autoGenerator.setCfg(injectionConfig()); return autoGenerator; } } ``` 3. 在application.properties或application.yml文件中配置相关信息: ```properties # 数据源配置 spring.datasource.url=数据库URL spring.datasource.username=数据库用户名 spring.datasource.password=数据库密码 spring.datasource.driver-class-name=数据库驱动类名 # MyBatis-Plus配置 mybatis-plus.package=你的包路径 mybatis-plus.author=代码作者名 mybatis-plus.table-prefix=表前缀 ``` 4. 创建一个启动类,添加@SpringBootApplication和@EnableAutoConfiguration注解,并在main方法中调用代码生成器生成代码: ```java @SpringBootApplication @EnableAutoConfiguration public class MyApplication { public static void main(String[] args) { SpringApplication.run(MyApplication.class, args); // 调用代码生成器生成代码 ApplicationContext context = SpringApplication.run(MyApplication.class, args); AutoGenerator autoGenerator = context.getBean(AutoGenerator.class); autoGenerator.execute(); } } ``` 5. 运行启动类,执行代码生成器。生成的代码将包含实体类、Mapper接口、Service接口、Service实现类和Controller类。 这样就能根据数据库表自动生成CRUD代码了。如果需要生成其他的代码,可以根据需要配置生成器相应的参数和模板。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

龚建波

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值