JAVA txt文件生成JPG图片水印文章
2012-04-20 23:07
537 查看
小试牛刀,今天在公司完成了 txt文件生成jpg连续图片,分享一下 代码不是很完善 但是能实现(可以直接copy到你的机器运行)希望各位大牛指点
缺陷就是耗费时间。
package picture;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.font.GlyphVector;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import javax.imageio.ImageIO;
public class DrawTest4 {
public static void pressText(String pressText, String targetImg,
String fontName, int fontStyle, Color color, Color col,
int fontSize, int x, int y) throws Exception {
File file = new File(targetImg);
Image src = ImageIO.read(file);
BufferedImage image = new BufferedImage(src.getWidth(null), src
.getHeight(null), BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = (Graphics2D) image.createGraphics();
g2.drawImage(src, 0, 0, src.getWidth(null), src.getHeight(null), null);
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
GlyphVector gv = new Font(fontName, fontStyle, fontSize)
.createGlyphVector(g2.getFontRenderContext(), pressText);
Shape sha = gv.getOutline();
g2.setStroke(new BasicStroke(4f)); // 描边像素
g2.translate(x, (y + fontSize / 2)); // 放置汉字的位置
g2.setColor(col); // 描边颜色
g2.draw(sha);
g2.setColor(color);// 填充字体颜色
g2.fill(sha);
g2.dispose();
ImageIO.write(image, "jpg", file);
}
public static void copyFile(String oldPath, String newPath) {
try {
int bytesum = 0;
int byteread = 0;
File oldfile = new File(oldPath);
if (oldfile.exists()) { // 文件存在时
InputStream inStream = new FileInputStream(oldPath); // 读入原文件
FileOutputStream fs = new FileOutputStream(newPath);
byte[] buffer = new byte[1444];
while ((byteread = inStream.read(buffer)) != -1) {
bytesum += byteread; // 字节数 文件大小
fs.write(buffer, 0, byteread);
}
inStream.close();
fs.close();
}
} catch (Exception e) {
System.out.println("复制文件操作出错");
e.printStackTrace();
}
}
@SuppressWarnings("static-access")
public static void resize(String filePath, int height, int width, boolean bb) {
try {
double ratio = 0.0; // 缩放比例
File f = new File(filePath);
BufferedImage bi = ImageIO.read(f);
Image itemp = bi.getScaledInstance(width, height, bi.SCALE_SMOOTH);
// 计算比例
if ((bi.getHeight() > height) || (bi.getWidth() > width)) {
if (bi.getHeight() > bi.getWidth()) {
ratio = (new Integer(height)).doubleValue()
/ bi.getHeight();
} else {
ratio = (new Integer(width)).doubleValue() / bi.getWidth();
}
AffineTransformOp op = new AffineTransformOp(AffineTransform
.getScaleInstance(ratio, ratio), null);
itemp = op.filter(bi, null);
}
if (bb) {
BufferedImage image = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
Graphics2D g = image.createGraphics();
g.setColor(Color.white);
g.fillRect(0, 0, width, height);
if (width == itemp.getWidth(null))
g.drawImage(itemp, 0, (height - itemp.getHeight(null)) / 2,
itemp.getWidth(null), itemp.getHeight(null),
Color.white, null);
else
g.drawImage(itemp, (width - itemp.getWidth(null)) / 2, 0,
itemp.getWidth(null), itemp.getHeight(null),
Color.white, null);
g.dispose();
itemp = image;
}
ImageIO.write((BufferedImage) itemp, "jpg", f);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void addText() {
try {
FileInputStream inputstream = new FileInputStream("f:/test/t1.txt");// 将TXT文件内容转换成String类型
StringBuffer buffer = new StringBuffer();
String line; // 用来保存每行读取的内容
BufferedReader bufferreader = new BufferedReader(
new InputStreamReader(inputstream));
line = bufferreader.readLine(); // 读取第一行
while (line != null) { // 如果 line 为空说明读完了
buffer.append(line); // 将读到的内容添加到 buffer 中
line = bufferreader.readLine(); // 读取下一行
}
// 将读到 buffer 中的内容写出来
inputstream.close();
String filetext = buffer.toString();
System.out.println("该文本共有" + filetext.length() + "个字符");
String tmp = "";
int length = 18;
int height = 45;
String oldFile = "F:/11.jpg";
String newFile = "F:/test/11.jpg";
int i;
int x=0;
int t = 0;
int cent = 1;
while (filetext.length()-x>0) {
copyFile(oldFile, newFile);
File f = new File(newFile);
f.renameTo(new File("F:/test/" + cent + ".jpg"));
resize("F:/test/" + cent + ".jpg", 455, 640, true);// 我先将图设置成规定的大小!!
for (i = 0; i < filetext.length() / length
&& height * (i + 1) <= 420; i++) {
int k = 0;
for (int j = 0; j < length;) {
tmp = filetext.substring(x+length * i + j, x+length * i + j
+ 1);
pressText(tmp, "F:/test/" + cent + ".jpg", "微软雅黑",
Font.CENTER_BASELINE, Color.BLACK, Color.WHITE,
28, 25 + k, (i + 1) * height);
j++;
t = j + length * i;
k = k + 33;
}
}
x+=t;
System.out.print("此时已经输出字符数:");
System.out.println(x);
cent++;
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
addText();
}
}
缺陷就是耗费时间。
package picture;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.font.GlyphVector;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import javax.imageio.ImageIO;
public class DrawTest4 {
public static void pressText(String pressText, String targetImg,
String fontName, int fontStyle, Color color, Color col,
int fontSize, int x, int y) throws Exception {
File file = new File(targetImg);
Image src = ImageIO.read(file);
BufferedImage image = new BufferedImage(src.getWidth(null), src
.getHeight(null), BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = (Graphics2D) image.createGraphics();
g2.drawImage(src, 0, 0, src.getWidth(null), src.getHeight(null), null);
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
GlyphVector gv = new Font(fontName, fontStyle, fontSize)
.createGlyphVector(g2.getFontRenderContext(), pressText);
Shape sha = gv.getOutline();
g2.setStroke(new BasicStroke(4f)); // 描边像素
g2.translate(x, (y + fontSize / 2)); // 放置汉字的位置
g2.setColor(col); // 描边颜色
g2.draw(sha);
g2.setColor(color);// 填充字体颜色
g2.fill(sha);
g2.dispose();
ImageIO.write(image, "jpg", file);
}
public static void copyFile(String oldPath, String newPath) {
try {
int bytesum = 0;
int byteread = 0;
File oldfile = new File(oldPath);
if (oldfile.exists()) { // 文件存在时
InputStream inStream = new FileInputStream(oldPath); // 读入原文件
FileOutputStream fs = new FileOutputStream(newPath);
byte[] buffer = new byte[1444];
while ((byteread = inStream.read(buffer)) != -1) {
bytesum += byteread; // 字节数 文件大小
fs.write(buffer, 0, byteread);
}
inStream.close();
fs.close();
}
} catch (Exception e) {
System.out.println("复制文件操作出错");
e.printStackTrace();
}
}
@SuppressWarnings("static-access")
public static void resize(String filePath, int height, int width, boolean bb) {
try {
double ratio = 0.0; // 缩放比例
File f = new File(filePath);
BufferedImage bi = ImageIO.read(f);
Image itemp = bi.getScaledInstance(width, height, bi.SCALE_SMOOTH);
// 计算比例
if ((bi.getHeight() > height) || (bi.getWidth() > width)) {
if (bi.getHeight() > bi.getWidth()) {
ratio = (new Integer(height)).doubleValue()
/ bi.getHeight();
} else {
ratio = (new Integer(width)).doubleValue() / bi.getWidth();
}
AffineTransformOp op = new AffineTransformOp(AffineTransform
.getScaleInstance(ratio, ratio), null);
itemp = op.filter(bi, null);
}
if (bb) {
BufferedImage image = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
Graphics2D g = image.createGraphics();
g.setColor(Color.white);
g.fillRect(0, 0, width, height);
if (width == itemp.getWidth(null))
g.drawImage(itemp, 0, (height - itemp.getHeight(null)) / 2,
itemp.getWidth(null), itemp.getHeight(null),
Color.white, null);
else
g.drawImage(itemp, (width - itemp.getWidth(null)) / 2, 0,
itemp.getWidth(null), itemp.getHeight(null),
Color.white, null);
g.dispose();
itemp = image;
}
ImageIO.write((BufferedImage) itemp, "jpg", f);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void addText() {
try {
FileInputStream inputstream = new FileInputStream("f:/test/t1.txt");// 将TXT文件内容转换成String类型
StringBuffer buffer = new StringBuffer();
String line; // 用来保存每行读取的内容
BufferedReader bufferreader = new BufferedReader(
new InputStreamReader(inputstream));
line = bufferreader.readLine(); // 读取第一行
while (line != null) { // 如果 line 为空说明读完了
buffer.append(line); // 将读到的内容添加到 buffer 中
line = bufferreader.readLine(); // 读取下一行
}
// 将读到 buffer 中的内容写出来
inputstream.close();
String filetext = buffer.toString();
System.out.println("该文本共有" + filetext.length() + "个字符");
String tmp = "";
int length = 18;
int height = 45;
String oldFile = "F:/11.jpg";
String newFile = "F:/test/11.jpg";
int i;
int x=0;
int t = 0;
int cent = 1;
while (filetext.length()-x>0) {
copyFile(oldFile, newFile);
File f = new File(newFile);
f.renameTo(new File("F:/test/" + cent + ".jpg"));
resize("F:/test/" + cent + ".jpg", 455, 640, true);// 我先将图设置成规定的大小!!
for (i = 0; i < filetext.length() / length
&& height * (i + 1) <= 420; i++) {
int k = 0;
for (int j = 0; j < length;) {
tmp = filetext.substring(x+length * i + j, x+length * i + j
+ 1);
pressText(tmp, "F:/test/" + cent + ".jpg", "微软雅黑",
Font.CENTER_BASELINE, Color.BLACK, Color.WHITE,
28, 25 + k, (i + 1) * height);
j++;
t = j + length * i;
k = k + 33;
}
}
x+=t;
System.out.print("此时已经输出字符数:");
System.out.println(x);
cent++;
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
addText();
}
}
相关文章推荐
- Java 遍历文件下jpg图片并解析图片
- Java根据随机数生成jpg图片
- java代码生成PDF文件-表格和图片
- 内存溢出WAS生成的文件javacore.***.txt 、heapdump.***.phd、core.***.dmp、Snap.***.trc说明
- Java图片生成水印
- java 利用输入输出流处理txt文件,并生成新txt文件
- java生成unicode编码格式的txt文件
- springboot搭建文件预览解决方案,支持目前主流格式office文件,txt文件,png,jpg等图片以及压缩文件的在线预览功能
- Java生成Txt文件及下载
- ASP 上传图片文件,生成水印图片
- java生成可执行文件方法及关于图片正确显示的问题
- java生成txt文件,读txt文件
- 赵祖辉 图片上传类;支持水印-日期文件夹-生成缩略图 ,支持多文件上传,
- java 生成pdf文件,添加图片
- java处理txt文件,并生成文件
- 【JAVA基础】0003--------图片生成缩略图、TIF转JPG
- Java解析PDF文件(PDFBOX、itext解析PDF)导出PDF中的子图片,去除PDF中的水印
- Thumbnailator java图片压缩,加水印,批量生成缩略图
- java文字生成水印图片并45度倾斜铺满图片(图例在下面)
- java 生成pdf文件,添加图片