我正在尝试调整图片大小并保存,但是我保存的图片没有调整大小。
以下是我正在尝试使用的代码。
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 :(得分:2)
你的问题是你没有读过JavaDoc在哪里是wrriten
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);
答案 1 :(得分: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);
答案 2 :(得分: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;
}