graphics2d 竖排文字 java_java在生成图片的时候,让文字竖排展示,如何实现?

这个博客介绍了一个Java类ImageUtil,它使用Graphics2D在生成图片时实现文字竖排展示。类中包含多种方法,如绘制指定颜色和字体大小的字符串到图片、图片缩放、旋转90度以及合并两张图像。通过此类,可以方便地创建含有竖排文字的自定义图片。
摘要由CSDN通过智能技术生成

展开全部

package honest.imageio;

import java.awt.Color;

import java.awt.Font;

import java.awt.Graphics;

import java.awt.image.BufferedImage;

import java.io.File;

import java.io.IOException;

import javax.imageio.ImageIO;

/**

* 图片操作类

*

* @e68a843231313335323631343130323136353331333337396239author

*

*/

public class ImageUtil {

private BufferedImage image;

private int width; // 图片宽度

private int height; // 图片高度

public ImageUtil(int width, int height) {

this.width = width;

this.height = height;

image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);

}

/**

* 创建一个含有指定颜色字符串的图片

*

* @param message

*            字符串

* @param fontSize

*            字体大小

* @param color

*            字体颜色

* @return 图片

*/

public BufferedImage drawString(String message, int fontSize, Color color) {

Graphics g = image.getGraphics();

g.setColor(color);

Font f = new Font("宋体", Font.BOLD, fontSize);

g.setFont(f);

int len = message.length();

g.drawString(message, (width - fontSize * len) / 2,

(height + (int) (fontSize / 1.5)) / 2);

g.dispose();

return image;

}

/**

* 缩放图片

*

* @param scaleW

*            水平缩放比例

* @param scaleY

*            垂直缩放比例

* @return

*/

public BufferedImage scale(double scaleW, double scaleH) {

width = (int) (width * scaleW);

height = (int) (height * scaleH);

BufferedImage newImage = new BufferedImage(width, height,

image.getType());

Graphics g = newImage.getGraphics();

g.drawImage(image, 0, 0, width, height, null);

g.dispose();

image = newImage;

return image;

}

/**

* 旋转90度旋转

*

* @return 对应图片

*/

public BufferedImage rotate() {

BufferedImage dest = new BufferedImage(height, width,

BufferedImage.TYPE_INT_ARGB);

for (int i = 0; i 

for (int j = 0; j 

dest.setRGB(height - j - 1, i, image.getRGB(i, j));

}

image = dest;

return image;

}

/**

* 合并两个图像

*

* @param anotherImage

*            另一张图片

* @return 合并后的图片,如果两张图片尺寸不一致,则返回null

*/

public BufferedImage mergeImage(BufferedImage anotherImage) {

int w = anotherImage.getWidth();

int h = anotherImage.getHeight();

if (w != width || h != height) {

return null;

}

for (int i = 0; i 

for (int j = 0; j 

int rgb1 = image.getRGB(i, j);

int rgb2 = anotherImage.getRGB(i, j);

Color color1 = new Color(rgb1);

Color color2 = new Color(rgb2);

// 如果该位置两张图片均没有字体经过,则跳过

// 如果跳过,则最后将会是黑色背景

if (color1.getRed() + color1.getGreen() + color1.getBlue()

+ color2.getRed() + color2.getGreen()

+ color2.getBlue() == 0) {

continue;

}

Color color = new Color(

(color1.getRed() + color2.getRed()) / 2,

(color1.getGreen() + color2.getGreen()) / 2,

(color1.getBlue() + color2.getBlue()) / 2);

image.setRGB(i, j, color.getRGB());

}

}

return image;

}

/**

* 保存图片int rgb1 = image.getRGB(i, j); int rgb2 = anotherImage.getRGB(i, j);

* rgb2 = rgb1 & rgb2; image.setRGB(height - i, j, rgb2);

*

* @param filePath

*            图片路径

*/

public void save(String filePath) {

try {

ImageIO.write(image, "png", new File(filePath));

} catch (IOException e) {

e.printStackTrace();

}

}

/**

* 得到对应的图片

*

* @return

*/

public BufferedImage getImage() {

return image;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值