1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
@Slf4j public class ImageUtilJnb { private static int mb = 1048576;// 1MB // 791202.com public static void main(String[] args) { String fullPath="D:\\123.jpg"; String newPath="D:\\321322198511205873F.jpg"; long length = new File(fullPath).length(); if (length / 1048576 >= JnbConstants.JNB_IMAGE_RATE) {// 如果图片大于等于0.5MB 则按80%缩小 try { ImageUtilJnb.zoom(fullPath, newPath, length, JnbConstants.JNB_IMAGE_RATE); } catch (IOException e) { log.error("出错啦:" + e); } } String base64Str = Base64Util.getBase64Str(newPath); System.out.println("base64Str===" + base64Str); } // 缩图 public static void zoom(String oldFile, String zoomFile,long length,double newRate) throws IOException { DecimalFormat df = new DecimalFormat("0.00");// 设置保留位数 double rate=0.8; log.info("原始图片大小:" + df.format((float) length / mb) + "MB"); long newfile = new File(oldFile).length(); int i = 1; // 如果首次压缩还大于2MB则继续处理 while ((float) newfile / mb >= newRate) { log.info("压缩后图片大小:" + newfile); rate = rate - 0.05;// 暂定按照0.03频率压缩 log.info(i + " rate=" + rate); BufferedImage srcImage = ImageIO.read(new File(oldFile)); int WIDTH = (int) (srcImage.getWidth() * rate); int HEIGHT = (int) (srcImage.getHeight() * rate); BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB); Graphics g = image.getGraphics(); g.drawImage(srcImage, 0, 0, WIDTH, HEIGHT, null); // 缩小 ImageIO.write(image, "jpg", new File(zoomFile)); i++; newfile = new File(zoomFile).length(); log.info("压缩次数:" + i); } // 调整方向 BufferedImage newImage = ImageIO.read(new File(zoomFile)); BufferedImage image1 = Rotate(newImage, 90);// 顺时针旋转90度 ImageIO.write(image1, "jpg", new File(zoomFile)); log.info("处理后图片路径:" + zoomFile + ";缩小之后大小:" + df.format((float) new File(zoomFile).length() / mb) + "MB"); } /** * 对图片进行旋转 * * @param src * 被旋转图片 * @param angel * 旋转角度 * @return 旋转后的图片 */ public static BufferedImage Rotate(Image src, int angel) { int src_width = src.getWidth(null); int src_height = src.getHeight(null); // 计算旋转后图片的尺寸 Rectangle rect_des = CalcRotatedSize(new Rectangle(new Dimension(src_width, src_height)), angel); BufferedImage res = null; res = new BufferedImage(rect_des.width, rect_des.height, BufferedImage.TYPE_INT_RGB); Graphics2D g2 = res.createGraphics(); // 进行转换 g2.translate((rect_des.width - src_width) / 2, (rect_des.height - src_height) / 2); g2.rotate(Math.toRadians(angel), src_width / 2, src_height / 2); g2.drawImage(src, null, null); return res; } /** * 计算旋转后的图片 * * @param src * 被旋转的图片 * @param angel * 旋转角度 * @return 旋转后的图片 */ public static Rectangle CalcRotatedSize(Rectangle src, int angel) { // 如果旋转的角度大于90度做相应的转换 if (angel >= 90) { if (angel / 90 % 2 == 1) { int temp = src.height; src.height = src.width; src.width = temp; } angel = angel % 90; } double r = Math.sqrt(src.height * src.height + src.width * src.width) / 2; double len = 2 * Math.sin(Math.toRadians(angel) / 2) * r; double angel_alpha = (Math.PI - Math.toRadians(angel)) / 2; double angel_dalta_width = Math.atan((double) src.height / src.width); double angel_dalta_height = Math.atan((double) src.width / src.height); int len_dalta_width = (int) (len * Math.cos(Math.PI - angel_alpha - angel_dalta_width)); int len_dalta_height = (int) (len * Math.cos(Math.PI - angel_alpha - angel_dalta_height)); int des_width = src.width + len_dalta_width * 2; int des_height = src.height + len_dalta_height * 2; return new Rectangle(new Dimension(des_width, des_height)); } } |
0