使用一些支持通过Java进行图像处理的良好框架,例如IamageJ
Java中还可以通过诸如MemoryImageSource和PixelGrabber之类的某些类提供一些基本支持。
0
您不需要完整的图像处理库即可简单地调整图像大小。
推荐的方法是使用渐进双线性缩放 ,就像这样(随意使用此方法,就像在您的代码中一样):
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
您可以使用100%Java,Apache2开源imgscalr库 (单个静态类),并执行以下操作:
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(...)就可以直接使用resize调用。
另外,如果写出的缩放图像质量不够好(尽管很快就会写出),则可以对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中添加以下dep条目即可开始使用该库(imgscalr在Maven中央存储库中):
<dependency>
<groupId>org.imgscalr</groupId>
<artifactId>imgscalr-lib</artifactId>
<version>4.2</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
0
我有一张图片,尺寸为800x800,尺寸为170 kb。我想将此图像调整为600x600。调整大小后,我希望减小图像尺寸。我怎样才能做到这一点?