java 改变图片的分辨率。。。可以吗? 20

一个分辨率高的图片2m多。。用java把它转化成300k的。。可以吗?... 一个分辨率高的图片2m多。。用java把它转化成300k的。。可以吗? 展开
 我来答
hsazj
2010-09-06 · TA获得超过448个赞
知道小有建树答主
回答量:188
采纳率:0%
帮助的人:80.2万
展开全部
我给你一个Servlet的源代码,你把这个配置好,试试吧。建议把代码复制到一个开发工具中,这样,你看起来会舒服一些。如下:
package com.photo.gallery;

import java.awt.AlphaComposite;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ImageResizer extends HttpServlet {
private static final long serialVersionUID = -8285774993751841288L;

public void doGet(HttpServletRequest request, HttpServletResponse response) {
String imageOutput = getParam(request, "output", "png");
String imageRoot = getParam(request, "root", "/albums");
String imageFile = getParam(request, "file", "/Album1/image1.jpg");
int width = Integer.parseInt(getParam(request, "width", "250"));
int height = Integer.parseInt(getParam(request, "width", "0"));
if ("png".equals(imageOutput))
response.setContentType("image/png");
else
response.setContentType("image/jpeg");
String imageLoc = request.getSession().getServletContext().getRealPath(
imageRoot)
+ imageFile;
try {
BufferedImage bufferedImage = ImageIO.read(new File(imageLoc));
int calcHeight = height > 0 ? height : (width
* bufferedImage.getHeight() / bufferedImage.getWidth());

ImageIO.write(createResizedCopy(bufferedImage, width, calcHeight),
imageOutput, response.getOutputStream());
} catch (Exception e) {
log("Problem with image: " + imageLoc + e);
}
}

BufferedImage createResizedCopy(Image originalImage, int scaledWidth,
int scaledHeight) {
BufferedImage scaledBI = new BufferedImage(scaledWidth, scaledHeight,
BufferedImage.TYPE_INT_RGB);
Graphics2D g = scaledBI.createGraphics();
g.setComposite(AlphaComposite.Src);
g.drawImage(originalImage, 0, 0, scaledWidth, scaledHeight, null);
g.dispose();
return scaledBI;
}

private String getParam(HttpServletRequest request, String param, String def) {
String parameter = request.getParameter(param);
if (parameter == null || "".equals(parameter)) {
return def;
} else {
return parameter;
}
}
}

2.配置web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<servlet-name>image</servlet-name>
<servlet-class>com.photo.gallery.ImageResizer</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>image</servlet-name>
<url-pattern>/image</url-pattern>
</servlet-mapping>

</web-app>

3.在地址栏输出如下的连接:
http://localhost:8088/Javaimageresizer/image?root=/albums&file=/thumbs/imagename.jpg&width=270&height=100
即可查看效果

参考资料: 网络获取

本回答被网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
百度网友38c9a58cb
2010-09-07
知道答主
回答量:7
采纳率:0%
帮助的人:9.8万
展开全部
ImageIcon imgIcon=new ImageIcon("c:\\img.jpg");
Image img=imgIcon.getImage();
int w=(int) (img.getWidth(null) * 0.5);
int h=(int) (img.getHeight(null) * 0.5);
img=img.getScaledInstance(w, h, Image.SCALE_SMOOTH);
BufferedImage bimg=new BufferedImage(w, h, BufferedImage.TYPE_INT_BGR);
try {
ImageIO.write(bimg, "jpg", new File("c;\\img1.jpg"));
} catch (IOException ex) {
Logger.getLogger(NewClass.class.getName()).log(Level.SEVERE, null, ex);
}

=======================================

上面是将图片文件缩小50%的代码.
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式