java链接生成二维码_java实现滑动验证码源代码

java链接生成二维码_java实现滑动验证码源代码packagecn.itcast.action;importjava.awt.image.BufferedImage;importjava.io.File;importjava.io.IOException;importjava.io.OutputStream;importjava.text.SimpleDateFormat;importjava.util.Date;importjava.util.HashMap;importjava.util.Map;impo…

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺

package cn.itcast.action;
 
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
 
import javax.imageio.ImageIO;
import javax.swing.filechooser.FileSystemView;
 
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.common.BitMatrix;
 
public class QrCodeUtil {

 
    public static void main(String[] args) {

        String url = “http://t.cn/RdZMeZJ111111”;
        String path = FileSystemView.getFileSystemView().getHomeDirectory() + File.separator + “testQrcode”;
        System.out.println(path);
        String fileName = new SimpleDateFormat(“yyyyMMddHHmmss”).format(new Date()) + “.jpg”;
        createQrCode(url, path, fileName);
    }
 
    public static String createQrCode(String url, String path, String fileName) {

        try {

            Map<EncodeHintType, String> hints = new HashMap<>();
            hints.put(EncodeHintType.CHARACTER_SET, “UTF-8”);
            BitMatrix bitMatrix = new MultiFormatWriter().encode(url, BarcodeFormat.QR_CODE, 400, 400, hints);
            File file = new File(path, fileName);
            if (file.exists() || ((file.getParentFile().exists() || file.getParentFile().mkdirs()) && file.createNewFile())) {

                writeToFile(bitMatrix, “jpg”, file);
                System.out.println(“测试完成:” + file);
            }
 
        } catch (Exception e) {

            e.printStackTrace();
        }
        return null;
    }
 
    static void writeToFile(BitMatrix matrix, String format, File file) throws IOException {

        BufferedImage image = toBufferedImage(matrix);
        if (!ImageIO.write(image, format, file)) {

            throw new IOException(“Could not write an image of format ” + format + ” to ” + file);
        }
    }
 
    static void writeToStream(BitMatrix matrix, String format, OutputStream stream) throws IOException {

        BufferedImage image = toBufferedImage(matrix);
        if (!ImageIO.write(image, format, stream)) {

            throw new IOException(“Could not write an image of format ” + format);
        }
    }
 
    private static final int BLACK = 0xFF000000;
    private static final int WHITE = 0xFFFFFFFF;
 
    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;
    }
 
}

本文是短链接生成二维码图片保存到本地的一个实例demo

核心包是

core-3.1.0.jar  自己下载吧

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/196914.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • aws 将俄勒冈的ami 镜像复制到东京地区

    aws 将俄勒冈的ami 镜像复制到东京地区

    2022年2月18日
    56
  • 哈希函数和哈希表

    哈希函数和哈希表哈希函数和哈希表 1 什么是哈希函数它是一种映射关系 它可以把任意长度的输入映射到任意一个固定长度的整数值 也称为散列函数 其值是十六进制的数 说白了 哈希函数就是用来将 key value 结构中关键字值转换为数组的下标的函数 一般都是通过取模 而且这样子在数据量很大的情况下一般是均匀分布的 然后将该结构存放到数组中去 然后这个数组就叫做哈希表 这个固定长度不是说所有长度的输入获取到的整数

    2025年6月26日
    3
  • goland2021.8激活码[免费获取]

    (goland2021.8激活码)这是一篇idea技术相关文章,由全栈君为大家提供,主要知识点是关于2021JetBrains全家桶永久激活码的内容IntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,下面是详细链接哦~https://javaforall.net/100143.htmlBI7JCUH1TG-eyJsa…

    2022年3月22日
    49
  • navicat 1146错误「建议收藏」

    navicat 1146错误「建议收藏」打开新安装的navicat后,有个test_3306的mysql连接,里面有写默认的mysql、information_schema、sys、performance_schema数据库,我以为这是没用的就删除了,之后建立自己的mysql连接后,打开连接报错1146-Table’historyhistoryperformance_schema.session_status’doesn’texist。查阅资料后了解mysql、information_schema、sys、performance_s

    2022年6月7日
    184
  • 扩展欧几里得

    扩展欧几里得

    2022年1月28日
    413
  • vscode 前端常用必备插件汇总。「建议收藏」

    vscode 前端常用必备插件汇总。「建议收藏」持续更新,欢迎推荐好的插件。1.vscode基础智能能提示类插件2.vscode美化优化类插件3.vscode各大框架辅助插件4.markdown工具类插件

    2022年7月25日
    22

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号