Graphics2D基本图形合成
最近这个版本迭代,竟然很有缘分的和Graphics2D有了很严重的交集,请问这是好事还是坏事呢?其实说实话,对于自己整合的图形并不是很满意,但是Graphics2D的功能还是很强大的,通过博客来记录我与它的美妙的交集。
Graphics2D构造方法详细信息:
构建一个新的 Graphics2D 对象。由于 Graphics2D 是一个抽象类,而且它必须由不同输出设备的子类定制,所以无法直接创建 Graphics2D 对象。相反,Graphics2D 对象必须从另一个 Graphics2D 对象获得、由某个 Component 创建,或者从 BufferedImage 之类的图像对象获得。
主要功能:
图文混合生成一张新的图片;
实践过程:
详情方法
public static BufferedImage graphicsGenerationlovely(BufferedImage imageQR, String lovelyNick, int fansCount, String lovelyScore) {
//整体图的高度和宽度
int lovelyImageWidth = 400;
int lovelyImageHeight = 531;
//整体图合成
BufferedImage bufferedImage = new BufferedImage(lovelyImageWidth, lovelyImageHeight, BufferedImage.TYPE_INT_RGB);
//设置图片的背景色
Graphics2D main = bufferedImage.createGraphics();
main.fillRect(0, 0, lovelyImageWidth, lovelyImageHeight);
//宝宝名称整合
Graphics2D textIntro = bufferedImage.createGraphics();
//颜色
textIntro.setColor(new Color(51, 51, 51));
//设置字体
Font introFont = new Font("黑体", Font.PLAIN, 30);
textIntro.setFont(introFont);
FontMetrics fm = textIntro.getFontMetrics(introFont);
int nickWidth = fm.stringWidth(lovelyNick);
//因为名称为不固定,所以设置据左位置
int nickWidthX = (lovelyImageWidth - nickWidth) / 2 - 10;
//字体失真处理
textIntro.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
textIntro.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_GASP);
textIntro.drawString(lovelyNick, nickWidthX, 50);
//宝宝评价和粉丝个数
Graphics2D textIntro1 = bufferedImage.createGraphics();
textIntro1.setColor(new Color(153, 153, 153));
Font intro1Font = new Font("微软雅黑", Font.PLAIN, 26);
textIntro1.setFont(intro1Font);
String fansAndScore = "评价:" + lovelyScore + "分 " + " | " + " 粉丝:" + fansCount;
int infoWidth = fm.stringWidth(fansAndScore);
int infoWidthX = (lovelyImageWidth - infoWidth) / 2;
//失真处理
textIntro1.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
textIntro1.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_GASP);
textIntro1.drawString(fansAndScore, infoWidthX, 100);
//可爱宝宝图
Graphics graphicsQR = bufferedImage.getGraphics();
graphicsQR.drawImage(imageQR, 70, 150, 250, 250, null);
//固定文字
Graphics2D fixed = bufferedImage.createGraphics();
fixed.setColor(new Color(51, 51, 51));
Font fixedFont = new Font("黑体", Font.PLAIN, 26);
fixed.setFont(fixedFont);
//失真处理
fixed.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
fixed.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_GASP);
fixed.drawString("可爱点赞奥,哈哈哈", 70, 500);
return bufferedImage;
}
调用方法
public static void main(String[] args) throws IOException {
//需要添加的图片
String imageQRUrl = "/Users/huoyajing/Desktop/image1.jpg";
//保存图片的路径
String result = "/Users/huoyajing/Desktop/savelovely.png";
FileInputStream inputStream = new FileInputStream(imageQRUrl);
BufferedImage imageQR = ImageIO.read(inputStream);
String lovelyNick = "可爱熊宝宝";
int fansCount = 10;
String socre = "4.9";
BufferedImage bufferedImage = graphicsGenerationlovely(imageQR, lovelyNick, fansCount, socre);
int temp = result.lastIndexOf(".") + 1;
ImageIO.write(bufferedImage, result.substring(temp), new File(result));
}
最终效果图
解释
其实功能很简单,主要就是把文字和图片整合一起,通过设置文字样式,位置一级避免失真,来达到图片的最完美的合成结果。
基本属性实践了解
详情代码
public static BufferedImage bufferedImageTest1(){
BufferedImage bufferedImage = new BufferedImage(200, 150, BufferedImage.TYPE_INT_RGB);
Graphics2D main = bufferedImage.createGraphics();
//画一条线
main.drawLine(5,5,5,80);
main.setColor(Color.CYAN);
//画一个点
main.drawLine(100,100,100,100);
//画线框,矩形或长方形
main.setColor(Color.BLUE);
main.drawRect(10,10,10,10);
//着色快
main.fillRect(10,25,10,10);
//圆角矩形
main.setColor(Color.RED);
main.drawRoundRect(30,10,20,20,5,5);
//圆角矩形着色快
main.fillRoundRect(30,35,20,20,5,5);
//圆形
main.setColor(Color.GRAY);
main.drawRoundRect(60,10,20,20,20,20);
//圆形着色
main.fillRoundRect(60,35,20,20,20,20);
//三维矩形
main.draw3DRect(90,10,20,20,true);
//椭圆形
main.drawOval(90,40,20,30);
//橡皮擦功能
main.setColor(Color.GREEN);
main.fillOval(130,10,30,30);
main.clearRect(140,20,10,10);
return bufferedImage;
}
最终效果图
了解了这些,基本的图形合成功能就够用了。