SWT:如何进行高质量图像调整

时间:2011-01-20 21:22:25

标签: java swt image-resizing

我的应用程序需要调整ImageData的大小。不幸的是,我没有得到我想要的GC结果(带有抗锯齿和高插值)或ImageData.scaledTo()。得到的图像质量太低而无法接受。什么是高质量ImageData调整大小的最佳方法?

编辑: 我缩小了。

3 个答案:

答案 0 :(得分:11)

AWT提供不同模式的图像缩放。对于缩小尺寸,面积平均提供最佳质量。当然,人们可以重新实现SWT的区域平均缩放算法,或者,通常只需快速解决方案:

  • 转换 SWT 图片 AWT 图片
  • 重新缩放使用适当的模式(如果缩小则平均区域,否则为bilinerar)
  • 将其转换回到SWT图片

可以在here找到要在SWT和AWT图像之间进行转换的代码。

/**
 * 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, newHeight, BufferedImage.TYPE_INT_RGB);
    img.getGraphics().drawImage(scaledImage, 0, 0, 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, 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, rgbs.length, red, green, blue);
        }
        BufferedImage bufferedImage = new BufferedImage(colorModel, colorModel.createCompatibleWritableRaster(data.width, data.height),
            false, 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, y, 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, y, pixelArray);
                int pixel = palette.getPixel(new RGB(pixelArray[0], pixelArray[1], pixelArray[2]));
                data.setPixel(x, y, 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(), bufferedImage.getHeight(), colorModel.getPixelSize(), 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, y, pixelArray);
                data.setPixel(x, y, pixelArray[0]);
            }
        }
        return data;
    }
    return null;
}

答案 1 :(得分:8)

公认的解决方案不涉及透明度。这是我遇到的一个片段,它可以高质量地调整大小并保持透明度:

public static Image resize(Image image, int width, int height) {
  Image scaled = new Image(Display.getDefault(), width, height);
  GC gc = new GC(scaled);
  gc.setAntialias(SWT.ON);
  gc.setInterpolation(SWT.HIGH);
  gc.drawImage(image, 0, 0,image.getBounds().width, image.getBounds().height, 0, 0, width, height);
  gc.dispose();
  image.dispose(); // don't forget about me!
  return scaled;
}

我在这里找到了它:

http://aniszczyk.org/2007/08/09/resizing-images-using-swt/

答案 2 :(得分:1)

我们在ImageMagick / JMagick上取得了成功。 http://www.jmagick.org/index.html

唯一的问题是,如果图像是用户上传的,并且您拥有庞大的用户群,则由于图像文件无效等原因会导致内存泄漏。