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 ...
随机推荐
- laravel 批量更新
/** * 转发动态和资讯数量统计 */ public function forwardCounts(FeedModel $feedModel) { //统计动态转发的id $feeds=$feedM ...
- 使用react中遇到的问题
引入antdesign中Carousel走马灯时遇到问题? Uncaught Error: Element ref was specified as a string (slick) but no o ...
- T-SQL语句3
一.删除表 1.drop table语句 drop table database_name,schema_name,table_name 2.删除数据表 drop table dbo.t_delete ...
- hdu 2923 map+Floyd 拉破车
有向图 具体方向看箭头 从起点到指定城市拉破车,一个城市可能有多个破车,一次只能拉一辆破车 也就是到了指定地点后要回到起点 假如有100辆破车 但是只有一个城市有 就得在起点与这个城市间往返100次所 ...
- Git(二)使用git管理文件版本(TortoiseGit )
一.创建版本库 什么是版本库呢?版本库又名仓库,英文名repository,你可以简单理解成一个目录,这个目录里面的所有文件都可以被Git管理起来,每个文件的修改.删除,Git都能跟踪,以便任何时刻都 ...
- shell学习(二)
1.EOF Shell中通常将EOF与 <<和cat 结合使用,表示后续的输入作为子命令或子Shell的输入,直到遇到EOF为止,再返回到主调Shell. 可以把EOF替换成其他东西,意思 ...
- HTML、CSS、JS 样式 (未整理)
随手记,有错误的地方希望留言 ^.-.^ PHP 实现关闭浏览器窗口echo "<script>window.close();</script>"; jqu ...
- 循序渐进学.Net Core Web Api开发系列【1】:开发环境
系列目录 循序渐进学.Net Core Web Api开发系列目录 本系列涉及到的源码下载地址:https://github.com/seabluescn/Blog_WebApi 一.本篇概述 本篇不 ...
- 用yum安装JDK
用yum安装JDK 1.查看yum库中都有哪些jdk版本(暂时只发现了openjdk) [root@localhost ~]# yum search java|grep jdkldapjdk-java ...
- Django 性能测试
唐纳德·克努特(Donald Knuth)曾经说过:“不成熟的优化方案是万恶之源.”然而,任何一个承受高负载的成熟项目都不可避免地需要进行优化.在本文中,我想谈谈优化Web项目代码的五种常用方法.虽然 ...