在Java中调整图像大小以缩小图像大小

在Java中调整图像大小以缩小图像大小

问题描述:

我有一个尺寸为800x800的图像,它的大小为170 kb。我想调整这个图像的大小,说600x600。调整大小后,我希望缩小图像大小。我怎样才能做到这一点?在Java中调整图像大小以缩小图像大小

+0

你可以看看http://*.com/questions/11959758/java-maintaining-aspect-ratio-of-jpanel-background-image/11959928#11959928 – MadProgrammer

+0

1)*“调整大小后我想要图像大小要减小。“*当存储在内存中,或序列化为PNG,JPG或GIF?第一个将导致字节数的减少,第二个通常不会。 2)图像的格式是什么? (PNG,GIF,JPEG ..) –

+0

如果格式为JPEG,则通过提高压缩率来减少磁盘上的图像字节大小,最有可能更有效。有关示例源代码,请参见['ImageCompressionDemo'](http://*.com/a/5998015/418556)。 –

你不需要一个完整的图像处理库来调整图像大小。

推荐的方法是使用渐进双线性缩放,像这样(随意使用这种方法是在你的代码):

public BufferedImage scale(BufferedImage img, int targetWidth, int targetHeight) { 

    int type = (img.getTransparency() == Transparency.OPAQUE) ? BufferedImage.TYPE_INT_RGB : BufferedImage.TYPE_INT_ARGB; 
    BufferedImage ret = img; 
    BufferedImage scratchImage = null; 
    Graphics2D g2 = null; 

    int w = img.getWidth(); 
    int h = img.getHeight(); 

    int prevW = w; 
    int prevH = h; 

    do { 
     if (w > targetWidth) { 
      w /= 2; 
      w = (w < targetWidth) ? targetWidth : w; 
     } 

     if (h > targetHeight) { 
      h /= 2; 
      h = (h < targetHeight) ? targetHeight : h; 
     } 

     if (scratchImage == null) { 
      scratchImage = new BufferedImage(w, h, type); 
      g2 = scratchImage.createGraphics(); 
     } 

     g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, 
       RenderingHints.VALUE_INTERPOLATION_BILINEAR); 
     g2.drawImage(ret, 0, 0, w, h, 0, 0, prevW, prevH, null); 

     prevW = w; 
     prevH = h; 
     ret = scratchImage; 
    } while (w != targetWidth || h != targetHeight); 

    if (g2 != null) { 
     g2.dispose(); 
    } 

    if (targetWidth != ret.getWidth() || targetHeight != ret.getHeight()) { 
     scratchImage = new BufferedImage(targetWidth, targetHeight, type); 
     g2 = scratchImage.createGraphics(); 
     g2.drawImage(ret, 0, 0, null); 
     g2.dispose(); 
     ret = scratchImage; 
    } 

    return ret; 

} 

代码修改,并在Filthy Rich Clients从原来的清洗。


基于您的评论,你可以降低质量和编码JPEG字节就像这样:

image它是BufferedImage。

ByteArrayOutputStream os = new ByteArrayOutputStream(); 
ImageWriter writer = (ImageWriter) ImageIO.getImageWritersByFormatName("jpeg").next(); 

ImageWriteParam param = writer.getDefaultWriteParam(); 
param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); 
param.setCompressionQuality(0.2f); // Change this, float between 0.0 and 1.0 

writer.setOutput(ImageIO.createImageOutputStream(os)); 
writer.write(null, new IIOImage(image, null, null), param); 
writer.dispose(); 

现在,由于os是一个ByteArrayOutputStream,你可以的Base64编码。

String base64 = Base64.encode(os.toByteArray()); 
+0

我想调整大小后缩小图像大小(以字节为单位),以便我可以通过套接字发送它。 dataoutputstream说编码的字符串(Base64)太大。 – Madeyedexter

+0

Base64编码JPEG字节。你可以使用'ImageIO'来做到这一点。 – LanguagesNamedAfterCofee

+0

@丹尼看到我的编辑 – LanguagesNamedAfterCofee

使用一些支持Java处理图像处理的好框架。 IamageJ

Java中还有一些基本支持,如MemoryImageSourcePixelGrabber

+0

我检查出来了。我想在我的Java程序中执行此操作! – Madeyedexter

+0

我使用Core Java中的基本支持信息更新了答案。希望有所帮助。 –

您可以使用100%的Java,Apache2的开源imgscalr library(单一静态类),做这样的事情:

import org.imgscalr.Scalr.*; // static imports are awesome with the lib 

... some class code ... 

// This is a super-contrived method name, I just wanted to incorporate 
// the details from your question accurately. 
public static void resizeImageTo600x600(BufferedImage image) { 
    ImageIO.write(resize(image, 600), "JPG", new File("/path/to/file.jpg")); 
} 

注意:如果上面看起来很奇怪,静进口允许我使用的调整大小直接调用,而无需指定Scalr.resize(...)

此外,如果写出看起来并不足够好了缩放图像的质量(它会很快写出来,虽然),你可以使用更多的参数给调整像方法,以便:

public static void resizeImageTo600x600(BufferedImage image) { 
    ImageIO.write(resize(image, Method.ULTRA_QUALITY, 600), "JPG", new File("/path/to/file.jpg")); 
} 

..而且你甚至可以应用BufferedImageOp中的结果柜面的缩小使图像看起来以锯齿软化它:

public static void resizeImageTo600x600(BufferedImage image) { 
    ImageIO.write(resize(image, Method.ULTRA_QUALITY, 600, Scalr.OP_ANTIALIAS), "JPG", new File("/path/to/file.jpg")); 
} 

你可以开始只需添加在你的Maven POM(imgscalr以下DEP项与库玩弄在Maven*回购):

<dependency> 
    <groupId>org.imgscalr</groupId> 
    <artifactId>imgscalr-lib</artifactId> 
    <version>4.2</version> 
    <type>jar</type> 
    <scope>compile</scope> 
</dependency> 
+0

我不明白许可证。我可以在个人网站上使用它吗? – Ced

+0

另外如果图像更小,它会放大吗? – Ced