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

发布于 2024-10-14 01:58:26 字数 181 浏览 4 评论 0原文

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

编辑: 我正在缩小规模。

My application needs to resize ImageData. Unfortunately, I haven't gotten the results I want with GC (with antialias on and interpolation on high), or ImageData.scaledTo(). The resulting image is of too low a quality to be acceptable. What's the best way to do a high quality ImageData resize?

Edit: I'm scaling down.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

烟若柳尘 2024-10-21 01:58:26

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

AWT offers image scaling with different modes. For downscaling, Area Averaging offers the best quality. One could of course re-implement the area averaging scaling algorithm for SWT, or, for a quick solution which often suffices:

  • convert the SWT image to an AWT image
  • rescale it using an appropriate mode (area averaging if downscaling, bilinerar otherwise)
  • convert it back to an SWT image

The code to convert between SWT and AWT images can be found here.

/**
 * 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;
}
看轻我的陪伴 2024-10-21 01:58:26

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

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/

The accepted solution doesn't deal with transparency. Here's a snippet I've came across that does hi-quality resize and preserves transparency as well:

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

I've found it here:

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

酒浓于脸红 2024-10-21 01:58:26

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

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

We've had success with ImageMagick / JMagick. http://www.jmagick.org/index.html

The only problem is that if the images are user uploads, and you have a large user base, you will have memory leaks because of invalid image files and so on.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文