Skip to content

Commit

Permalink
Thumbnailer Change
Browse files Browse the repository at this point in the history
  • Loading branch information
kenu committed Aug 20, 2014
1 parent 662a72e commit e54b5de
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 144 deletions.
3 changes: 1 addition & 2 deletions src/main/java/kr/pe/okjsp/member/ProfileHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response)
Thread.sleep(1000);
} catch (InterruptedException e1) {
}
boolean delete = file.delete();
System.out.println("del:"+delete);
file.delete();
// DB 기록
try {
storeProfileInfo(sid);
Expand Down
200 changes: 58 additions & 142 deletions src/main/java/kr/pe/okjsp/util/Thumbnailer.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,167 +13,83 @@
import java.awt.RenderingHints;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.FileOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URL;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

public class Thumbnailer {
private Image inImage;
private Image inImage;
private String thumb;
private int maxX;
private int maxY;
private int margin;

/**
* Method Thumbnailer.
* @param orig
* @param thumb
* @param xx
* @param yy
*/
// 생성자
public Thumbnailer(String orig, String thumb, int xx, int yy) {
initLocal(orig, thumb, xx, yy, 0);
}

/**
* Method Thumbnailer.
* @param remotePath
* @param thumb
* @param xx
* @param yy
*/
// 원격이미지 생성자
public Thumbnailer(URL remotePath, String thumb, int xx, int yy) {
initRemote(remotePath, thumb, xx, yy, 0);
}
private int maxY;
private int margin;

/**
* Method Thumbnailer.
*
* @param orig
* @param thumb
* @param xx
* @param yy
* @param margin
*/
// 생성자
public Thumbnailer(String orig, String thumb, int xx, int yy, int margin) {
initLocal(orig, thumb, xx, yy, margin);
}

/**
* Method Thumbnailer.
* @param remotePath
* @param thumb
* @param xx
* @param yy
* @param margin
*/
// 원격이미지 생성자
public Thumbnailer(URL remotePath, String thumb, int xx, int yy, int margin) {
initRemote(remotePath, thumb, xx, yy, margin);
}

/**
* Method initLocal.
* @param orig
* @param thumb
* @param xx
* @param yy
* @param margin
*/
private void initLocal(String orig, String thumb, int xx, int yy, int margin) {
this.inImage = new ImageIcon(orig).getImage();
this.thumb = thumb;
this.maxX = xx;
this.maxY = yy;
this.margin = margin;
}

/**
* Method initRemote.
* @param remotePath
* @param thumb
* @param xx
* @param yy
* @param margin
*/
private void initRemote(URL remotePath, String thumb, int xx, int yy, int margin) {
this.inImage = new ImageIcon(remotePath).getImage();
this.thumb = thumb;
this.maxX = xx;
this.maxY = yy;
this.margin = margin;
}
// 생성자
public Thumbnailer(String orig, String thumb, int xx, int yy) {
this.inImage = new ImageIcon(orig).getImage();
this.thumb = thumb;
this.maxX = xx;
this.maxY = yy;
this.margin = 0;
}

// 작은 이미지를 만든다.
public void createThumbnail() {
// Determine the scale.
double scaleX = (double) (maxX - margin * 2) / inImage.getWidth(null);
double scaleY = (double) (maxY - margin * 2) / inImage.getHeight(null);
double scale = scaleX;
if (scaleX > scaleY) {
scale = scaleY;
}

// Determine size of new image.
// One of them should equal maxDim.
int scaledW = (int) (scale * inImage.getWidth(null));
int scaledH = (int) (scale * inImage.getHeight(null));

// Create an image buffer in which to paint on.
BufferedImage outImage = new BufferedImage(maxX, maxY,
BufferedImage.TYPE_INT_RGB);

// Set the scale.
AffineTransform tx = new AffineTransform();

// If the image is smaller than the desired image size,
// don't bother scaling.
tx.scale(scale, scale);
AffineTransform toCenterAt = new AffineTransform();
int startx = (maxX - scaledW) / 2;
int starty = (maxY - scaledH) / 2;
toCenterAt.translate(startx, starty);
toCenterAt.concatenate(tx);

// Paint image.
Graphics2D g2d = outImage.createGraphics();
RenderingHints qualityHints = new RenderingHints(
RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
qualityHints.put(RenderingHints.KEY_RENDERING,
RenderingHints.VALUE_RENDER_QUALITY);
g2d.setRenderingHints(qualityHints);
g2d.fillRect(0, 0, maxX, maxY);
g2d.drawImage(inImage, toCenterAt, null);

g2d.dispose();

// JPEG-encode the image and write to file.
try {
// Determine the scale.
double scaleX = (double) (maxX - margin * 2) / inImage.getWidth(null);
double scaleY = (double) (maxY - margin * 2) / inImage.getHeight(null);
double scale = scaleX;
if (scaleX > scaleY) {
scale = scaleY;
}

// Determine size of new image.
//One of them
// should equal maxDim.
int scaledW = (int) (scale * inImage.getWidth(null));
int scaledH = (int) (scale * inImage.getHeight(null));

// Create an image buffer in
//which to paint on.
BufferedImage outImage =
new BufferedImage(maxX, maxY, BufferedImage.TYPE_INT_RGB);

// Set the scale.
AffineTransform tx = new AffineTransform();

// If the image is smaller than
//the desired image size,
// don't bother scaling.
// if (scale < 1.0d) {
tx.scale(scale, scale);
// }
AffineTransform toCenterAt = new AffineTransform();
int startx = (maxX - scaledW) / 2 ;
int starty = (maxY - scaledH) / 2 ;
toCenterAt.translate(startx, starty);
toCenterAt.concatenate(tx);

// Paint image.
Graphics2D g2d = outImage.createGraphics();
RenderingHints qualityHints;
qualityHints = new
RenderingHints(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
qualityHints.put(RenderingHints.KEY_RENDERING,
RenderingHints.VALUE_RENDER_QUALITY);
g2d.setRenderingHints(qualityHints);
g2d.fillRect(0,0,maxX,maxY);
g2d.drawImage(inImage, toCenterAt, null);

g2d.dispose();

// JPEG-encode the image
//and write to file.
OutputStream os = new FileOutputStream(thumb);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(os);
JPEGEncodeParam param =
encoder.getDefaultJPEGEncodeParam(outImage);
float quality = 80/100.0f;
param.setQuality(quality, false);
encoder.encode(outImage);
os.close();
ImageIO.write(outImage, "jpg", new File(thumb));
} catch (IOException e) {
System.out.println(e);
}
Expand Down

0 comments on commit e54b5de

Please sign in to comment.