首先加入工具类test用于画图使用
import javax. imageio. ImageIO;
import java. awt. *;
import java. awt. image. BufferedImage;
import java. io. File;
import java. io. FileInputStream;
import java. io. IOException;
public class Test {
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;
String fansAndScore2 = "数量:" + fansCount + "件" ;
int infoWidth = fm. stringWidth ( fansAndScore) ;
int infoWidth2 = fm. stringWidth ( fansAndScore2) ;
int infoWidthX = ( lovelyImageWidth - infoWidth) / 2 ;
int infoWidthX2 = ( lovelyImageWidth - infoWidth2) / 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 ) ;
textIntro1. drawString ( fansAndScore2, infoWidthX2, 135 ) ;
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 = "C:\\pdf\\20200616_145416_19614.png" ;
String result = "C:\\pdf\\20200616_145416_1961400.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