你的位置:首页 > Java教程

[Java教程]姓名倒转写入到图片


package com.tbc.app.tam.main;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;

public class TextChangToImage {

/**
* 创建图片
* @param image 图片对象
* @param fileLocation 图片路径
*/
void createImage(BufferedImage image ,String fileLocation) {
try {
FileOutputStream fos = new FileOutputStream(fileLocation);
BufferedOutputStream bos = new BufferedOutputStream(fos);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(bos);
encoder.encode(image);
bos.close();
} catch (Exception e) {
e.printStackTrace();
}
}

/**
* 将文本绘制到图片上
* @param text 文本
* @param imgurl 生成的图片路径
*/
public void graphicsGeneration(String text, String imgurl) {

int imageWidth = 600;// 图片的宽度
int imageHeight = 250;// 图片的高度
int textMaxWidth = 450;

BufferedImage image = new BufferedImage(imageWidth, imageHeight,BufferedImage.TYPE_INT_RGB);
Graphics graphics = image.getGraphics();
graphics.setColor(Color.WHITE);
graphics.fillRect(0, 0, imageWidth, imageHeight);
graphics.setColor(Color.BLACK);
graphics.setFont(new Font("微软雅黑", 0, 100));
int stringWidth = graphics.getFontMetrics().stringWidth(text);
if(800<stringWidth && stringWidth<=1000){ //限制了图片大小,根据文本字数限制更改文字大小
graphics.setFont(new Font("微软雅黑", 0, 90));
}else if(1000<stringWidth && stringWidth <=1800){
graphics.setFont(new Font("微软雅黑", 0, 65));
}else if(1800<stringWidth && stringWidth<=4000){
graphics.setFont(new Font("微软雅黑", 0, 45));
}else if(4000<stringWidth && stringWidth <=6000){
graphics.setFont(new Font("微软雅黑", 0, 35));
}
int stringAscent = graphics.getFontMetrics().getAscent();
int textHeight = 100;
int j=0;
if(stringWidth <= textMaxWidth){
graphics.drawString(text, imageWidth/2-stringWidth/2, textHeight);
}else{
int textWidth = 0;
StringBuffer strBuffer = new StringBuffer();
int textCount = 1;
for (int i = 0;i < text.length();i++) {
String tempStr = String.valueOf(text.charAt(i));
int tempWidth = graphics.getFontMetrics().stringWidth(tempStr);
if((textWidth + tempWidth)>textMaxWidth){ //大于固定的文本宽度则换行
int textHeightTemp = graphics.getFontMetrics().stringWidth(strBuffer.toString());
graphics.drawString(strBuffer.toString(), imageWidth/2-textHeightTemp/2, textHeight);
textHeight+=stringAscent;
textWidth = tempWidth;
strBuffer.setLength(0);
}else{
textWidth += tempWidth;
}
strBuffer.append(tempStr);
if(i == text.length()-1){
String textTemp = strBuffer.toString();
int textHeightTemp = graphics.getFontMetrics().stringWidth(textTemp);
graphics.drawString(strBuffer.toString(), imageWidth/2-textHeightTemp/2, textHeight);
}
}
}

BufferedImage bimg = null;
try {
bimg = javax.imageio.ImageIO.read(new java.io.File(imgurl));
} catch (Exception e) {
}

if (bimg != null)
graphics.drawImage(bimg, 230, 0, null);
graphics.dispose();
image = rotateImage(image,180); //对绘制的图片做旋转
createImage(image,imgurl);

}

public static void main(String[] args) {
TextChangToImage cg = new TextChangToImage();
try {
cg.graphicsGeneration("张三李四王五", "D://8.jpg");
/* BufferedImage src = ImageIO.read(new File("d:/8.jpg"));
BufferedImage des = rotateImage(src, 90); //对已有的图片进行旋转
Assert.assertNotNull(des);
Assert.assertTrue(ImageIO.write(des, "jpg", new File("d:/dog2.jpg")));
*/
} catch (Exception e) {
e.printStackTrace();
}


}

/**
* 旋转图片(该方法对180、360以外的角度旋转会更改图片长宽)
* @param bufferedimage 图片对象
* @param degree 旋转角度
* @return
*/
public static BufferedImage rotateImage(final BufferedImage bufferedimage,
final int degree) {
int w = bufferedimage.getWidth();
int h = bufferedimage.getHeight();
int type = bufferedimage.getColorModel().getTransparency();
BufferedImage img;
Graphics2D graphics2d;
(graphics2d = (img = new BufferedImage(w, h, type))
.createGraphics()).setRenderingHint(
RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BILINEAR);
graphics2d.rotate(Math.toRadians(degree), w / 2, h / 2);
graphics2d.drawImage(bufferedimage, 0, 0, null);
graphics2d.dispose();
return img;
}
}