Coverage Report - net.sf.webphotos.util.legacy.Photo
 
Classes in this File Line Coverage Branch Coverage Complexity
Photo
0%
0/31
0%
0/2
3
 
 1  
 /**
 2  
  * Copyright 2008 WebPhotos
 3  
  *
 4  
  * Licensed under the Apache License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  *      http://www.apache.org/licenses/LICENSE-2.0
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  */
 16  
 package net.sf.webphotos.util.legacy;
 17  
 
 18  
 import com.sun.image.codec.jpeg.*;
 19  
 import java.awt.*;
 20  
 import java.awt.image.*;
 21  
 import java.io.*;
 22  
 import javax.swing.ImageIcon;
 23  
 
 24  
 /**
 25  
  * Responsável pelas funções relacionadas a alterações de fotos. Armazena um
 26  
  * método para reajustar o tamanho da foto.
 27  
  *
 28  
  * @author Guilherme
 29  
  */
 30  0
 public class Photo {
 31  
 
 32  
     /**
 33  
      * Redimensiona o tamanho da imagem, alterando o arquivo.
 34  
      *
 35  
      * @param original Nome do arquivo original.
 36  
      * @param resized Nome do arquivo redimensionado.
 37  
      * @param maxSize Tamanho máximo.
 38  
      */
 39  
     public static void resize(String original, String resized, int maxSize) {
 40  
         try {
 41  0
             File originalFile = new File(original);
 42  0
             ImageIcon ii = new ImageIcon(originalFile.getCanonicalPath());
 43  0
             Image i = ii.getImage();
 44  
             Image resizedImage;
 45  0
             int iWidth = i.getWidth(null);
 46  0
             int iHeight = i.getHeight(null);
 47  0
             if (iWidth > iHeight) {
 48  0
                 resizedImage = i.getScaledInstance(maxSize, (maxSize * iHeight) / iWidth, Image.SCALE_SMOOTH);
 49  
             } else {
 50  0
                 resizedImage = i.getScaledInstance((maxSize * iWidth) / iHeight, maxSize, Image.SCALE_SMOOTH);
 51  
             }
 52  
             // This code ensures that all the
 53  
             // pixels in the image are loaded.
 54  0
             Image temp = new ImageIcon(resizedImage).getImage();
 55  
 
 56  
             // Create the buffered image.
 57  0
             BufferedImage bufferedImage = new BufferedImage(temp.getWidth(null), temp.getHeight(null), BufferedImage.TYPE_INT_RGB);
 58  
 
 59  
             // Copy image to buffered image.
 60  0
             Graphics g = bufferedImage.createGraphics();
 61  
             // Clear background and paint the image.
 62  0
             g.setColor(Color.white);
 63  0
             g.fillRect(0, 0, temp.getWidth(null), temp.getHeight(null));
 64  0
             g.drawImage(temp, 0, 0, null);
 65  0
             g.dispose();
 66  
 
 67  
             // sharpen
 68  0
             float[] sharpenArray = {0, -1, 0, -1, 5, -1, 0, -1, 0};
 69  0
             Kernel kernel = new Kernel(3, 3, sharpenArray);
 70  0
             ConvolveOp cOp = new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP, null);
 71  0
             bufferedImage = cOp.filter(bufferedImage, null);
 72  
 
 73  
             /* write the jpeg to a file */
 74  0
             File file = new File(resized);
 75  0
             FileOutputStream out = new FileOutputStream(file);
 76  
 
 77  
             /* encodes image as a JPEG data stream */
 78  0
             JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
 79  0
             com.sun.image.codec.jpeg.JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bufferedImage);
 80  
 
 81  
             // writeParam = new JPEGImageWriteParam(null);
 82  
             // writeParam.setCompressionMode(JPEGImageWriteParam.MODE_EXPLICIT);
 83  
             // writeParam.setProgressiveMode(JPEGImageWriteParam.MODE_DEFAULT);
 84  
 
 85  0
             param.setQuality(0.7f, true);
 86  0
             encoder.setJPEGEncodeParam(param);
 87  0
             encoder.encode(bufferedImage);
 88  
 
 89  0
         } catch (Exception e) {
 90  0
             System.out.println(e.getMessage());
 91  0
         }
 92  
 
 93  0
     }
 94  
 }