用java写图片
登录注册的时候都会有图片验证,这是为了防止暴力破解和恶意注册。写一个思路来实现验证图片的实现,只是一个思路,随机生成文字并没有写。
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException; import javax.imageio.ImageIO; public class Image {
public static void main(String[] args) throws FileNotFoundException, IOException{
//得到图片缓冲区
BufferedImage bi=new BufferedImage(150,70, BufferedImage.TYPE_INT_RGB);
//得到画笔
Graphics2D g2=(Graphics2D) bi.getGraphics();
//填充背景
g2.setColor(Color.WHITE);
g2.fillRect(0, 0, 150, 70);
//设置边框
g2.setColor(Color.RED);
g2.drawRect(0, 0, 149, 69);
//向图片上写字符串
g2.setFont(new Font("宋体", Font.BOLD, 10));
g2.setColor(Color.BLACK);
g2.drawString("grup", 19, 20);
ImageIO.write(bi, "JPEG", new FileOutputStream("f:/g.jpg"));
}
}
可以做的文章有,第一是随机生成字符,第二随机生成颜色,第三随机生成干扰线,第四随机生成字体,第五随机生成字号等等
网页登录的时候需要图片验证,今天刚好用到,把之前没有做的东西补充上来。
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random; import javax.imageio.ImageIO; public class VerifyCode {
//设置宽
private int w=;
//设置高
private int h=;
private Random r=new Random();
//字体
private String[] fontNames={"宋体","华文楷体","黑体","微软雅黑","楷体_GB2312"};
//字符
private String codes="23456789abcdefghigklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
//背景颜色
private Color bgColor=new Color(, , );
//图片文字
private String text;
//生成随机数
private Color randomColor(){
int red=r.nextInt();
int green=r.nextInt();
int blue=r.nextInt();
return new Color(red,green,blue);
}
//生成随机字体
private Font randomFont(){
int index=r.nextInt(fontNames.length);
String fontName=fontNames[index];
int style=r.nextInt();
int size=r.nextInt()+;
return new Font(fontName, style, size);
}
//生成随机字符
private char randomChar(){
int index=r.nextInt(codes.length());
return codes.charAt(index);
}
//生成空白图片
private BufferedImage createImage(){
BufferedImage image=new BufferedImage(w,h,BufferedImage.TYPE_INT_RGB);
Graphics2D g2=(Graphics2D)image.getGraphics();
g2.setColor(this.bgColor);
g2.fillRect(, , w, h);
return image;
}
//生成随机乱线
private void drawLine(BufferedImage image){ int num=;
Graphics2D g2=(Graphics2D)image.getGraphics();
for(int i=;i<;i++){
int x1=r.nextInt(w);
int y1=r.nextInt(h);
int x2=r.nextInt(w);
int y2=r.nextInt(h);
g2.setStroke(new BasicStroke(1.5F));
g2.setColor(Color.BLUE);
g2.drawLine(x1,y1,x2,y2);
} }
//将图片输出到某个输出流中
public static void output(BufferedImage image,OutputStream out) throws IOException{
ImageIO.write(image, "JPEG",out);
}
//得到图片上的文字
public String getText(){
return text;
}
//得到图片
public BufferedImage getImage(){
BufferedImage image=createImage();
Graphics2D g2=(Graphics2D)image.getGraphics();
StringBuilder sb=new StringBuilder();
for(int i=;i<;i++){
String s=randomChar()+"";
sb.append(s);
float x=i*1.0F*w/;
g2.setFont(randomFont());
g2.setColor(randomColor());
g2.drawString(s, x, h-);
}
this.text=sb.toString();
drawLine(image);
return image;
}
}
用java写图片的更多相关文章
- Java——Image 图片合并
1.合并图片 package com.tb.image; import java.awt.Image; import java.awt.image.BufferedImage; import java ...
- JAVA之旅(三十五)——完结篇,终于把JAVA写完了,真感概呐!
JAVA之旅(三十五)--完结篇,终于把JAVA写完了,真感概呐! 这篇博文只是用来水经验的,写这个系列是因为我自己的java本身也不是特别好,所以重温了一下,但是手比较痒于是就写出了这三十多篇博客了 ...
- java多图片上传--前端实现预览--图片压缩 、图片缩放,区域裁剪,水印,旋转,保持比例。
java多图片上传--前端实现预览 前端代码: https://pan.baidu.com/s/1cqKbmjBSXOhFX4HR1XGkyQ 解压后: java后台: <!--文件上传--&g ...
- java服务器图片压缩的几种方式及效率比较
以下是测试了三种图片压缩方式,通过测试发现使用jdk的ImageIO压缩时间更短,使用Google的thumbnailator更简单,但是thumbnailator在GitHub上的源码已经停止维护了 ...
- 用 Java 写个塔防游戏「GitHub 热点速览 v.21.37」
作者:HelloGitHub-小鱼干 本周 GitHub Trending 的主题词是:多语言.本周特推的 C 语言教程是大家都知道的阮一峰编写的,想必和他之前的技术文章类似,能起到科普作用.再来时 ...
- Java中图片压缩处理
原文http://cuisuqiang.iteye.com/blog/2045855 整理文档,搜刮出一个Java做图片压缩的代码,稍微整理精简一下做下分享. 首先,要压缩的图片格式不能说动态图片,你 ...
- kafka集群搭建和使用Java写kafka生产者消费者
1 kafka集群搭建 1.zookeeper集群 搭建在110, 111,112 2.kafka使用3个节点110, 111,112 修改配置文件config/server.properties ...
- java获取图片原始尺寸
java获取图片原始尺寸 URL url = null; InputStream is = null; BufferedImage img = null; try { url = new URL(pi ...
- 利用bootstrap写图片轮播
利用bootstrap写图片轮播 缺点是轮播没有固定样式图片样式会改变外框的大小,所以要再设置 以及左右按钮的style也要从新设置 <div class="carousel slid ...
随机推荐
- R2CNN项目部分代码学习
首先放出大佬的项目地址:https://github.com/yangxue0827/R2CNN_FPN_Tensorflow 那么从输入的数据开始吧,输入的数据要求为tfrecord格式的数据集,好 ...
- 一脸懵逼加从入门到绝望学习hadoop之 org.apache.hadoop.ipc.RemoteException(org.apache.hadoop.security.AccessControlException): Permission denied: user=Administrator, access=WRITE, inode="/":root:supergroup:drwxr-xr报错
1:初学hadoop遇到各种错误,这里贴一下,方便以后脑补吧,报错如下: 主要是在window环境下面搞hadoop,而hadoop部署在linux操作系统上面:出现这个错误是权限的问题,操作hado ...
- [转] HTML5 Blob与ArrayBuffer、TypeArray和字符串String之间转换
1.将String字符串转换成Blob对象 //将字符串 转换成 Blob 对象 var blob = new Blob(["Hello World!"], { type: 'te ...
- LeetCode-450 二叉搜索树删除一个节点
二叉搜索树 建树 删除节点,三种情况,递归处理.左右子树都存在,两种方法,一种找到左子树最大节点,赋值后递归删除.找右子树最小同理 class Solution { public: TreeNode* ...
- 用webstorm搭建vue项目
本文只针对新手. 首先要明白几个名词(概念). Node.js: Node.js 是一个基于 Chrome V8 引擎的 JavaScript 运行环境. Node.js 使用了一个事件驱动.非阻塞式 ...
- (5).NET CORE微服务 Micro-Service ---- 熔断降级(Polly)
一. 什么是熔断降级 熔断就是“保险丝”.当出现某些状况时,切断服务,从而防止应用程序不断地尝试执行可能会失败的操作给系统造成“雪崩”,或者大量的超时等待导致系统卡死. 降级的目的是当某个服务提供者发 ...
- jenkins(3): jenkins执行shell命令
参考: https://www.cnblogs.com/reblue520/p/7146693.html 1. 执行 本地 shell命令或者脚本 是在一个构建中的 bulid 选项卡. 执行本地中 ...
- Python自用笔记
函数:raw_input()和input() 注意:在python3.x中,已经删除raw_input(),取而代之的是input(),当然这仅仅是重命名,用法还是一样.因此在这里介绍的是python ...
- themeleaf跳转锚链接
<a class="lianjie3" th:href="@{/}+'#requires'"></a>
- webpack的build的时候时间长处理方案
观察第一次build的时间比较长,之后的编译时间较短,可以通过webpack -watch 监测性能 1, 将webpack升级到4.0,build 的速度提升很多 2,用webpack -watch ...