DownloadWXCodeUtil.java 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. package com.iamberry.wechat.tools;
  2. import javax.imageio.ImageIO;
  3. import java.awt.*;
  4. import java.awt.image.BufferedImage;
  5. import java.io.File;
  6. import java.io.IOException;
  7. import java.io.InputStream;
  8. import java.net.URL;
  9. import java.net.URLConnection;
  10. public class DownloadWXCodeUtil {
  11. /**
  12. * 下载微信二维码
  13. * @param urlString
  14. * @param filename 存储名称
  15. * @param savePath 路径
  16. * @param width 宽度
  17. * @param height 高度
  18. * @throws Exception
  19. */
  20. public static void download(String urlString, String filename,String savePath,int width,int height) throws Exception {
  21. // 输出的文件流
  22. File sf=new File(savePath);
  23. if(!sf.exists()){
  24. sf.mkdirs();
  25. }
  26. // 构造URL
  27. URL url = new URL(urlString);
  28. // 打开连接
  29. URLConnection con = url.openConnection();
  30. //设置请求超时为5s
  31. con.setConnectTimeout(5*1000);
  32. // 输入流
  33. InputStream is = con.getInputStream();
  34. BufferedImage bi = ImageIO.read(is);
  35. ImageIO.write(bi, "png", new File(savePath+filename));
  36. // JPEGImageDecoder decoderFile = JPEGCodec.createJPEGDecoder(is);
  37. // BufferedImage image = decoderFile.decodeAsBufferedImage();
  38. // Image srcImg = image;
  39. // image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
  40. // image.getGraphics().drawImage(srcImg.getScaledInstance(width, height, Image.SCALE_SMOOTH), 0,0, null);
  41. // ImageIO.write(image, "JPEG", new File(savePath+filename));
  42. is.close();
  43. }
  44. /**
  45. * 下载微信二维码
  46. * @param url
  47. * @param filename
  48. * @param savePath
  49. * @param width
  50. * @param height
  51. * @param isCircular 是否为圆
  52. */
  53. public static void downloadByNIO2(String url, String filename,String savePath,int width,int height,boolean isCircular) {
  54. try (InputStream ins = new URL(url).openStream()) {
  55. BufferedImage bi = ImageIO.read(ins);
  56. if(isCircular){
  57. int w = width;
  58. int h = height;
  59. BufferedImage output = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
  60. Graphics2D g2 = output.createGraphics();
  61. output = g2.getDeviceConfiguration().createCompatibleImage(w, h, Transparency.TRANSLUCENT);
  62. g2.dispose();
  63. g2 = output.createGraphics();
  64. g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  65. g2.fillRoundRect(0, 0,w, h, 720, 720);
  66. g2.setComposite(AlphaComposite.SrcIn);
  67. g2.drawImage(bi, 0, 0, w, h, null);
  68. g2.dispose();
  69. ImageIO.write(output, "png", new File(savePath+filename));
  70. }else{
  71. Image srcImg = bi;
  72. bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
  73. bi.getGraphics().drawImage(srcImg.getScaledInstance(width, height, Image.SCALE_SMOOTH), 0,0, null);
  74. ImageIO.write(bi, "png", new File(savePath+filename));
  75. }
  76. ins.close();
  77. } catch (IOException e) {
  78. e.printStackTrace();
  79. }
  80. }
  81. }