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 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
| package classes;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
/**
*
* @author pj
*/
public class Img {
BufferedImage source;
BufferedImage destination;
File sourceFile;
File destinationFile;
int width;
int height;
String extension;
public boolean resizeWidth(File sourceFile, File destinationFile, int width) {
try {
this.sourceFile = sourceFile;
this.destinationFile=destinationFile;
this.extension=(sourceFile.getName().substring(sourceFile.getName().lastIndexOf("."))).toLowerCase();
this.extension=this.extension.substring(1);
this.source = ImageIO.read(this.sourceFile);
this.width = width;
int height0 = this.source.getHeight();
int width0 = this.source.getWidth();
this.height = (int) (((double)height0*(double)this.width)/((double)width0));
this.destination = new BufferedImage(this.width, this.height, BufferedImage.TYPE_INT_ARGB);
Graphics2D g = this.destination.createGraphics();
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g.drawImage(this.source, 0, 0, this.width, this.height, null);
g.dispose();
ImageIO.write(this.destination, this.extension, this.destinationFile);
return true;
} catch (IOException ex) {
Logger.getLogger(Img.class.getName()).log(Level.SEVERE, null, ex);
return false;
}
}
public boolean resizeHeight(File sourceFile, File destinationFile, int height) {
try {
this.sourceFile = sourceFile;
this.destinationFile=destinationFile;
this.extension=(sourceFile.getName().substring(sourceFile.getName().lastIndexOf("."))).toLowerCase();
this.extension=this.extension.substring(1);
this.source = ImageIO.read(this.sourceFile);
this.height = height;
int height0 = this.source.getHeight();
int width0 = this.source.getWidth();
this.width = (int) (((double)width0*(double)this.height)/((double)height0));
this.destination = new BufferedImage(this.width, this.height, BufferedImage.TYPE_INT_ARGB);
Graphics2D g = this.destination.createGraphics();
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g.drawImage(this.source, 0, 0, this.width, this.height, null);
g.dispose();
ImageIO.write(this.destination, this.extension, this.destinationFile);
return true;
} catch (IOException ex) {
Logger.getLogger(Img.class.getName()).log(Level.SEVERE, null, ex);
return false;
}
}
public boolean getSize(File sourceFile) {
try {
this.sourceFile = sourceFile;
this.source = ImageIO.read(this.sourceFile);
this.width=this.source.getWidth();
this.height=this.source.getHeight();
return true;
} catch (IOException ex) {
Logger.getLogger(Img.class.getName()).log(Level.SEVERE, null, ex);
return false;
}
}
} |
Partager