我有一個尺寸爲800x800的圖像,它的大小爲170 kb。我想調整這個圖像的大小,說600x600。調整大小後,我希望縮小圖像大小。我怎樣才能做到這一點?在Java中調整圖像大小以縮小圖像大小
回答
你不需要一個完整的圖像處理庫來調整圖像大小。
推薦的方法是使用漸進雙線性縮放,像這樣(隨意使用這種方法是在你的代碼):
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());
我想調整大小後縮小圖像大小(以字節爲單位),以便我可以通過套接字發送它。 dataoutputstream說編碼的字符串(Base64)太大。 – Madeyedexter
Base64編碼JPEG字節。你可以使用'ImageIO'來做到這一點。 – LanguagesNamedAfterCofee
@丹尼看到我的編輯 – LanguagesNamedAfterCofee
使用一些支持Java處理圖像處理的好框架。 IamageJ
Java中還有一些基本支持,如MemoryImageSource和PixelGrabber。
我檢查出來了。我想在我的Java程序中執行此操作! – Madeyedexter
我使用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>
- 1. 圖像大小調整 - 文件大小不像Python縮小
- 2. 圖像調整大小Java
- 3. 圖像調整大小,Java
- 4. Java調整圖像大小
- 5. 圖像縮略圖和調整大小
- 6. 調整圖像大小和縮略圖
- 7. Wordpress調整圖像大小時如何縮小文件大小?
- 8. 自動調整圖像大小並縮小文件大小
- 9. C#簡單圖像調整大小:文件大小不縮小
- 10. NSImageView調整大小與圖像大小
- 11. 在Netbeans Java中調整圖像大小
- 12. 在java中調整圖像大小
- 13. 在java中調整大小圖像
- 14. 用Java調整圖像大小。如何調整大小
- 15. 縮小圖像的大小
- 16. 縮小圖像大小php
- 17. 如何根據圖像大小調整圖像視圖大小?
- 18. 縮小大圖像
- 19. 圖像縮放調整大小IMAGEVIEW android
- 20. 圖像未調整大小
- 21. 調整jpeg圖像大小?
- 22. codeigniter圖像調整大小
- 23. Phalcon圖像調整大小
- 24. JavaFx圖像調整大小
- 25. ckfinder圖像調整大小
- 26. 圖像調整大小Codeigniter
- 27. 調整圖像大小。 。 。 。
- 28. 調整圖像大小
- 29. 調整圖像大小
- 30. 調整圖像大小5
你可以看看http://stackoverflow.com/questions/11959758/java-maintaining-aspect-ratio-of-jpanel-background-image/11959928#11959928 – MadProgrammer
1)*「調整大小後我想要圖像大小要減小。「*當存儲在內存中,或序列化爲PNG,JPG或GIF?第一個將導致字節數的減少,第二個通常不會。 2)圖像的格式是什麼? (PNG,GIF,JPEG ..) –
如果格式爲JPEG,則通過提高壓縮率來減少磁盤上的圖像字節大小,最有可能更有效。有關示例源代碼,請參見['ImageCompressionDemo'](http://stackoverflow.com/a/5998015/418556)。 –