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
   | public class ImageUtil {     
 
 
 
 
 
 
      private byte[] getFaceHead(String faceName, byte[] pictureBytes, MediaType contentType) {                  double[] faceDetectResult = this.getFaceDetectResult(faceName, pictureBytes, contentType);         if (faceDetectResult[0] == 0                 && faceDetectResult[1] == 0                 && (faceDetectResult[2] == 0                 || faceDetectResult[3] == 0)) {             log.warn("图片无法裁剪,使用原图片");             return pictureBytes;         }                  try {                          BufferedImage pictureImage = ImageIO.read(new ByteArrayInputStream(pictureBytes));             final int width = pictureImage.getWidth();             final int height = pictureImage.getHeight();                          int x = (int) (faceDetectResult[0] * width);                          x = Math.max((x - 10), 0);             int y = (int) (faceDetectResult[1] * height);                          y = Math.max((y - 10), 0);             int w = (int) (faceDetectResult[2] * width);                          w = Math.min((w + 10), width - x);             int h = (int) (faceDetectResult[3] * height);                          h = Math.min((h + 10), height - y);             BufferedImage headImage = pictureImage.getSubimage(x, y, w, h);             ByteArrayOutputStream out = new ByteArrayOutputStream();             ImageIO.write(headImage, "jpg", out);             return out.toByteArray();         } catch (IOException e) {             log.warn("裁剪图片失败使用原照片 {}", e.getMessage());             throw new RuntimeException("裁剪图片失败");         }     } }
  |