Пример #1
0
  /** crops image to square */
  public void cropImageToSquare() {
    int width = image.getWidth();
    int height = image.getHeight();

    // possibly crop
    if (width > height) image = Scalr.crop(image, (width - height) / 2, 0, height, height);
    else if (width < height) image = Scalr.crop(image, 0, (height - width) / 2, width, width);
  }
Пример #2
0
 public static void crop(File file, int x, int y, int width, int height)
     throws FileOperationException {
   BufferedImage image;
   try {
     image = ImageIO.read(file);
     image = Scalr.crop(image, x, y, width, height);
     saveToJPG(image, file);
     image.flush();
   } catch (IOException | IllegalArgumentException e) {
     logger.error(e.getMessage(), e);
     throw new FileOperationException("Cropping failed");
   }
 }
Пример #3
0
  public void createThumbnail(MultipartFile multipartFile, String fileName, int thumbnailSize)
      throws DdException, IOException {
    boolean f = false;
    for (String mime : mimeTypeAllow) {
      if (mime.equalsIgnoreCase(multipartFile.getContentType())) {
        f = true;
        break;
      }
    }

    if (!f) {
      throw new DdException(
          DdException.VALIDATION_EXCEPTION,
          UploadUtil.FILE_TYPE_NOT_ALLOW_CODE,
          UploadUtil.FILE_TYPE_NOT_ALLOW);
    }

    String fileNameTemp = "", fileExt = "";
    fileNameTemp = multipartFile.getOriginalFilename();
    if (fileNameTemp.indexOf(".") > -1) {
      fileExt = fileNameTemp.substring(fileNameTemp.lastIndexOf("."), fileNameTemp.length());
    }
    fileExt = fileExt.toLowerCase();

    // Check for valid file type upload file
    if (fileTypeAllow.indexOf(fileExt) == -1) {
      throw new DdException(
          DdException.VALIDATION_EXCEPTION,
          UploadUtil.FILE_TYPE_NOT_ALLOW_CODE,
          UploadUtil.FILE_TYPE_NOT_ALLOW);
    }

    // Check for limit upload file
    if (multipartFile.getSize() <= 0 || multipartFile.getSize() > fileSizeLimit) {
      throw new DdException(
          DdException.VALIDATION_EXCEPTION,
          UploadUtil.FILE_SIZE_LIMIT_CODE,
          UploadUtil.FILE_SIZE_LIMIT);
    }

    BufferedImage img = ImageIO.read(multipartFile.getInputStream());

    int cropMargin = (int) Math.abs(Math.round(((img.getWidth() - img.getHeight()) / 2.0)));

    // determine the crop coordinates
    int x1 = 0;
    int y1 = 0;
    int width = img.getWidth();
    int height = img.getHeight();
    if (img.getWidth() > img.getHeight()) {
      x1 = cropMargin;
      width = height;
    } else {
      y1 = cropMargin;
      height = width;
    }

    BufferedImage thumbnail = Scalr.crop(img, x1, y1, width, height);

    thumbnail =
        Scalr.resize(
            thumbnail,
            Scalr.Method.ULTRA_QUALITY,
            Scalr.Mode.AUTOMATIC,
            thumbnailSize,
            thumbnailSize,
            Scalr.OP_ANTIALIAS);

    ImageIO.write(thumbnail, fileExt.substring(1), new File(path + fileName));
  }