栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java将文件转换成二维码

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java将文件转换成二维码

生成二维码主要代码为在步骤1、2中,下边后续步骤3、4、5涉及到具体的业务。要是用该工具类时需要拷贝前两个步骤代码即可。

1、在项目pom.xml文件中引入生成二维码的第三方依赖


    com.google.zxing
    core
    3.3.3

2、生成二维码的工具类
public class QRCodeUtils {

    private static final int BLACK = 0xFF000000;
    private static final int WHITE = 0xFFFFFFFF;

    
    public static void buildQuickMark(String content, String path, String filename) {
        try {
            BitMatrix byteMatrix = new MultiFormatWriter().encode(new String(content.getBytes(), "iso-8859-1"),
                    BarcodeFormat.QR_CODE, 300, 300);
            String format = Constants.SYS_HOME_QRCODE_FORMAT;
            File file = new File(path + "\" + filename + "." + format);
            BufferedImage image = toBufferedImage(byteMatrix);
            if (!ImageIO.write(image, format, file)) {
                throw new IOException("Could not write an image of format " + format + " to " + file);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    
    private static BufferedImage toBufferedImage(BitMatrix matrix) {
        int width = matrix.getWidth();
        int height = matrix.getHeight();
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        for (int x = 0; x < width; x++) {
            for (int y = 0; y < height; y++) {
                image.setRGB(x, y, matrix.get(x, y) ? BLACK : WHITE);
            }
        }
        return image;
    }
}


//测试将http://127.0.0.1:8888链接生成二维码
public static void main(String[] args) {
    //设置图片内容
    String content = "http://127.0.0.1:8888";
    //设置图片存放位置
    String path = "D:\QRCode";
    //设置生成的图片的名字
    String filename = "test";
    QRCodeUtils.buildQuickMark(content, path, filename);
    System.out.println("创建成功");
}
3、生成的二维码图片

4、生成二维码的业务层代码实现类
@Service
public class SysQRCodeServiceImpl implements SysQRCodeService {

    @Autowired
    SysConfigService sysConfigService;

    public String getQRCode() {
        return saveQRCode();
    }

    //将生成的二维码保存到服务器,并返回文件名
    public String saveQRCode(){
        //首先拿到系统配置中的要转换的二维码的值
        String configValue = sysConfigService.selectConfigByKey(Constants.SYS_HOME_QRCODE).getConfigValue();
        String qrCodeName = "QRCode";
        //生成二维码图片
        QRCodeUtils.buildQuickMark(configValue,uploadUrl(), qrCodeName);
        String resultQRCodeName = qrCodeName+"."+Constants.SYS_HOME_QRCODE_FORMAT;
        return resultQRCodeName;
    }


    //设置生成的二维码存放路径
    public String uploadUrl() {
        // 根据系统当前日期
        Date now = new Date();
        String date = new SimpleDateFormat("yyyyMM").format(now);
        // 设置图片存放地址
        String uploadFilePath = SenseConfig.getUploadFilePath();
        // 判断是否存在地址,如果不存在,按照规则创建文件夹
        File addr = new File(uploadFilePath);
        if (!addr.exists()) {
            addr.mkdirs();
        }
        return uploadFilePath;
    }
5、生成二维码的业务层代码接口
public interface SysQRCodeService {
    String getQRCode();
}
6、生成二维码的接口,共前端调用,获取生成二维码的图片名
@RestController
@RequestMapping("/sys/QRCode")
public class SysQRCodeController {

    @Autowired
    SysQRCodeService sysQRCodeService;

    @RequestMapping()
    public AjaxResult getQRCode(){
        return AjaxResult.success(sysQRCodeService.getQRCode());
    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/846156.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号