您的位置:首页 > 编程语言 > Java开发

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();

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: