java实现⽤⽤户头像代替⼩程序码的logo(任意图⽚替换⼩程
序码logo)
由于个⼈⽔平有限,实在是不能保证代码的严谨性,我只能说是可以实现功能,⽤来个性化⼩程序码logo
//⽣成⽂件名称,这个不是必须的,可以根据⾃⼰的业务逻辑来写
Random random = new Random();
int rannum = (int) (Double() * (99999 - 10000 + 1)) + 10000;
SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
String newFileName = df.format(new Date())+rannum+".png";
//截取为正⽅形图⽚,长宽⾃定义,我⾃⼰估摸着是195,这个可以根据⾃⼰情况⾃⼰设置⼤⼩
try {
//此处是要替代logo的⾃定义图⽚路径,⾃定义图⽚不要⽤透明的或者半透明的
BufferedImage image = ad(new File("C:\\Program Files\\tomcat-huipiao\\webapps\\Financial\\img\\"+imgNameString));
//int w = Width();
//int h = Height();
BufferedImage output = new BufferedImage(195, 195, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = ateGraphics();
output = g2.getDeviceConfiguration().createCompatibleImage(195, 195, Transparency.TRANSLUCENT);
g2.dispose();
g2 = ateGraphics();
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.fillRoundRect(0, 0,195, 195, 720, 720);
g2.setComposite(AlphaComposite.SrcIn);
g2.drawImage(image, 0, 0, 195, 195, null);
g2.dispose();
//这是⽣成的临时替换logo图⽚的保存路径
ImageIO.write(output, "png", new File("C:\\Program Files\\tomcat-huipiao\\webapps\\Financial\\img\\"+newFileName));
} catch (IOException e) {
e.printStackTrace();
}
//⽣产新的⼆维码图⽚
try{
//此处是⼩程序码的路径
BufferedImage appletImg = ad(new FileInputStream("C:\\Program Files\\tomcat-huipiao\\webapps\\Financial\\img\\"+codeNameString));    Graphics2D g2d = ateGraphics();
// 设置抗锯齿的属性
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
//此处是替换logo的临时图⽚路径
BufferedImage centerImg = ad(new File("C:\\Program Files\\tomcat-huipiao\\webapps\\Financial\\img\\"+newFileName));
g2d.Width(), Height(), Image.SCALE_SMOOTH), (Width() - W
// 关闭资源
g2d.dispose();
//⽣成新的⼆维码,覆盖原来的,此处为原⼩程序码路径,如需另为保存,请⾃定义路径
ImageIO.write(appletImg, "png", new File("C:\\Program Files\\tomcat-huipiao\\webapps\\Financial\\img\\"+codeNameString));
}catch (Exception e){
e.printStackTrace();
}有趣的java小程序
//删除⽣成的临时替代logo⽂件
new File("C:\\Program Files\\tomcat-huipiao\\webapps\\Financial\\img\\"+newFileName).delete();
//有啥优化意见⼤家可以提出

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。