一篇头像裁剪,组合图片,生成海报的代码

 1 /**
 2      * 头像裁剪,组合图片,生成海报
 3      * @param picUrl
 4      * @param headPic
 5      * @throws Exception
 6      */
 7         public void createPost(String picUrl,String headPic) throws Exception{
 8             int width1 = 130;
 9             URL url1 = new URL(headPic);//背景图片
10             BufferedImage headPicImg = ImageIO.read(url1);
11             // 透明底的图片
12              BufferedImage formatAvatarImage = new BufferedImage(width1, width1, BufferedImage.TYPE_INT_RGB);
13 
14             Graphics2D graphics = formatAvatarImage.createGraphics();
15             graphics.setColor(Color.white);    //GREEN:绿色;  红色:RED;   灰色:GRAY 白色:white
16             graphics.fillRect(0, 0, width1, width1);
17             //把图片切成一个圓
18              graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
19                 //留一个像素的空白区域,这个很重要,画圆的时候把这个覆盖
20              int border =1;
21              //图片是一个圆型
22              Ellipse2D.Double shape = new Ellipse2D.Double(border, border, width1 - border * 2, width1 - border * 2);
23              //需要保留的区域
24              graphics.setClip(shape);
25              graphics.drawImage(headPicImg, border, border, width1 - border * 2, width1 - border * 2, null);
26              //绘制二维码
27             HashMap hints = new HashMap();
28             hints.put(EncodeHintType.CHARACTER_SET, "utf-8");    //指定字符编码为“utf-8”
29             hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);  //指定二维码的纠错等级为中级
30             hints.put(EncodeHintType.MARGIN, 1);    //设置图片的边距
31             BitMatrix bitMatrix = new MultiFormatWriter().encode( "爱你一万年", BarcodeFormat.QR_CODE, 250, 250, hints);
32             BufferedImage ii = toBufferedImage(bitMatrix);
33             URL url = new URL(picUrl);//背景图片
34             BufferedImage picUrlImg = ImageIO.read(url);
35 
36             //绘制宽=480,长=640的图板
37             int width = 750, hight = 1606;
38             BufferedImage image = new BufferedImage(width, hight, BufferedImage.TYPE_INT_RGB);
39             //获取图形上下文,graphics想象成一个画笔
40              graphics = (Graphics2D) image.getGraphics();
41             //消除线条锯齿
42             graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
43             //对指定的矩形区域填充颜色
44             graphics.setColor(Color.white);    //GREEN:绿色;  红色:RED;   灰色:GRAY 白色:white
45             graphics.fillRect(0, 0, 750, 1606);
46             //对指定的矩形区域填充颜色
47             graphics.drawImage(picUrlImg,0,0,null);
48             graphics.drawImage(formatAvatarImage,30,1326,null);
49             graphics.drawImage(ii,470,1326,null);
50 
51             Font font=new Font("宋体",Font.BOLD,28);
52             graphics.setFont(font);
53             graphics.setColor(Color.BLACK);
54             graphics.drawString("徐睿", 170, 1386);
55             graphics.drawString("18703652539", 170, 1416);
56              font=new Font("宋体",Font.ITALIC,25);
57             graphics.setFont(font);
58             graphics.setColor(Color.BLACK);
59             graphics.drawString("我是你的专属客服,您有任何关于", 50, 1496);
60             graphics.drawString("信用卡申请的相关问题可随时向我", 50, 1536);
61             graphics.drawString("咨询", 50, 1576);
62 
63             //graphics.setPaintMode();
64             //graphics.translate(400, 600);
65 
66             graphics.dispose();//释放此图形的上下文并释放它所使用的所有系统资源
67             FileOutputStream out = new FileOutputStream("D:\\yy/1.jpeg");
68             ImageIO.write(image, "JPEG", out);
69 
70             out.flush();
71             out.close();
72 
73         }

 

转载于:https://www.cnblogs.com/hxz-nl/p/10736837.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值