SWT IMAGE的无缝缩略图实现

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);
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值