将缩略图像素值加载到 Java 中的最快方法

发布于 2024-10-07 23:38:16 字数 2161 浏览 0 评论 0原文

我需要能够将特定分辨率的 RGB 像素值加载到 Java 中。该分辨率很小(~300x300)。

目前,我一直这样加载它们:

File file = new File("...path...");     
BufferedImage imsrc = ImageIO.read(file);
int width = imsrc.getWidth();
int height = imsrc.getHeight();     
int[] data = new int[width * height];       
imsrc.getRGB(0,0, width, height, data, 0, width);

然后自己缩小它的大小。

Sam 要求提供缩小尺寸的代码,所以这里是:

/**
 * DownSize an image. 
 * This is NOT precise, and is noisy. 
 * However, this is fast and better than NearestNeighbor
 * @param pixels - _RGB pixel values for the original image
 * @param width - width of the original image
 * @param newWidth - width of the new image
 * @param newHeight - height of the new image
 * @return - _RGB pixel values of the resized image
 */
public static int[] downSize(int[] pixels, int width, int newWidth, int newHeight) {
    int height = pixels.length / width;
    if (newWidth == width && height == newHeight) return pixels;
    int[] resized = new int[newWidth * newHeight];
    float x_ratio = (float) width / newWidth;
    float y_ratio = (float) height / newHeight;
    float xhr = x_ratio / 2;
    float yhr = y_ratio / 2;
    int i, j, k, l, m;
    for (int x = 0; x < newWidth; x ++)
        for (int y = 0; y < newHeight; y ++) {              
            i = (int) (x * x_ratio);
            j = (int) (y * y_ratio);
            k = (int) (x * x_ratio + xhr);
            l = (int) (y * y_ratio + yhr);
            for (int p = 0; p < 3; p ++) {
                m = 0xFF << (p * 8);
                resized[x + y * newWidth] |= (
                        (pixels[i + j * width] & m) +
                        (pixels[k + j * width] & m) +
                        (pixels[i + l * width] & m) + 
                        (pixels[k + l * width] & m) >> 2) & m;
            }
        }
    return resized;
}

最近,我意识到我可以使用 ImageMagick 的“转换”缩小尺寸,然后以这种方式加载缩小的版本。这可额外节省 33%。

我想知道是否有更好的方法。

编辑:我意识到有些人会想知道我的代码总体上是否良好,答案是否定的。我使用的代码对我来说效果很好,因为我缩小了已经很小的图像(比如 640x480,否则 .getRGB() 需要永远),并且我不在乎是否有几个色点溢出(从添加中遗留下来) ,我知道有些人真的很关心这一点。

I need to be able to load RGB pixel values at a certain resolution into Java. That resolution is small (~300x300).

Currently, I've been loading them like this:

File file = new File("...path...");     
BufferedImage imsrc = ImageIO.read(file);
int width = imsrc.getWidth();
int height = imsrc.getHeight();     
int[] data = new int[width * height];       
imsrc.getRGB(0,0, width, height, data, 0, width);

and then downsizing it myself.

Sam asked for the down-sizing code, so here it is:

/**
 * DownSize an image. 
 * This is NOT precise, and is noisy. 
 * However, this is fast and better than NearestNeighbor
 * @param pixels - _RGB pixel values for the original image
 * @param width - width of the original image
 * @param newWidth - width of the new image
 * @param newHeight - height of the new image
 * @return - _RGB pixel values of the resized image
 */
public static int[] downSize(int[] pixels, int width, int newWidth, int newHeight) {
    int height = pixels.length / width;
    if (newWidth == width && height == newHeight) return pixels;
    int[] resized = new int[newWidth * newHeight];
    float x_ratio = (float) width / newWidth;
    float y_ratio = (float) height / newHeight;
    float xhr = x_ratio / 2;
    float yhr = y_ratio / 2;
    int i, j, k, l, m;
    for (int x = 0; x < newWidth; x ++)
        for (int y = 0; y < newHeight; y ++) {              
            i = (int) (x * x_ratio);
            j = (int) (y * y_ratio);
            k = (int) (x * x_ratio + xhr);
            l = (int) (y * y_ratio + yhr);
            for (int p = 0; p < 3; p ++) {
                m = 0xFF << (p * 8);
                resized[x + y * newWidth] |= (
                        (pixels[i + j * width] & m) +
                        (pixels[k + j * width] & m) +
                        (pixels[i + l * width] & m) + 
                        (pixels[k + l * width] & m) >> 2) & m;
            }
        }
    return resized;
}

Recently, I realized that I can down-size with ImageMagick's 'convert' and then load the down-sized version that way. This saves additional 33%.

I was wondering, if there's an even better way.

EDIT: I realized that some people would wonder if my code is good in general, and the answer is NO. The code I used works well for me, because I down-size already small images (say 640x480, otherwise .getRGB() takes forever) and I don't care if a couple of color points spill over (carry-over from addition), and I know some people really care about that.

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

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

发布评论

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

评论(1

凉风有信 2024-10-14 23:38:16

这是一篇关于以最佳方式在 Java 中生成缩略图的非常好的文章:

http://today.java.net/pub/a/today/2007/04/03/perils-of-image-getscaledinstance.html

通过指定,您可能会得到更好的结果不同的缩放/渲染参数。

    Graphics2D g2 = (Graphics2D)g;
    int newW = (int)(originalImage.getWidth() * xScaleFactor);
    int newH = (int)(originalImage.getHeight() * yScaleFactor);
    g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
                        RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR);
    g2.drawImage(originalImage, 0, 0, newW, newH, null);

Here's a very good article on generating thumbnails in Java in an optimal way:

http://today.java.net/pub/a/today/2007/04/03/perils-of-image-getscaledinstance.html

You may have better results with specifying different scaling/rendering parameters.

    Graphics2D g2 = (Graphics2D)g;
    int newW = (int)(originalImage.getWidth() * xScaleFactor);
    int newH = (int)(originalImage.getHeight() * yScaleFactor);
    g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
                        RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR);
    g2.drawImage(originalImage, 0, 0, newW, newH, null);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文