用Java写春联:一键自动发送微信祝福给喜欢的人【撩】

目录

1.前言

2、准备好祝福的春联图片和老虎图片放在调用路径下文件夹下

3、打开IDEA软件,编写一个WeChatRobot机器人

4、编写一个开始发送消息的任务

5、最后编写一个启动类,完成所有的祝福代码

6.我们看下图片和视频效果:


1.前言

春联,又称“春贴”、“门对”、“对联”,是过年时所贴的红色喜庆元素“年红”中一个种类。它以对仗工整、简洁精巧的文字描绘美好形象,抒发美好愿望,是中国特有的文学形式,是华人们过年的重要习俗。当人们在自己的家门口贴年红(春联、福字、窗花等)的时候,意味着过春节正式拉开序幕。 

每逢春节,无论城市还是农村,家家户户都要挑漂亮的红春联贴于门上,辞旧迎新,增加喜庆的节日气氛。春联的另一来源是春贴,古人在立春日多贴“宜春”二字,后渐渐发展为春联,表达了中国劳动人民一种辟邪除灾、迎祥纳福的美好愿望。以下是我用代码编写的春联图片,上联:虎跃龙腾山河美,下联:莺歌燕舞月江春,横批:虎跃龙腾。如何将它一键自动发送给小姐姐呢?我们拭目以待!

当今时代的程序员,大多充当键盘侠角色,在微信、论坛、游戏上大行其道发送消息,很少会使用笔墨纸砚来陶冶情操,挥洒出心中的祝福春联,自然就少了些许浪漫诗情画意!在这,我们可以使用java语言的自动发送功能,一键轻松不失高雅の送出我们心中的祝福!下面从以下几步完成对联和祝福语:

2、准备好祝福的春联图片和老虎图片放在调用路径下文件夹下

3、打开IDEA软件,编写一个WeChatRobot机器人

/**
 * @ClassName RobotUtil
 * @Description 编写机器人
 * @Author sxl
 * @Date 2022-01-27 10:23
 **/
public class WeChatRobot {

    private Robot bot = null;
    private Clipboard clip = null;
    private static Toolkit kit;

    public WeChatRobot() {
        try {
            this.clip = Toolkit.getDefaultToolkit().getSystemClipboard();
            this.bot = new Robot();
        } catch (AWTException e) {
            e.printStackTrace();
        }
    }

    /**
     * 打开微信,我这边设置了CTRL+ALT+W 快捷键打开微信
     */
    public void openWeChat() {
        bot.keyPress(KeyEvent.VK_CONTROL);
        bot.keyPress(KeyEvent.VK_ALT);
        bot.keyPress(KeyEvent.VK_W);

        bot.keyRelease(KeyEvent.VK_CONTROL);
        bot.keyRelease(KeyEvent.VK_ALT);

        bot.delay(1000);
    }

    /**
     * 查找微信中好友名称
     *
     * @param name 好友/群名称
     */
    public void chooseFriends(String name) {
        Transferable text = new StringSelection(name);
        clip.setContents(text, null);
        bot.delay(1000);
        bot.keyPress(KeyEvent.VK_CONTROL);
        bot.keyPress(KeyEvent.VK_F);
        bot.keyRelease(KeyEvent.VK_CONTROL);

        bot.delay(1000);

        bot.keyPress(KeyEvent.VK_CONTROL);
        bot.keyPress(KeyEvent.VK_V);
        bot.keyRelease(KeyEvent.VK_CONTROL);

        bot.delay(2000);

        bot.keyPress(KeyEvent.VK_ENTER);

    }

    /**
     * 发送消息
     *
     * @param message 消息
     */
    public void sendMessage(String message) {
        Transferable text = new StringSelection(message);
        clip.setContents(text, null);
        bot.delay(1000);
        bot.keyPress(KeyEvent.VK_CONTROL);
        bot.keyPress(KeyEvent.VK_V);
        bot.keyRelease(KeyEvent.VK_CONTROL);
        bot.delay(1000);

        bot.keyPress(KeyEvent.VK_ENTER);

        //来只老虎文字
        Transferable text2 = new StringSelection("送你一只老虎,祝你财源广进,吉祥如意!");
        clip.setContents(text2, null);
        bot.delay(1000);
        bot.keyPress(KeyEvent.VK_CONTROL);
        bot.keyPress(KeyEvent.VK_V);
        bot.keyRelease(KeyEvent.VK_CONTROL);
        bot.delay(1000);

        bot.keyPress(KeyEvent.VK_ENTER);
        //发送老虎图片

        Image image2 = null;
        File file2 = new File("C:\\Users\\Administrator\\Desktop\\img\\tiger.jpg");
        BufferedImage bi2;
        //通过io流操作把file对象转换成Image
        try {
            InputStream is=new FileInputStream(file2);
            bi2 = ImageIO.read(is);
            image2=(Image)bi2;
        } catch (IOException e) {
            e.printStackTrace();
        }

        // 将字符串放到剪切板内,相当于做了一次复制操作
        Transferable tImg2 = new ImageSelection(image2);
        clip.setContents(tImg2, null);
        bot.delay(1000);
        bot.keyPress(KeyEvent.VK_CONTROL);
        bot.keyPress(KeyEvent.VK_V);
        bot.keyRelease(KeyEvent.VK_CONTROL);
        bot.delay(1000);

        bot.keyPress(KeyEvent.VK_ENTER);

        //来副对联
        Transferable text3 = new StringSelection("送你一副对联,祝你龙腾虎跃,如虎添翼!");
        clip.setContents(text3, null);
        bot.delay(1000);
        bot.keyPress(KeyEvent.VK_CONTROL);
        bot.keyPress(KeyEvent.VK_V);
        bot.keyRelease(KeyEvent.VK_CONTROL);
        bot.delay(1000);

        bot.keyPress(KeyEvent.VK_ENTER);

        //发送春联祝福图片

        Image image = null;
        File file = new File("C:\\Users\\Administrator\\Desktop\\春联\\春联.png");
        BufferedImage bi;
        //通过io流操作把file对象转换成Image
        try {
            InputStream is=new FileInputStream(file);
            bi = ImageIO.read(is);
            image=(Image)bi;
        } catch (IOException e) {
            e.printStackTrace();
        }

        // 将字符串放到剪切板内,相当于做了一次复制操作
        Transferable tImg = new ImageSelection(image);
        clip.setContents(tImg, null);
        bot.delay(1000);
        bot.keyPress(KeyEvent.VK_CONTROL);
        bot.keyPress(KeyEvent.VK_V);
        bot.keyRelease(KeyEvent.VK_CONTROL);
        bot.delay(1000);

        bot.keyPress(KeyEvent.VK_ENTER);


    }

    /**
     * 发送消息
     *
     * @param message 消息
     */
    public void sendMessage1(String img) {

        if (bot == null) {
            return;
        }
        Image image = null;
        File file = new File(img);
        BufferedImage bi;
        //通过io流操作把file对象转换成Image
        try {
            InputStream is=new FileInputStream(file);
            bi = ImageIO.read(is);
            image=(Image)bi;
        } catch (IOException e) {
            e.printStackTrace();
        }

        // 将字符串放到剪切板内,相当于做了一次复制操作
        Transferable tImg = new ImageSelection(image);
        clip.setContents(tImg, null);
        bot.delay(1000);
        bot.keyPress(KeyEvent.VK_CONTROL);
        bot.keyPress(KeyEvent.VK_V);
        bot.keyRelease(KeyEvent.VK_CONTROL);
        bot.delay(1000);

        bot.keyPress(KeyEvent.VK_ENTER);

        bot.delay(1000);
        bot.keyPress(KeyEvent.VK_CONTROL);
        bot.keyPress(KeyEvent.VK_ALT);
        bot.keyPress(KeyEvent.VK_W);

        bot.keyRelease(KeyEvent.VK_CONTROL);
        bot.keyRelease(KeyEvent.VK_ALT);

    }

    /**
     * 自定义Transferable实现类实现图片复制到剪切板
     *
     * @date 2020/05/14
     */
    public class ImageSelection implements Transferable {
        private Image image;

        public ImageSelection(Image image) {
            this.image = image;
        }

        // Returns supported flavors
        public DataFlavor[] getTransferDataFlavors() {
            return new DataFlavor[]{DataFlavor.imageFlavor};
        }

        // Returns true if flavor is supported
        public boolean isDataFlavorSupported(DataFlavor flavor) {
            return DataFlavor.imageFlavor.equals(flavor);
        }

        // Returns image
        public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException {
            if (!DataFlavor.imageFlavor.equals(flavor)) {
                throw new UnsupportedFlavorException(flavor);
            }
            return image;
        }

    }

}

4、编写一个开始发送消息的任务

/**
 * @ClassName StartSendMsgTask
 * @Description 开始发送消息的任务
 * @Author sxl
 * @Date 2022-01-27 10:46
 **/
public class StartSendMsgTask {

    private final WeChatRobot robot = new WeChatRobot();
    ScheduledExecutorService exe = Executors.newSingleThreadScheduledExecutor();

    /**
     * 立刻发送消息
     *
     * @param friendName 发送的朋友/群名称
     * @param message    发送的内容
     */
    public void sendMsgNow(String friendName, String message) {
        printLog(friendName, message);
        robot.openWeChat();
        robot.chooseFriends(friendName);
        robot.sendMessage(message);

    }



    /**
     * 定时发送任务
     *
     * @param friendName 发送的朋友/群名称
     * @param timeStr    定时时间
     * @param message    发送的内容
     */
    public void sendMsgSchedule(String friendName, String timeStr, String message) {
        exe.schedule(() -> sendMsgNow(friendName, message), getDate(timeStr), TimeUnit.SECONDS);
    }

    /**
     * 发送内容日志
     *
     * @param friendName 发送给谁
     * @param message    发送的消息
     */
    private void printLog(String friendName, String message) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println("-----------------发送祝福消息给小姐姐-----------------");
        System.out.println("当前时间: " + sdf.format(new Date()));
        System.out.println("发送对象: " + friendName);
        System.out.println("发送内容: " + message);
    }

    /**
     * 获取定时任务的时间
     *
     * @param timeStr 时间
     * @return 任务延时发送时间(秒)
     */
    private long getDate(String timeStr) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String currentDate = sdf.format(new Date());
        String targetTime = currentDate + " " + timeStr;

        sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //目标时间 时间戳
        long targetTimer = 0;
        try {
            targetTimer = sdf.parse(targetTime).getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        //当前时间 时间戳
        long currentTimer = System.currentTimeMillis();
        //判断是否已过目标时间
        if (targetTimer < currentTimer) {
            //目标时间加一天
            targetTimer += 1000 * 60 * 60 * 24;
        }
        //返回目标日期
        Date date = new Date(targetTimer);

        return (date.getTime() - System.currentTimeMillis()) / 1000;
    }
}

5、最后编写一个启动类,完成所有的祝福代码

/**
 * @ClassName Main
 * @Description 启动类
 * @Author sxl
 * @Date 2022-01-27 10:48
 **/
public class Main {

    public  void sendMessage() {
        System.out.println("Start Send Message Task!");
        // 测试数据,真实数据可以使用爬虫获取
        String msg = "今天是2022年1月27日,星期四\n" +
                "\n" +
                "首先今天好想你喔(づ ̄3 ̄)づ╭❤~,然后我就要来播送天气预报了!!\n" +
                "\n" +
                "今天最:高温 15.0℃,最低温 8.0℃\n" +
                "\n" +
                "阴转小雨,风力<4级,空气质量是良\n" +
                "\n" +
                "今天将在 17:39 太阳会缓缓落下,我会在家做好饭等你哟!\n" +
                "\n" +
                "摩羯座今日运势\n" +
                "\n" +
                "健康指数:92%\n" +
                "商谈指数:88%\n" +
                "幸运颜色:粉色\n" +
                "幸运数字:8\n" +
                "\n" +
                "综合运势: 整体运势中规中矩,建议保持低调的姿态。盲目追求风头只会掉入欲望的陷阱中,你需要找到自己想做的事情,或是目标方向更重要。生活方面可以打扮简单利落一些,能提高舒适感的同时,也能给别人带来好印象。\n" +
                "\n" +
                "最后阴晴之间,谨防紫外线侵扰\n" +
                "\n" +
                "爱你٩(๑>◡<๑)۶傻宝宝!!!";

        StartSendMsgTask startSendMsgTask = new StartSendMsgTask();
        // 立刻发送
        startSendMsgTask.sendMsgNow("老婆", msg);
        // 定时发送
        startSendMsgTask.sendMsgSchedule("老婆", "13:14:00", msg);
    }
}

启动方法:编写好后我们在idea编辑器里运行启用类Main主方法即可

6.我们看下图片和视频效果:

自动发送虎年祝福给小姐姐视频链接icon-default.png?t=M0H8https://v.qq.com/x/page/c3320gmrwhp.html

  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

IT施sir

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

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

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

打赏作者

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

抵扣说明:

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

余额充值