| | |
| | | import java.io.*; |
| | | import java.text.SimpleDateFormat; |
| | | import java.util.Date; |
| | | import java.util.HashMap; |
| | | import java.util.Map; |
| | | |
| | | import static java.time.LocalDate.now; |
| | | |
| | | /** |
| | | * 二进制原始数据转换为图片工具类 |
| | | * base64原始数据转换为图片工具类 |
| | | */ |
| | | public class ImageUtil { |
| | | static BASE64Encoder encoder = new sun.misc.BASE64Encoder(); |
| | |
| | | private static String PRF_MINIO_URL = "minio/jfpt/animalHeat"; |
| | | |
| | | /** |
| | | * 二进制原始数据转换为图片 |
| | | * base64原始数据转换为图片 |
| | | * @param base64String |
| | | */ |
| | | public static String base64StringToImage(String base64String){ |
| | | try { |
| | | byte[] bytes1 = decoder.decodeBuffer(base64String); |
| | | ByteArrayInputStream bais = new ByteArrayInputStream(bytes1); |
| | | BufferedImage bi1 = ImageIO.read(bais); |
| | | byte[] bytes = decoder.decodeBuffer(base64String); |
| | | ByteArrayInputStream bais = new ByteArrayInputStream(bytes); |
| | | BufferedImage bi = ImageIO.read(bais); |
| | | String UUID = DateUtil.today()+StringPool.SLASH+ StringUtil.randomUUID(); |
| | | //可以是jpg,png,gif格式 |
| | | File w2 = new File("D://"+PRE_URL+StringPool.SLASH+ UUID+".jpg"); |
| | |
| | | parentFile.mkdirs(); |
| | | } |
| | | //不管输出什么格式图片,此处不需改动 |
| | | ImageIO.write(bi1, "jpg", w2); |
| | | ImageIO.write(bi, "jpg", w2); |
| | | //返回图片路径 |
| | | return PRF_MINIO_URL+ StringPool.SLASH+ UUID +StringPool.DOT+ "jpg"; |
| | | } catch (IOException e) { |
| | |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 获取base64原始数据图片的长宽 |
| | | * @param base64String |
| | | */ |
| | | public static Map<String,Object> getBase64ImageWidthAndHeight(String base64String){ |
| | | Map<String, Object> map = new HashMap<>(); |
| | | try { |
| | | //将base64字符串转换成二进制字节数组 |
| | | byte[] bytes = decoder.decodeBuffer(base64String); |
| | | //二进制输入流 |
| | | ByteArrayInputStream bais = new ByteArrayInputStream(bytes); |
| | | //io读取图片输入流 |
| | | BufferedImage bi = ImageIO.read(bais); |
| | | //封装数据 |
| | | map.put("imgWidth",bi.getWidth()); |
| | | map.put("imgHeight",bi.getHeight()); |
| | | //返回 |
| | | return map; |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | return null; |
| | | } |
| | | } |