8

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

编辑: 我正在缩小。

4

3 回答 3

11

AWT 提供不同模式的图像缩放。对于缩小比例,面积平均提供最佳质量。当然可以为 SWT 重新实现面积平均缩放算法,或者,对于通常就足够的快速解决方案:

  • SWT图像转换AWT图像_
  • 使用适当的模式重新缩放它(如果缩小,则为区域平均,否则为双线性)
  • 将其转换回SWT 图像

在 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;
}
于 2012-05-11T09:13:55.157 回答
10

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

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/

于 2016-09-12T11:50:18.987 回答
1

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

唯一的问题是,如果图片是用户上传的,而且你有很大的用户群,你会因为无效的图片文件等而出现内存泄漏。

于 2011-01-21T01:09:29.083 回答