import com.sun.image.codec.jpeg.ImageFormatException;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageDecoder;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
import javax.imageio.ImageIO;
import javax.swing.;
import java.awt.;
import java.awt.font.FontRenderContext;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.*;
public class MainIn {
static String picturePath;
static String pictureInName = "/bg.jpeg";
static String pictureOutName = "/bg_out.jpeg";
public static void main(String[] args) {
String _dir = System.getProperty("user.dir");
picturePath = _dir + File.separator + "src" + File.separator;
// exportImg1();
drawTextToPicture("我好,中國", picturePath + "/head_img.png");
}
///在圖片上畫文字
public static void drawTextToPicture(String username, String headImg) {
try {
//1.jpg是你的 主圖片的路徑
InputStream is = new FileInputStream(picturePath + pictureInName);
//通過JPEG圖象流創(chuàng)建JPEG數(shù)據(jù)流解碼器
JPEGImageDecoder jpegDecoder = JPEGCodec.createJPEGDecoder(is);
//解碼當前JPEG數(shù)據(jù)流药有,返回BufferedImage對象
BufferedImage buffImg = jpegDecoder.decodeAsBufferedImage();
//得到畫筆對象
Graphics g = buffImg.getGraphics();
//創(chuàng)建你要附加的圖象城榛。
//小圖片的路徑
ImageIcon imgIcon = new ImageIcon(headImg);
//得到Image對象漾岳。
Image img = imgIcon.getImage();
//將小圖片繪到大圖片上。
//5,300 .表示你的小圖片在大圖片上的位置搪搏。
g.drawImage(img, 400, 15, null);
//設置顏色。
g.setColor(Color.BLACK);
//最后一個參數(shù)用來設置字體的大小
Font f = new Font("宋體", Font.PLAIN, 25);
Color mycolor = Color.red;//new Color(0, 0, 255);
g.setColor(mycolor);
g.setFont(f);
//10,20 表示這段文字在圖片上的位置(x,y) .第一個是你設置的內(nèi)容瘪撇。
g.drawString(username, 100, 135);
g.dispose();
OutputStream os;
os = new FileOutputStream(picturePath + pictureOutName);
//創(chuàng)鍵編碼器堡赔,用于編碼內(nèi)存中的圖象數(shù)據(jù)。
JPEGImageEncoder en = JPEGCodec.createJPEGEncoder(os);
en.encode(buffImg);
is.close();
os.close();
} catch (ImageFormatException | IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
///創(chuàng)建一個文本畫布
public static void createTextCanvas() {
int width = 100;
int height = 100;
String s = "你好";
File file = new File(picturePath + pictureInName);
Font font = new Font("Serif", Font.BOLD, 10);
BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = (Graphics2D) bi.getGraphics();
g2.setBackground(Color.WHITE);
g2.clearRect(0, 0, width, height);
g2.setPaint(Color.RED);
FontRenderContext context = g2.getFontRenderContext();
Rectangle2D bounds = font.getStringBounds(s, context);
double x = (width - bounds.getWidth()) / 2;
double y = (height - bounds.getHeight()) / 2;
double ascent = -bounds.getY();
double baseY = y + ascent;
g2.drawString(s, (int) x, (int) baseY);
try {
ImageIO.write(bi, "jpg", file);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
2021年01月15日13:29:09更新
文字增加抗鋸齒, 文本x y 坐標校準
~~~
///在圖片上畫文字
public static void drawTextToPicture(List<DetailCoordinate> _detailList) {
try {
BufferedImage _mainImage = getImage(picturePath + pictureInName);
SunGraphics2D g = (SunGraphics2D) _mainImage.getGraphics();
g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB);
OutputStream os = new FileOutputStream(picturePath + pictureOutName);
for (DetailCoordinate itemCoordinate : _detailList) {
if (itemCoordinate.type == 0) {
//最后一個參數(shù)用來設置字體的大小
Font f = new Font(Font.SERIF, Font.PLAIN, itemCoordinate.size);
Color myColor = fromStrToARGB(itemCoordinate.getColor());//new Color(0, 0, 255);
g.setColor(myColor);
g.setFont(f);
FontMetrics fm = g.getFontMetrics();
g.drawString(itemCoordinate.txt, itemCoordinate.x, fm.getMaxAscent() + itemCoordinate.y);
} else if (itemCoordinate.type == 1) {
//創(chuàng)建你要附加的圖象甸祭。
//小圖片的路徑
ImageIcon imgIcon = new ImageIcon(itemCoordinate.txt);
//得到Image對象缕碎。
Image img = imgIcon.getImage();
//將小圖片繪到大圖片上。
//5,300 .表示你的小圖片在大圖片上的位置池户。
g.drawImage(img, itemCoordinate.x, itemCoordinate.y, null);
//設置顏色咏雌。
g.setColor(Color.BLACK);
}
}
g.dispose();
_mainImage.flush();
ImageIO.write(_mainImage, "png", os);
} catch (ImageFormatException | IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}