如何在java中减小图像文件的大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4449362/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to reduce image file size in java
提问by Anand
I want to reduce image file size of an uploaded image before saving it into server (to reduce loading time). how can I do it using java?
我想在将上传的图像保存到服务器之前减小其图像文件大小(以减少加载时间)。我怎样才能使用 java 做到这一点?
采纳答案by Gabriel Reid
This kind of question has been answered quite a few times on this site. I suggest you check out How to resize the original image into a common size of image in Java?or search for java image resize on this site.
此类问题已在本网站上多次回答。我建议您查看如何在 Java 中将原始图像调整为常见大小的图像?或在此站点上搜索 java image resize。
回答by Rehan Ahmad
/*
* This will get an image file and returns a byte array resized by the given value.
*/
package tajneed;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import javax.imageio.ImageIO;
import org.imgscalr.Scalr;
public class ImageResizer {
public static byte[] resize(File icon) {
try {
BufferedImage originalImage = ImageIO.read(icon);
originalImage= Scalr.resize(originalImage, Scalr.Method.QUALITY, Scalr.Mode.FIT_EXACT, 128, 153);
//To save with original ratio uncomment next line and comment the above.
//originalImage= Scalr.resize(originalImage, 153, 128);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(originalImage, "jpg", baos);
baos.flush();
byte[] imageInByte = baos.toByteArray();
baos.close();
return imageInByte;
} catch (Exception e) {
return null;
}
}
}
回答by Umesh Markande
May this following code help you it will resize and keep quality of image.
愿以下代码可以帮助您调整大小并保持图像质量。
protected void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
if (!isMultipart) {
} else {
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
List items = null;
try {
items = upload.parseRequest(request);
} catch (FileUploadException e) {
System.out.println("Unable to load image" + e.getMessage());}
Iterator itr = items.iterator();
while(itr.hasNext()) {
System.out.println(items.size());
FileItem item = (FileItem) itr.next();
if (!item.isFormField()) {
try {
int size = 200;// size of the new image.
//take the file as inputstream.
InputStream imageStream = item.getInputStream();
//read the image as a BufferedImage.
BufferedImage image = javax.imageio.ImageIO.read(imageStream);
//cal the sacleImage method.
BufferedImage newImage = scaleImage(image, size);
String path = getServletContext().getRealPath("/image");
//write file.
File file = new File(path, "testimage.jpg");
ImageIO.write(newImage, "JPG", file);
} catch (Exception e) {
System.out.println("Unable to save the image" + e.getMessage());
//System.out.println(path);
}//if
}//iter
}
private BufferedImage scaleImage(BufferedImage bufferedImage, int size) {
double boundSize = size;
int origWidth = bufferedImage.getWidth();
int origHeight = bufferedImage.getHeight();
double scale;
if (origHeight > origWidth)
scale = boundSize / origHeight;
else
scale = boundSize / origWidth;
//* Don't scale up small images.
if (scale > 1.0)
return (bufferedImage);
int scaledWidth = (int) (scale * origWidth);
int scaledHeight = (int) (scale * origHeight);
Image scaledImage = bufferedImage.getScaledInstance(scaledWidth, scaledHeight, Image.SCALE_SMOOTH);
// new ImageIcon(image); // load image
// scaledWidth = scaledImage.getWidth(null);
// scaledHeight = scaledImage.getHeight(null);
BufferedImage scaledBI = new BufferedImage(scaledWidth, scaledHeight, BufferedImage.TYPE_INT_RGB);
Graphics2D g = scaledBI.createGraphics();
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g.drawImage(scaledImage, 0, 0, null);
g.dispose();
return (scaledBI);
}