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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • C语言中getopt()函数的用法[通俗易懂]

    C语言中getopt()函数的用法[通俗易懂]1.getopt()函数getopt函数用来解析命令行选项,声明所在头文件为:#include<unistd.h>函数原型如下:intgetopt(intargc,char*constargv[],constchar*optstring);第一个参数argc是

    2022年5月3日
    43
  • 手把手从零开始学习树莓派教程

    手把手从零开始学习树莓派教程项目开始时间:2021/4/1119:43树莓派4b文章目录1.树莓派系统下载2.操作系统移植3.树莓派联网1.树莓派系统下载这里使用raspbian-buster-full系统,官网速度太慢,使用清华镜像下载也很慢,官网下载种子,然后用迅雷下载比较快。解压后8个g,我用的32gsd卡2.操作系统移植用win32diskimager工具把操作系统写入SD卡,插入树莓派中,启动毫无响应,将sd卡从树莓派上拔下,用读卡器再次插入电脑usb口,显示类似这样:在网上找了很多办法,都不能解决,

    2022年10月14日
    3
  • python 怎么保留小数「建议收藏」

    python 怎么保留小数「建议收藏」使用字符串格式化大部分语言都可以使用字符串格式化的方法来实现保留两位小数的效果,python也不例外:a=3.1415926print(“%.2f”%a)#%代表格式化输出,.2代表小数点后保留两位,f代表数据类型是浮点型使用round内置函数python内置了一个名为round的函数,这个函数可以用来对数据进行格式化。代码如下:a=3.1415926a1=round(a,2)#将a通过round函数处理后赋值给a1,传入的2代表保留两位小数print(a1)使

    2022年8月12日
    6
  • asp.net中的Gridview控件添加序号列

    asp.net中的Gridview控件添加序号列asp.net中的Gridview控件添加序号列

    2022年4月24日
    72
  • spring SchedulerFactoryBean 没有创建 Scheduler的实现类bea

    spring SchedulerFactoryBean 没有创建 Scheduler的实现类bea2019独角兽企业重金招聘Python工程师标准>>>…

    2022年5月23日
    27
  • pycharm2021.5有效激活码【在线注册码/序列号/破解码】

    pycharm2021.5有效激活码【在线注册码/序列号/破解码】,https://javaforall.net/100143.html。详细ieda激活码不妨到全栈程序员必看教程网一起来了解一下吧!

    2022年3月20日
    47

发表回复

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

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