我的应用程序需要调整
ImageData的大小.不幸的是,我没有得到我想要的GC结果(带有抗锯齿和高插值)或ImageData.scaledTo().得到的图像质量太低而无法接受.什么是高质量ImageData调整大小的最佳方法?
编辑:我缩小了.
解决方法
AWT提供不同模式的图像缩放.对于缩小尺寸,Area Averaging提供最佳质量.当然,人们可以重新实现SWT的区域平均缩放算法,或者,对于通常足够的快速解决方案:
>将SWT图像转换为AWT图像
>使用适当的模式重新缩放它(如果缩小则平均区域,否则为双线性)
>将其转换回SWT图像
/** * Resizes an image,using the given scaling factor. Constructs a new image resource,please take care of resource * disposal if you no longer need the original one. This method is optimized for quality,not for speed. * * @param image source image * @param scale scale factor (<1 = downscaling,>1 = upscaling) * @return scaled image */ public static org.eclipse.swt.graphics.Image resize (org.eclipse.swt.graphics.Image image,float scale) { int w = image.getBounds().width; int h = image.getBounds().height; // convert to buffered image BufferedImage img = convertToAWT(image.getImageData()); // resize buffered image int newWidth = Math.round(scale * w); int newHeight = Math.round(scale * h); // determine scaling mode for best result: if downsizing,use area averaging,if upsizing,use smooth scaling // (usually bilinear). int mode = scale < 1 ? BufferedImage.SCALE_AREA_AVERAGING : BufferedImage.SCALE_SMOOTH; java.awt.Image scaledImage = img.getScaledInstance(newWidth,newHeight,mode); // convert the scaled image back to a buffered image img = new BufferedImage(newWidth,BufferedImage.TYPE_INT_RGB); img.getGraphics().drawImage(scaledImage,null); // reconstruct swt image ImageData imageData = convertToSWT(img); return new org.eclipse.swt.graphics.Image(Display.getDefault(),imageData); } public static BufferedImage convertToAWT (ImageData data) { ColorModel colorModel = null; PaletteData palette = data.palette; if (palette.isDirect) { colorModel = new DirectColorModel(data.depth,palette.redMask,palette.greenMask,palette.blueMask); BufferedImage bufferedImage = new BufferedImage(colorModel,colorModel.createCompatibleWritableRaster(data.width,data.height),false,null); WritableRaster raster = bufferedImage.getRaster(); int[] pixelArray = new int[3]; for (int y = 0; y < data.height; y++) { for (int x = 0; x < data.width; x++) { int pixel = data.getPixel(x,y); RGB rgb = palette.getRGB(pixel); pixelArray[0] = rgb.red; pixelArray[1] = rgb.green; pixelArray[2] = rgb.blue; raster.setPixels(x,y,1,pixelArray); } } return bufferedImage; } else { RGB[] rgbs = palette.getRGBs(); byte[] red = new byte[rgbs.length]; byte[] green = new byte[rgbs.length]; byte[] blue = new byte[rgbs.length]; for (int i = 0; i < rgbs.length; i++) { RGB rgb = rgbs[i]; red[i] = (byte) rgb.red; green[i] = (byte) rgb.green; blue[i] = (byte) rgb.blue; } if (data.transparentPixel != -1) { colorModel = new IndexColorModel(data.depth,rgbs.length,red,green,blue,data.transparentPixel); } else { colorModel = new IndexColorModel(data.depth,blue); } BufferedImage bufferedImage = new BufferedImage(colorModel,null); WritableRaster raster = bufferedImage.getRaster(); int[] pixelArray = new int[1]; for (int y = 0; y < data.height; y++) { for (int x = 0; x < data.width; x++) { int pixel = data.getPixel(x,y); pixelArray[0] = pixel; raster.setPixel(x,pixelArray); } } return bufferedImage; } } public static ImageData convertToSWT (BufferedImage bufferedImage) { if (bufferedImage.getColorModel() instanceof DirectColorModel) { DirectColorModel colorModel = (DirectColorModel) bufferedImage.getColorModel(); PaletteData palette = new PaletteData(colorModel.getRedMask(),colorModel.getGreenMask(),colorModel.getBlueMask()); ImageData data = new ImageData(bufferedImage.getWidth(),bufferedImage.getHeight(),colorModel.getPixelSize(),palette); WritableRaster raster = bufferedImage.getRaster(); int[] pixelArray = new int[3]; for (int y = 0; y < data.height; y++) { for (int x = 0; x < data.width; x++) { raster.getPixel(x,pixelArray); int pixel = palette.getPixel(new RGB(pixelArray[0],pixelArray[1],pixelArray[2])); data.setPixel(x,pixel); } } return data; } else if (bufferedImage.getColorModel() instanceof IndexColorModel) { IndexColorModel colorModel = (IndexColorModel) bufferedImage.getColorModel(); int size = colorModel.getMapSize(); byte[] reds = new byte[size]; byte[] greens = new byte[size]; byte[] blues = new byte[size]; colorModel.getReds(reds); colorModel.getGreens(greens); colorModel.getBlues(blues); RGB[] rgbs = new RGB[size]; for (int i = 0; i < rgbs.length; i++) { rgbs[i] = new RGB(reds[i] & 0xFF,greens[i] & 0xFF,blues[i] & 0xFF); } PaletteData palette = new PaletteData(rgbs); ImageData data = new ImageData(bufferedImage.getWidth(),palette); data.transparentPixel = colorModel.getTransparentPixel(); WritableRaster raster = bufferedImage.getRaster(); int[] pixelArray = new int[1]; for (int y = 0; y < data.height; y++) { for (int x = 0; x < data.width; x++) { raster.getPixel(x,pixelArray); data.setPixel(x,pixelArray[0]); } } return data; } return null; }