2012-12-19 95 views
1

我想調整圖片大小並保存,但是我保存的圖片沒有調整大小。Java SWT圖像大小調整不起作用

這是我正在嘗試使用的代碼。

if(CC_Files.fileExists(path)){ 
       if(path.contains(".jpg") || path.contains(".png") || path.contains(".gif")){ 
       Image image = (Image) SWTResourceManager 
         .getImage(path); 
       ImageData imgData = image.getImageData(); 
       imgData.scaledTo(150, 150); 
       ImageLoader imageLoader = new ImageLoader(); 
       imageLoader.data = new ImageData[] {imgData}; 
       imageLoader.save(Variables.getStrResources() + "\\Pics\\" + a.getHerd_id() + ".jpg",SWT.IMAGE_JPEG); 
    }  
} 
+0

我希望我的回答是useful..thanks。 。 –

回答

2

你的問題是,你不讀其中wrriten

ImageData#scaledTo(int width, int height) - Returns a copy of the receiver which has been stretched or shrunk to the specified size.

的JavaDoc因此,解決辦法是:

imgData = imgData.scaledTo(150, 150);

Documentation

+0

Works Perfect Now謝謝 – Talon06

+0

不要忘記處理你的未使用的圖像處理! –

1

爪哇SWT圖像的大小調整爲適當的工作

ImageLoader的類被用於從加載的圖像,和圖像保存到,一個文件或流

imageLoader.save(result, SWT.IMAGE_COPY) 

的FileDialog類允許用戶導航文件系統並選擇或輸入文件名。

Button btnOpen = new Button(parent, SWT.NONE); 
btnOpen.setBounds(200, 55, 68, 23); 
btnOpen.addSelectionListener(new SelectionAdapter() { 
    @Override 
    public void widgetSelected(SelectionEvent e) { 

      FileDialog dialog = new FileDialog(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), SWT.OPEN); 
      String result = dialog.open(); 

      if(result!=null) 
      { 
       Image image=SWTResourceManager.getImage(result); 
       //ImageData class are device-independent descriptions of images 
       ImageData imgData = image.getImageData(); 
       imgData=imgData.scaledTo(200, 200); 

       ImageLoader imageLoader = new ImageLoader(); 
       imageLoader.data = new ImageData[] {imgData}; 
       imageLoader.save(result, SWT.IMAGE_COPY); 

       System.out.println("Width: "+imgData.width+".....Height: "+imgData.height); 
       lbl_image_text.setBounds(25,88,imgData.width+10,imgData.height+10); 
       lbl_image_text.setImage(SWTResourceManager.getImage(result)); 
      } 
    } 
}); 
btnOpen.setText("open"); 
CLabel lbl_image_text = new CLabel(parent, SWT.Resize); 

圖像大小設置爲標籤動態

Button btnOpen = new Button(parent, SWT.NONE); 
btnOpen.setBounds(200, 55, 68, 23); 
btnOpen.addSelectionListener(new SelectionAdapter() { 
    @Override 
    public void widgetSelected(SelectionEvent e) { 

      FileDialog dialog = new FileDialog(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), SWT.OPEN); 
      String result = dialog.open(); 

      if(result!=null) 
      { 

       Image image=SWTResourceManager.getImage(result); 
       //get Image width and height 
       lbl_image_text.setBounds(25,88,image.getBounds().width+10,image.getBounds().height+10); 
       lbl_image_text.setImage(SWTResourceManager.getImage(result)); 
      } 
    } 
}); 
btnOpen.setText("open"); 
CLabel lbl_image_text = new CLabel(parent, SWT.Resize); 
0

呼叫從SWT.addListener這種方法(SWT.Close,新CustomShellCloseListener())。傳遞所需的塔參數LabelImage(不要使用Label.getImage(),通過直接路徑),label.getBounds.width,label.getBounds.height

protected Image resize(Image imageFromSource, int width, int height) { 
    if(width>0 && height>0){ 
     Image scaledImage = new Image(shellCCMPFMatrixBomCompare.getDisplay(), width, height); 
     GC gc = new GC(scaledImage);   //Graphics Capabilities(GC instance) in SWT used to draw an Image, graphics, display 
     gc.setAntialias(SWT.ON);  // Anti aliasing is used for making the low resolution image to redraw and make into a good resolution Image 
     gc.setInterpolation(SWT.HIGH);  //Interpolation is based in the Graphics, it may not work properly in some systems 
     gc.drawImage(imageFromSource, 0, 0, 
       imageFromSource.getBounds().width, imageFromSource.getBounds().height, 
       0, 0, width, height);  

     /*drawImage(Image image, int srcX, int srcY, int srcWidth, int srcHeight, int destX, int destY, int destWidth, int destHeight) 
     Copies a rectangular area from the source image into a (potentially different sized) rectangular area in the receiver.*/ 

     gc.dispose(); 
     return scaledImage; 
     } 
     else return imageFromSource; 
}