300字范文,内容丰富有趣,生活中的好帮手!
300字范文 > java实现多张图片和文字合并 java图片合成示例代码

java实现多张图片和文字合并 java图片合成示例代码

时间:2023-01-30 06:52:54

相关推荐

java实现多张图片和文字合并 java图片合成示例代码

我们通过java把下面的背景图和二维码以及一段文字合并成一张新的图片

背景图片:

二维码图片:

publicclassMain{/***合成图片**@parambackgroundPath*@paramqrCodePath*@parammessage*@paramoutPutPath*@throwsIOException*/publicstaticvoidoverlapImage(StringbackgroundPath,StringqrCodePath,Stringmessage,StringoutPutPath)throwsIOException{//设置背景图片大小BufferedImagebackgroundImage=resizeImage(566,230,ImageIO.read(newFile(backgroundPath)));//设置二维码图片大小BufferedImageqrCodeImage=resizeImage(150,150,ImageIO.read(newFile(qrCodePath)));Graphics2Dgraphics=backgroundImage.createGraphics();//在背景图片上添加文字graphics.setColor(Color.white);graphics.setFont(newFont("微软雅黑",Font.BOLD,20));graphics.drawString(message,100,40);//在背景图片上添加二维码图片graphics.drawImage(qrCodeImage,210,60,qrCodeImage.getWidth(),qrCodeImage.getHeight(),null);graphics.dispose();//输出新的图片ImageIO.write(backgroundImage,"png",newFile(outPutPath));}/***重新设置图片大小**@paramwidth*@paramheight*@parambufferedImage*@return*/privatestaticBufferedImageresizeImage(intwidth,intheight,BufferedImagebufferedImage){BufferedImagenewBufferedImage=newBufferedImage(width,height,BufferedImage.TYPE_INT_RGB);newBufferedImage.getGraphics().drawImage(bufferedImage.getScaledInstance(width,height,Image.SCALE_SMOOTH),0,0,null);returnnewBufferedImage;}publicstaticvoidmain(String[]args)throwsIOException{StringbackgroundPath="/Users/admin/Desktop/background.png";StringqrCodePath="/Users/admin/Desktop/qrcode.png";Stringmessage="java菜市场-专注于技术干货的免费分享";StringoutPutPath="/Users/admin/Desktop/newImage.png";overlapImage(backgroundPath,qrCodePath,message,outPutPath);}}

最终生成的合成后的图片:

如果想了解通过java实现图片压缩、裁剪、翻转、添加水印等,请参考这篇文章:

java使用google开源工具Thumbnailator实现图片压缩

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。