public class ResizeImage {
private static Image cLabelImagePaintControl(Image oldImage, Point size) {
Image newImage = null;
Rectangle bounds = oldImage.getBounds();
int MAX_WIDTH = size.x;// TODO: 缩放后的图片最大宽度
int MAX_HEIGHT = size.y;// TODO: 缩放后的图片最大高度
int imageWidth = bounds.width;
int imageHeight = bounds.height;
// determine thumbnail size from MAX_WIDTH and MAX_HEIGHT
int thumbWidth = MAX_WIDTH;
int thumbHeight = MAX_HEIGHT;
double thumbRatio = (double) thumbWidth / (double) thumbHeight;
double imageRatio = (double) imageWidth / (double) imageHeight;
if (thumbRatio < imageRatio) {
thumbHeight = (int) (thumbWidth / imageRatio);
} else {
thumbWidth = (int) (thumbHeight * imageRatio);
}
// 如果图片小于所略图大小, 不作处理
if (imageWidth < MAX_WIDTH && imageHeight < MAX_HEIGHT) {
thumbWidth = imageWidth;
thumbHeight = imageHeight;
}
//creat scaled data
byte[] outdata = new byte[0];
ByteArrayOutputStream out = new ByteArrayOutputStream();
ImageLoader imageLoader = new ImageLoader();
imageLoader.data = new ImageData[]{oldImage.getImageData()};
imageLoader.save(out, SWT.IMAGE_JPEG);
try {
outdata = out.toByteArray();
out.flush();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}finally{
try {
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
ByteArrayInputStream in = new ByteArrayInputStream(outdata);
java.awt.Image newImageTemp = null;
try {
newImageTemp = javax.imageio.ImageIO.read(in);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
BufferedImage tag= new BufferedImage((int) thumbWidth, (int) thumbHeight, BufferedImage.TYPE_INT_RGB);
tag.getGraphics().drawImage(newImageTemp.getScaledInstance(thumbWidth, thumbHeight, java.awt.Image.SCALE_SMOOTH), 0, 0, null);
//out scaled data
try {
byte[] outdatascale = new byte[0];
ByteArrayOutputStream outscale = new ByteArrayOutputStream();
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(outscale);
encoder.encode(tag);
try {
outdatascale = outscale.toByteArray();
outscale.flush();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}finally{
try {
outscale.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
ByteArrayInputStream inscale = new ByteArrayInputStream(outdatascale);
newImage = new Image(Display.getDefault(), inscale);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return newImage;
}
public static void main(String[] args){
ImageLoader loader1 = new ImageLoader();
loader1.load("d://shell.JPG");
Image old = new Image(Display.getDefault(), loader1.data[0]);
Image news = cLabelImagePaintControl(old,new Point(100,100));
if(null != news){
ImageLoader loader2 = new ImageLoader();
loader2.data = new ImageData[]{news.getImageData()};
loader2.save("d://223.jpg", SWT.IMAGE_JPEG);
}
}
}