java把指定文字输出为图片流,支持文字换行
public class IamgeUtils { private static final int WIDTH = 350;
private static final int HEIGHT = 100; private static Random random = new Random(); public static void main(String[] args) throws IOException {
String[] verifyCodes = new String[] { "你好", "第二行,******", "第三行" };
String result = outputImage(verifyCodes);
write2Pic(result);
} public static int r(int min, int max) {
int num = 0;
num = random.nextInt(max - min) + min;
return num;
} public static void write2Pic(String sourceByte) { FileOutputStream output;
try {
output = new FileOutputStream(new File("F:/123.jpg"));
output.write(Base64.decodeBase64(sourceByte));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} /**
* 输出指定文本图片流
*
* @param w
* @param h
* @param os
* @param code
* @throws IOException
*/
public static String outputImage(String[] verifyCodes) throws IOException {
BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
Random rand = new Random();
Graphics2D g2 = image.createGraphics();
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
Color[] colors = new Color[5];
Color[] colorSpaces = new Color[] { Color.WHITE, Color.CYAN, Color.GRAY, Color.LIGHT_GRAY, Color.MAGENTA,
Color.ORANGE, Color.PINK, Color.YELLOW };
float[] fractions = new float[colors.length];
for (int i = 0; i < colors.length; i++) {
colors[i] = colorSpaces[rand.nextInt(colorSpaces.length)];
fractions[i] = rand.nextFloat();
}
Arrays.sort(fractions); g2.setColor(Color.GRAY);// 设置边框色
g2.fillRect(0, 0, WIDTH, HEIGHT); Color c = getRandColor(200, 250);
g2.setColor(c);// 设置背景色
g2.fillRect(0, 2, WIDTH, HEIGHT - 4); // 绘制干扰线
Random random = new Random();
g2.setColor(getRandColor(160, 200));// 设置线条的颜色
for (int i = 0; i < 20; i++) {
int x = random.nextInt(WIDTH - 1);
int y = random.nextInt(HEIGHT - 1);
int xl = random.nextInt(6) + 1;
int yl = random.nextInt(12) + 1;
g2.drawLine(x, y, x + xl + 40, y + yl + 20);
} // 添加噪点
float yawpRate = 0.05f;// 噪声率
int area = (int) (yawpRate * WIDTH * HEIGHT);
for (int i = 0; i < area; i++) {
int x = random.nextInt(WIDTH);
int y = random.nextInt(HEIGHT);
int rgb = getRandomIntColor();
image.setRGB(x, y, rgb);
} shear(g2, WIDTH, HEIGHT, c);// 使图片扭曲 g2.setColor(getRandColor(100, 160));
int fontSize = HEIGHT;
Font font = new Font("宋体", Font.ITALIC, 20);
g2.setFont(font); FontMetrics fm = sun.font.FontDesignMetrics.getMetrics(font); int y = fm.getHeight();
int h = fm.getHeight();
for (int i = 0; i < verifyCodes.length; i++) {
g2.drawString(verifyCodes[i], (int) (h * (0.8)), (int) (i * y * (0.8)) + 24);
}
g2.dispose();
ByteArrayOutputStream out = new ByteArrayOutputStream();
ImageIO.write(image, "jpg", out);
String bytePic = Base64.encodeBase64String(out.toByteArray());// Base64.encodeBase64String(out.toByteArray());
System.out.println(bytePic);
return bytePic;
} private static Color getRandColor(int fc, int bc) {
if (fc > 255)
fc = 255;
if (bc > 255)
bc = 255;
int r = fc + random.nextInt(bc - fc);
int g = fc + random.nextInt(bc - fc);
int b = fc + random.nextInt(bc - fc);
return new Color(r, g, b);
} private static int getRandomIntColor() {
int[] rgb = getRandomRgb();
int color = 0;
for (int c : rgb) {
color = color << 8;
color = color | c;
}
return color;
} private static int[] getRandomRgb() {
int[] rgb = new int[3];
for (int i = 0; i < 3; i++) {
rgb[i] = random.nextInt(255);
}
return rgb;
} private static void shear(Graphics g, int w1, int h1, Color color) {
shearX(g, w1, h1, color);
shearY(g, w1, h1, color);
} private static void shearX(Graphics g, int w1, int h1, Color color) { int period = random.nextInt(2); boolean borderGap = true;
int frames = 1;
int phase = random.nextInt(2); for (int i = 0; i < h1; i++) {
double d = (double) (period >> 1)
* Math.sin((double) i / (double) period + (6.2831853071795862D * (double) phase) / (double) frames);
g.copyArea(0, i, w1, 1, (int) d, 0);
if (borderGap) {
g.setColor(color);
g.drawLine((int) d, i, 0, i);
g.drawLine((int) d + w1, i, w1, i);
}
} } private static void shearY(Graphics g, int w1, int h1, Color color) { int period = random.nextInt(40) + 50; // 50; boolean borderGap = true;
int frames = 20;
int phase = 7;
for (int i = 0; i < w1; i++) {
double d = (double) (period >> 1)
* Math.sin((double) i / (double) period + (6.2831853071795862D * (double) phase) / (double) frames);
g.copyArea(i, 0, 1, h1, 0, (int) d);
if (borderGap) {
g.setColor(color);
g.drawLine(i, (int) d, i, 0);
g.drawLine(i, (int) d + h1, i, h1);
} } }
}
java把指定文字输出为图片流,支持文字换行的更多相关文章
- 通过java的i/o机制进行图片流的存储以及对网络图片的存储
存储内地图片思路:首先把原有的图片以流的方式读取出来,再以流的方式存储到目标文件: package imgStream; import java.io.*; public class ImgStrea ...
- java按照指定格式输出系统时间
public class TimeFour { public static void main(String[] args) throws ParseException{ TimeFour four ...
- java按照指定格式输出系统时间使用SimpleDateFormat方法
public class TimeThree { public static void main(String[] args) { SimpleDateFormat d = new SimpleDat ...
- C# 图片流下载;图片流输出
图片流下载 string filePath = HttpContext.Current.Server.MapPath("/img/wxPic/"); if (!Directory. ...
- 从 Java 代码逆向工程生成 UML 类图和序列图
from:http://blog.itpub.net/14780914/viewspace-588975/ 本文面向于那些软件架构师,设计师和开发人员,他们想使用 IBM® Rational® Sof ...
- Java 控制台输入数字 输出乘法表(代码练习)
最近,回忆了一些刚学习Java时经常练习的一些小练习题.感觉还是蛮有趣的,在回顾时想起好多学习时的经历和坎坷,一道小小的练习题要研究半天,珍重过往,直面未来.下面贡献代码,Java 控制台输入数字 输 ...
- java虚拟机的基本结构如图
1 java虚拟机的基本结构如图: 1)类加载子系统负责从文件系统或者网络中加载Class信息,加载的类信息存放于一块称为方法区的内存空间.除了类的信息外,方法区中可能还会存放运行时常量池信息,包括字 ...
- 纯Java实现微信朋友圈分享图
纯Java实现微信朋友圈分享图 1.实现分享图的效果 2.开发环境 2.1 JDK * oracle's jdk 1.8以上 2.2 字体 * 若选择了微软雅黑字体又是代码部署到Linux,则需要安装 ...
- Java程序性能定位工具-火焰图
Java程序性能定位工具-火焰图 前言 Java火焰图是一种新的查看CPU利用率方式.今天就带大家一起使用来自Google大神的工具来生成火焰图.火焰图非常的直观,问题一目了然,希望有一天它能成为JA ...
随机推荐
- Mockito 简明教程
什么是 Mock 测试 Mock 测试就是在测试过程中,对于某些不容易构造(如 HttpServletRequest 必须在Servlet 容器中才能构造出来)或者不容易获取比较复杂的对象(如 JDB ...
- Nginx 日志分析命令
查看日志存放目录 # find / -name access.log /var/log/nginx/access.log cd /var/log/nginx IP相关统计 统计IP访问量(独立ip访问 ...
- Java日期时间类
日期时间类有三种: 一.java.util.Date:一般用于声明日期时间类型的变量. 二.java.sql.Date:一般用于数据库日期时间的映射. 三.java.util.Calendar:一般用 ...
- 多线程学习笔记二之JUC组件
目录 概述 JUC锁框架图 使用内置锁还是JUC显示锁? 概述 为了对共享资源提供更细粒度的同步控制,JDK5新增了java.util.concurrent(JUC)并发工具包,并发包新增了Loc ...
- leetcode 岛屿的个数 python
岛屿的个数 给定一个由 '1'(陆地)和 '0'(水)组成的的二维网格,计算岛屿的数量.一个岛被水包围,并且它是通过水平方向或垂直方向上相邻的陆地连接而成的.你可以假设网格的四个边均被水包 ...
- 膨胀卷积与IDCNN
Dilation 卷积,也被称为:空洞卷积.膨胀卷积. 一.一般的卷积操作: 首先,可以通过动态图,理解正常卷积的过程: 如上图,可以看到卷积操作. 对于CNN结构,通常包括如下部分: 输入层 (in ...
- 在多线程中使用spring的bean
由于spring在java开发中的广泛运用大大的方便了开发的同时,当运用一些技术比如多线程等 在由spring管理的配置文件中,可以通过封装spring提供工具,手动获得spring管理的bean,这 ...
- KVM磁盘镜像qcow2、raw、vmdk等格式区别(转)
raw(default) the raw format is a plain binary image of the disc image, and is very portable. On file ...
- request.getRequestDispatcher("").forward()中文乱码
即使jsp页面的编码已设为“UTF-8”,有中文的地方还是会出现乱码,但是用response.sendRedirect不会出现此问题. 解决方案一: 不使用PrintWriter out=respon ...
- 从零开始部署CAS服务器
从0开始部署CAS服务器的操作过程文档,我已经整理完毕,一共分为8步,这8步都是我自己操作实践过的. Setp1:Ubuntu server安装 在virtual box中安装ubuntu serve ...