核查系统api
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

69 lines
2.0 KiB

2 years ago
  1. package com.iflytop.nuclear.utils;
  2. import javax.imageio.ImageIO;
  3. import java.awt.image.BufferedImage;
  4. import java.io.*;
  5. import java.util.Base64;
  6. /**
  7. * @author cool
  8. * @date 2023/7/10 15:53
  9. */
  10. public class ImageUtils {
  11. /**
  12. * 图片转Base64码
  13. * @param src
  14. * @return
  15. */
  16. public static String convertImageToBase64Str(String src) {
  17. ByteArrayOutputStream baos = null;
  18. try {
  19. String suffix = src.substring(src.lastIndexOf(".") + 1);
  20. File imageFile = new File(src);
  21. BufferedImage bufferedImage = ImageIO.read(imageFile);
  22. baos = new ByteArrayOutputStream();
  23. ImageIO.write(bufferedImage, suffix, baos);
  24. byte[] bytes = baos.toByteArray();
  25. return Base64.getEncoder().encodeToString(bytes);
  26. } catch (Exception e) {
  27. e.printStackTrace();
  28. } finally {
  29. try {
  30. if (baos != null) {
  31. baos.close();
  32. }
  33. } catch (IOException e) {
  34. e.printStackTrace();
  35. }
  36. }
  37. return null;
  38. }
  39. /**
  40. * Base64码转图片
  41. * @param base64String
  42. * @param newSrc
  43. */
  44. public static void convertBase64StrToImage(String base64String, String newSrc) {
  45. ByteArrayInputStream bais = null;
  46. try {
  47. String suffix = newSrc.substring(newSrc.lastIndexOf(".") + 1);
  48. byte[] bytes = Base64.getDecoder().decode(base64String);
  49. bais = new ByteArrayInputStream(bytes);
  50. BufferedImage bufferedImage = ImageIO.read(bais);
  51. File imageFile = new File(newSrc);
  52. ImageIO.write(bufferedImage, suffix, imageFile);
  53. } catch (Exception e) {
  54. e.printStackTrace();
  55. } finally {
  56. try {
  57. if (bais != null) {
  58. bais.close();
  59. }
  60. } catch (IOException e) {
  61. e.printStackTrace();
  62. }
  63. }
  64. }
  65. }