Exemplo n.º 1
0
 /**
  * toBufferedImage
  *
  * <p>Transform from an Image instance to a BufferedImage instance
  *
  * @param image Source image
  * @return BufferedImage
  */
 public static BufferedImage toBufferedImage(Image image) {
   return ImageTransform.toBufferedImage(image, BufferedImage.TYPE_INT_ARGB_PRE);
 }
Exemplo n.º 2
0
  /**
   * scaleImage
   *
   * <p>scale original image related to the ImageProperties.xml dimensions
   *
   * @param bufImg Buffered image to scale
   * @param imgHeight Original image height
   * @param imgwidth Original image width
   * @param dimensionMap Image dimensions by size type
   * @param sizeType Size type to scale
   * @return New scaled buffered image
   */
  public static Map<String, Object> scaleImage(
      BufferedImage bufImg,
      double imgHeight,
      double imgWidth,
      Map<String, Map<String, String>> dimensionMap,
      String sizeType,
      Locale locale) {

    /* VARIABLES */
    BufferedImage bufNewImg;
    double defaultHeight, defaultWidth, scaleFactor;
    Map<String, Object> result = FastMap.newInstance();

    /* DIMENSIONS from ImageProperties */
    defaultHeight = Double.parseDouble(dimensionMap.get(sizeType).get("height").toString());
    defaultWidth = Double.parseDouble(dimensionMap.get(sizeType).get("width").toString());
    if (defaultHeight == 0.0 || defaultWidth == 0.0) {
      String errMsg =
          UtilProperties.getMessage(
                  resource, "ImageTransform.one_default_dimension_is_null", locale)
              + " : defaultHeight = "
              + defaultHeight
              + " ; defaultWidth = "
              + defaultWidth;
      Debug.logError(errMsg, module);
      result.put("errorMessage", errMsg);
      return result;
    }

    /* SCALE FACTOR */
    // find the right Scale Factor related to the Image Dimensions
    if (imgHeight > imgWidth) {
      scaleFactor = defaultHeight / imgHeight;
      if (scaleFactor == 0.0) {
        String errMsg =
            UtilProperties.getMessage(
                    resource, "ImageTransform.height_scale_factor_is_null", locale)
                + "  (defaultHeight = "
                + defaultHeight
                + "; imgHeight = "
                + imgHeight;
        Debug.logError(errMsg, module);
        result.put("errorMessage", errMsg);
        return result;
      }
      // get scaleFactor from the smallest width
      if (defaultWidth < (imgWidth * scaleFactor)) {
        scaleFactor = defaultWidth / imgWidth;
      }
    } else {
      scaleFactor = defaultWidth / imgWidth;
      if (scaleFactor == 0.0) {
        String errMsg =
            UtilProperties.getMessage(resource, "ImageTransform.width_scale_factor_is_null", locale)
                + "  (defaultWidth = "
                + defaultWidth
                + "; imgWidth = "
                + imgWidth;
        Debug.logError(errMsg, module);
        result.put("errorMessage", errMsg);
        return result;
      }
      // get scaleFactor from the smallest height
      if (defaultHeight < (imgHeight * scaleFactor)) {
        scaleFactor = defaultHeight / imgHeight;
      }
    }

    if (scaleFactor == 0.0) {
      String errMsg =
          UtilProperties.getMessage(resource, "ImageTransform.final_scale_factor_is_null", locale)
              + " = "
              + scaleFactor;
      Debug.logError(errMsg, module);
      result.put("errorMessage", errMsg);
      return result;
    }
    int bufImgType;
    if (BufferedImage.TYPE_CUSTOM == bufImg.getType()) {
      String errMsg =
          UtilProperties.getMessage(resource, "ImageTransform.unknown_buffered_image_type", locale);
      Debug.logWarning(errMsg, module);
      // apply a type for image majority
      bufImgType = BufferedImage.TYPE_INT_ARGB_PRE;
    } else {
      bufImgType = bufImg.getType();
    }

    // scale original image with new size
    Image newImg =
        bufImg.getScaledInstance(
            (int) (imgWidth * scaleFactor), (int) (imgHeight * scaleFactor), Image.SCALE_SMOOTH);

    bufNewImg = ImageTransform.toBufferedImage(newImg, bufImgType);

    result.put("responseMessage", "success");
    result.put("bufferedImage", bufNewImg);
    result.put("scaleFactor", scaleFactor);
    return result;
  }