1,下载网络图片

  1. package org.jeecg.modules.ppl.utils;
  2. import java.awt.Graphics2D;
  3. import java.awt.image.BufferedImage;
  4. import java.io.*;
  5. import java.net.MalformedURLException;
  6. import java.net.URL;
  7. import javax.imageio.ImageIO;
  8. import javax.swing.ImageIcon;
  9. /**
  10. * 下载网络图片
  11. */
  12. public class PngUtil{
  13. /**
  14. * 通过链接url下载图片
  15. * inImageUrl 网络图片url
  16. * putImagePath 保存的本地路径
  17. */
  18. public static String downloadPic(String inImageUrl,String putImagePath) {
  19. URL url = null;
  20. int imageNumber = 0;
  21. try {
  22. url = new URL(inImageUrl);
  23. DataInputStream dataInputStream = new DataInputStream(url.openStream());
  24. putImagePath = "E:/upload/media/lanzhou2.png";
  25. FileOutputStream fileOutputStream = new FileOutputStream(new File(putImagePath));
  26. ByteArrayOutputStream output = new ByteArrayOutputStream();
  27. byte[] buffer = new byte[1024];
  28. int length;
  29. while ((length = dataInputStream.read(buffer)) > 0) {
  30. output.write(buffer, 0, length);
  31. }
  32. byte[] context = output.toByteArray();
  33. fileOutputStream.write(output.toByteArray());
  34. dataInputStream.close();
  35. fileOutputStream.close();
  36. } catch (MalformedURLException e) {
  37. e.printStackTrace();
  38. putImagePath = "错误:" + e.getMessage();
  39. } catch (IOException e) {
  40. e.printStackTrace();
  41. putImagePath = "错误:" + e.getMessage();
  42. }
  43. return putImagePath;
  44. }
  45. }

2,图片转换成透明底图片(推荐)

  1. package org.jeecg.modules.ppl.utils;
  2. import java.awt.*;
  3. import java.awt.image.BufferedImage;
  4. import java.io.*;
  5. import java.net.MalformedURLException;
  6. import java.net.URL;
  7. import javax.imageio.ImageIO;
  8. import javax.imageio.stream.FileImageInputStream;
  9. import javax.imageio.stream.FileImageOutputStream;
  10. import javax.swing.ImageIcon;
  11. /**
  12. * 生成透明图片
  13. */
  14. public class PngUtil{
  15. /**
  16. * 通过链接url下载图片
  17. * inImageUrl 网络图片url
  18. * putImagePath 保存的本地路径
  19. */
  20. public static String downloadPic(String inImageUrl,String putImagePath) {
  21. URL url = null;
  22. int imageNumber = 0;
  23. try {
  24. url = new URL(inImageUrl);
  25. DataInputStream dataInputStream = new DataInputStream(url.openStream());
  26. putImagePath = "E:/upload/media/lanzhou2.png";
  27. FileOutputStream fileOutputStream = new FileOutputStream(new File(putImagePath));
  28. ByteArrayOutputStream output = new ByteArrayOutputStream();
  29. byte[] buffer = new byte[1024];
  30. int length;
  31. while ((length = dataInputStream.read(buffer)) > 0) {
  32. output.write(buffer, 0, length);
  33. }
  34. byte[] context = output.toByteArray();
  35. fileOutputStream.write(output.toByteArray());
  36. dataInputStream.close();
  37. fileOutputStream.close();
  38. } catch (MalformedURLException e) {
  39. e.printStackTrace();
  40. putImagePath = "错误:" + e.getMessage();
  41. } catch (IOException e) {
  42. e.printStackTrace();
  43. putImagePath = "错误:" + e.getMessage();
  44. }
  45. return putImagePath;
  46. }
  47. /**
  48. * @param imgSrc 原图片地址
  49. * @param imgTarget 新图片地址
  50. * @return 转换结果ture or false
  51. */
  52. public static boolean transferAlpha2File(String imgSrc, String imgTarget) {
  53. File file = new File(imgSrc);
  54. InputStream is = null;
  55. boolean result = false;
  56. try {
  57. is = new FileInputStream(file);
  58. // 如果是MultipartFile类型,那么自身也有转换成流的方法:is = file.getInputStream();
  59. BufferedImage bi = ImageIO.read(is);
  60. Image image = (Image) bi;
  61. ImageIcon imageIcon = new ImageIcon(image);
  62. BufferedImage bufferedImage = new BufferedImage(imageIcon.getIconWidth(), imageIcon.getIconHeight(),BufferedImage.TYPE_4BYTE_ABGR);
  63. Graphics2D g2D = (Graphics2D) bufferedImage.getGraphics();
  64. g2D.drawImage(imageIcon.getImage(), 0, 0, imageIcon.getImageObserver());
  65. int alpha = 0;
  66. for (int j1 = bufferedImage.getMinY(); j1 < bufferedImage.getHeight(); j1++) {
  67. for (int j2 = bufferedImage.getMinX(); j2 < bufferedImage.getWidth(); j2++) {
  68. int rgb = bufferedImage.getRGB(j2, j1);
  69. int R = (rgb & 0xff0000) >> 16;
  70. int G = (rgb & 0xff00) >> 8;
  71. int B = (rgb & 0xff);
  72. if (((255 - R) < 30) && ((255 - G) < 30) && ((255 - B) < 30)) {
  73. rgb = ((alpha + 1) << 24) | (rgb & 0x00ffffff);
  74. }
  75. bufferedImage.setRGB(j2, j1, rgb);
  76. }
  77. }
  78. g2D.drawImage(bufferedImage, 0, 0, imageIcon.getImageObserver());
  79. result = ImageIO.write(bufferedImage, "png", new File(imgTarget));// 直接输出文件
  80. // result = ImageIO.write(bufferedImage, "jpg", new File(imgTarget));// 直接输出文件
  81. } catch (Exception e) {
  82. e.printStackTrace();
  83. result = false;
  84. } finally {
  85. if (is != null) {
  86. try {
  87. is.close();
  88. } catch (IOException e) {
  89. // TODO Auto-generated catch block
  90. }
  91. }
  92. }
  93. return result;
  94. }
  95. /**
  96. * 透明图片的byte数组
  97. * @param imgSrc
  98. * @return
  99. */
  100. public static byte[] transferAlpha2Byte(String imgSrc) {
  101. ByteArrayOutputStream byteArrayOutputStream = null;
  102. File file = new File(imgSrc);
  103. InputStream is = null;
  104. byte[] result = null;
  105. try {
  106. is = new FileInputStream(file);
  107. // 如果是MultipartFile类型,那么自身也有转换成流的方法:is = file.getInputStream();
  108. BufferedImage bi = ImageIO.read(is);
  109. Image image = (Image) bi;
  110. ImageIcon imageIcon = new ImageIcon(image);
  111. BufferedImage bufferedImage = new BufferedImage(imageIcon.getIconWidth(), imageIcon.getIconHeight(),BufferedImage.TYPE_4BYTE_ABGR);
  112. Graphics2D g2D = (Graphics2D) bufferedImage.getGraphics();
  113. g2D.drawImage(imageIcon.getImage(), 0, 0, imageIcon.getImageObserver());
  114. int alpha = 0;
  115. for (int j1 = bufferedImage.getMinY(); j1 < bufferedImage.getHeight(); j1++) {
  116. for (int j2 = bufferedImage.getMinX(); j2 < bufferedImage.getWidth(); j2++) {
  117. int rgb = bufferedImage.getRGB(j2, j1);
  118. int R = (rgb & 0xff0000) >> 16;
  119. int G = (rgb & 0xff00) >> 8;
  120. int B = (rgb & 0xff);
  121. if (((255 - R) < 30) && ((255 - G) < 30) && ((255 - B) < 30)) {
  122. rgb = ((alpha + 1) << 24) | (rgb & 0x00ffffff);
  123. }
  124. bufferedImage.setRGB(j2, j1, rgb);
  125. }
  126. }
  127. g2D.drawImage(bufferedImage, 0, 0, imageIcon.getImageObserver());
  128. byteArrayOutputStream = new ByteArrayOutputStream();
  129. ImageIO.write(bufferedImage, "png", byteArrayOutputStream);// 转换成byte数组
  130. result = byteArrayOutputStream.toByteArray();
  131. } catch (Exception e) {
  132. e.printStackTrace();
  133. } finally {
  134. if (is != null) {
  135. try {
  136. is.close();
  137. } catch (IOException e) {
  138. // TODO Auto-generated catch block
  139. }
  140. }
  141. if (byteArrayOutputStream != null) {
  142. try {
  143. byteArrayOutputStream.close();
  144. } catch (IOException e) {
  145. // TODO Auto-generated catch block
  146. }
  147. }
  148. }
  149. return result;
  150. }
  151. /**
  152. * byte数组到图片
  153. */
  154. public static void byteToImage(byte[] data,String path){
  155. if (data.length < 3 || path.equals("")) return;
  156. try {
  157. FileImageOutputStream imageOutput = new FileImageOutputStream(new File(path));
  158. imageOutput.write(data, 0, data.length);
  159. imageOutput.close();
  160. } catch(Exception ex) {
  161. System.out.println("Exception: " + ex.getMessage());
  162. ex.printStackTrace();
  163. }
  164. }
  165. /**
  166. * 图片转化为byte数组
  167. * @param path
  168. * @return
  169. */
  170. public byte[] imageToByte(String path){
  171. byte[] data = null;
  172. FileImageInputStream input = null;
  173. try {
  174. input = new FileImageInputStream(new File(path));
  175. ByteArrayOutputStream output = new ByteArrayOutputStream();
  176. byte[] buf = new byte[1024];
  177. int numBytesRead = 0;
  178. while ((numBytesRead = input.read(buf)) != -1) {
  179. output.write(buf, 0, numBytesRead);
  180. }
  181. data = output.toByteArray();
  182. output.close();
  183. input.close();
  184. } catch (FileNotFoundException ex1) {
  185. ex1.printStackTrace();
  186. } catch (IOException ex1) {
  187. ex1.printStackTrace();
  188. }
  189. return data;
  190. }
  191. /**
  192. * 图片转base64编码
  193. * 拼接 "data:image/png;base64,"
  194. */
  195. public static String imageToBase64(String path){
  196. byte[] bytes = imageToByte(path);
  197. String base64String = Base64.encodeBase64String(bytes);
  198. return base64String;
  199. }
  200. }

3,图片转换成透明底图片(不推荐,图片会模糊)

  1. package org.jeecg.modules.ppl.utils;
  2. import java.awt.Graphics2D;
  3. import java.awt.image.BufferedImage;
  4. import java.io.*;
  5. import java.net.MalformedURLException;
  6. import java.net.URL;
  7. import javax.imageio.ImageIO;
  8. import javax.swing.ImageIcon;
  9. /**
  10. * 生成透明图片
  11. */
  12. public class PngUtil{
  13. //色差范围0~255
  14. public static int color_range = 20;
  15. public static void opacityPng(String imgPath) throws IOException {
  16. BufferedImage image = ImageIO.read(new File(imgPath));
  17. // 高度和宽度
  18. int height = image.getHeight();
  19. int width = image.getWidth();
  20. // 生产背景透明和内容透明的图片
  21. ImageIcon imageIcon = new ImageIcon(image);
  22. BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_4BYTE_ABGR);
  23. Graphics2D g2D = (Graphics2D) bufferedImage.getGraphics(); // 获取画笔
  24. g2D.drawImage(imageIcon.getImage(), 0, 0, null); // 绘制Image的图片,使用了imageIcon.getImage(),目的就是得到image,直接使用image就可以的
  25. int alpha = 0; // 图片透明度
  26. // 外层遍历是Y轴的像素
  27. for (int y = bufferedImage.getMinY(); y < bufferedImage.getHeight(); y++) {
  28. // 内层遍历是X轴的像素
  29. for (int x = bufferedImage.getMinX(); x < bufferedImage.getWidth(); x++) {
  30. int rgb = bufferedImage.getRGB(x, y);
  31. // 对当前颜色判断是否在指定区间内
  32. if (colorInRange(rgb)){
  33. alpha = 0;
  34. }else{
  35. // 设置为不透明
  36. alpha = 255;
  37. }
  38. // #AARRGGBB 最前两位为透明度
  39. rgb = (alpha << 24) | (rgb & 0x00ffffff);
  40. bufferedImage.setRGB(x, y, rgb);
  41. }
  42. }
  43. // 绘制设置了RGB的新图片,这一步感觉不用也可以只是透明地方的深浅有变化而已,就像蒙了两层的感觉
  44. g2D.drawImage(bufferedImage, 0, 0, null);
  45. // 生成图片为PNG
  46. ImageIO.write(bufferedImage, "png", new File("E:/upload/media/lanzhou1.png"));
  47. }
  48. // 判断是背景还是内容
  49. public static boolean colorInRange(int color) {
  50. int red = (color & 0xff0000) >> 16;// 获取color(RGB)中R位
  51. int green = (color & 0x00ff00) >> 8;// 获取color(RGB)中G位
  52. int blue = (color & 0x0000ff); // 获取color(RGB)中B位
  53. // 通过RGB三分量来判断当前颜色是否在指定的颜色区间内
  54. if (red >= color_range && green >= color_range && blue >= color_range){
  55. return true;
  56. };
  57. return false;
  58. }
  59. /**
  60. * 通过链接url下载图片
  61. * inImageUrl 网络图片url
  62. * putImagePath 保存的本地路径
  63. */
  64. public static String downloadPic(String inImageUrl,String putImagePath) {
  65. URL url = null;
  66. int imageNumber = 0;
  67. try {
  68. url = new URL(inImageUrl);
  69. DataInputStream dataInputStream = new DataInputStream(url.openStream());
  70. putImagePath = "E:/upload/media/lanzhou2.png";
  71. FileOutputStream fileOutputStream = new FileOutputStream(new File(putImagePath));
  72. ByteArrayOutputStream output = new ByteArrayOutputStream();
  73. byte[] buffer = new byte[1024];
  74. int length;
  75. while ((length = dataInputStream.read(buffer)) > 0) {
  76. output.write(buffer, 0, length);
  77. }
  78. byte[] context = output.toByteArray();
  79. fileOutputStream.write(output.toByteArray());
  80. dataInputStream.close();
  81. fileOutputStream.close();
  82. } catch (MalformedURLException e) {
  83. e.printStackTrace();
  84. putImagePath = "错误:" + e.getMessage();
  85. } catch (IOException e) {
  86. e.printStackTrace();
  87. putImagePath = "错误:" + e.getMessage();
  88. }
  89. return putImagePath;
  90. }
  91. }