1

我正在尝试调整图片大小并保存它,但是我保存的图片没有调整大小。

这是我尝试使用的代码。

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

3 回答 3

2

您的问题是您没有阅读所写的 JavaDoc

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

所以解决方案是:

imgData = imgData.scaledTo(150, 150);

文档

于 2012-12-19T21:12:17.180 回答
1

Java SWT Image Resize 正常工作

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);
于 2017-02-02T13:35:16.290 回答
0

从 SWT.addListener(SWT.Close, new CustomShellCloseListener()) 调用此方法。需要传递的 Tha 参数是 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;
}
于 2017-05-12T04:19:12.430 回答