星空网 > 软件开发 > 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;
}
}



原标题:姓名倒转写入到图片

关键词:图片

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

德威国际:https://www.goluckyvip.com/tag/30183.html
德威国际供应链:https://www.goluckyvip.com/tag/30184.html
德信海外仓:https://www.goluckyvip.com/tag/30185.html
德阳跨境电子商务协会:https://www.goluckyvip.com/tag/30186.html
德阳市:https://www.goluckyvip.com/tag/30187.html
德阳综试区:https://www.goluckyvip.com/tag/30188.html
深圳到西安自驾路线攻略 深圳到西安自驾最佳路线:https://www.vstour.cn/a/411228.html
松花蛋是哪里的特产松花蛋的产地:https://www.vstour.cn/a/411229.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流