Package it.hotel.model

Source Code of it.hotel.model.ImageUtils

package it.hotel.model;

import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.File;

import javax.imageio.ImageIO;

public class ImageUtils {

  public static byte[] convertImageToByte (Image image){
    Byte.parseByte(image.toString());
    return null;



  }

  public static String getExtension(String s) {
        String ext = null;
        int i = s.lastIndexOf('.');
        if (i > 0 && i < s.length() - 1) {
            ext = s.substring(i+1).toLowerCase();
        }
        return ext;
}
 
   public static void resize (Image imageReal,int w, int h,String infilePath, String outfilePath){
     int heightActual = imageReal.getHeight(null);
      int widthActual = imageReal.getWidth(null);
      while (heightActual == -1 || widthActual == -1){
        heightActual = imageReal.getHeight(null);
        widthActual = imageReal.getWidth(null)
      }
     
      double scale = computeScale(widthActual, heightActual, widthActual, heightActual);
      int scaledW = (int) (scale * widthActual);
      int scaledH = (int) (scale * heightActual);
     
      BufferedImage image = new BufferedImage(scaledW, scaledH, BufferedImage.TYPE_INT_RGB);
      AffineTransform tx = new AffineTransform();
      tx.scale(scale, scale);
     
      Graphics2D g2d = image.createGraphics();
      g2d.drawImage(imageReal, tx, null);
     
        
    if (h==0)
            scale = ((double)w / (double)image.getWidth());
        else scale = ((double)h / (double)image.getHeight());
        AffineTransformOp op = new
               AffineTransformOp(AffineTransform.getScaleInstance(scale, scale),
               AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
        image = op.filter(image,null);
        try {
     
            ImageIO.write(image, getExtension(infilePath), new File(outfilePath));
        }   catch (Exception ex) {}
    }
 

  


  /**
   * Calcola il rapporto di scala sulla base della dimensione maggiore (tenuto conto
   * del rapporto finale desiderato).
   * Il fattore di scala restituito non sarà comunque superiore ad 1.
   * @param width Dimensione attuale dell'immagine
   * @param height Dimensione attuale dell'immagine
   * @param finalWidth Dimensione finale dell'immagine
   * @param finalHeight Dimensione finale dell'immagine
   * @return Il fattore di scala da applicare all'immagine
   */
  private static double computeScale(int width, int height, int finalWidth, int finalHeight){
    double scale;
    if(((double) width / (double) height) >= ((double) finalWidth / (double) finalHeight)){
      scale = (double) finalWidth / width;
    } else {
      scale = (double) finalHeight / height;
    }
    if(scale > 1) {
      scale = 1;
    }
    return scale;
  }

  private static String getFileExtension(String fileName) {
    String extension = fileName.substring(fileName.lastIndexOf('.')+1).trim();
    return extension;
  }
 
 

  /**
   * @param name
   * @param path
   * @param image
   * @param hotelId
   * @param galleryId
   * @param height
   * @param width
   * @param finalPath
   * @return pathFolderPhoto
   * this method creates folders and image File
   */
  public static String upload (String name, String path, Image image, int hotelId, int galleryId, int height, int width, String finalPath) {


 
    String pathBasePhoto = "resources/images/imagesStruct" + hotelId + "/gallery" + galleryId;
    String pathPhoto = path +
        File.separator + "resources" + File.separator +
        "images" + File.separator + "imagesStruct"
        + hotelId + File.separator +
        "gallery" + galleryId + File.separator + finalPath;
   
   
   
    boolean success = DirectoryUtils.makeDirectory(pathPhoto);
    pathPhoto = pathPhoto + File.separator + name + ".jpeg";
    File file = new File(pathPhoto);

   
   
    ImageUtils.resize (image,width, height, pathPhoto,  pathPhoto);
    return pathBasePhoto;
   





  }
 


}
TOP

Related Classes of it.hotel.model.ImageUtils

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.