【CSDN常见问题解答】Web上传图片生成指定大小图片
2013-10-20 22:21
537 查看
在使用playframework的时候,发现框架提供了一个Image类,这个类可以实现生成指定大小的图片功能,我们来看下这个类:
/**
* Resize an image
* @param originalImage The image file
* @param to The destination file
* @param w The new width (or -1 to proportionally resize)
* @param h The new height (or -1 to proportionally resize)
*/
public static void resize(File originalImage, File to, Integer w, Integer h) {
try {
BufferedImage source = ImageIO.read(originalImage);
int owidth = source.getWidth();
int oheight = source.getHeight();
double ratio = (double) owidth / oheight;
if (w < 0 && h < 0) {
w = owidth;
h = oheight;
}
if (w < 0 && h > 0) {
w = (int) (h * ratio);
}
if (w > 0 && h < 0) {
h = (int) (w / ratio);
}
String mimeType = "image/jpeg";
if (to.getName().endsWith(".png")) {
mimeType = "image/png";
}
if (to.getName().endsWith(".gif")) {
mimeType = "image/gif";
}
// out
BufferedImage dest = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
Image srcSized = source.getScaledInstance(w, h, Image.SCALE_SMOOTH);
Graphics graphics = dest.getGraphics();
graphics.setColor(Color.WHITE);
graphics.fillRect(0, 0, w, h);
graphics.drawImage(srcSized, 0, 0, null);
ImageWriter writer = ImageIO.getImageWritersByMIMEType(mimeType).next();
ImageWriteParam params = writer.getDefaultWriteParam();
FileImageOutputStream toFs = new FileImageOutputStream(to);
writer.setOutput(toFs);
IIOImage image = new IIOImage(dest, null, null);
writer.write(null, image, params);
toFs.flush();
toFs.close();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
写个方法测试一下,
上传一个图片,重新生成一个100×100像素大小的图片,并存放在public/images/目录下。
File attachment是上传文件空间file对应的名字,和之前上传文件的一样。
public static void imageresize(File attachment){
if(request.method.equalsIgnoreCase("POST")){
Images.resize(attachment, Play.getFile("public/images/" + attachment.getName()), 100, 100);
render();
}else{
render();
}
}
大功告成。
/**
* Resize an image
* @param originalImage The image file
* @param to The destination file
* @param w The new width (or -1 to proportionally resize)
* @param h The new height (or -1 to proportionally resize)
*/
public static void resize(File originalImage, File to, Integer w, Integer h) {
try {
BufferedImage source = ImageIO.read(originalImage);
int owidth = source.getWidth();
int oheight = source.getHeight();
double ratio = (double) owidth / oheight;
if (w < 0 && h < 0) {
w = owidth;
h = oheight;
}
if (w < 0 && h > 0) {
w = (int) (h * ratio);
}
if (w > 0 && h < 0) {
h = (int) (w / ratio);
}
String mimeType = "image/jpeg";
if (to.getName().endsWith(".png")) {
mimeType = "image/png";
}
if (to.getName().endsWith(".gif")) {
mimeType = "image/gif";
}
// out
BufferedImage dest = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
Image srcSized = source.getScaledInstance(w, h, Image.SCALE_SMOOTH);
Graphics graphics = dest.getGraphics();
graphics.setColor(Color.WHITE);
graphics.fillRect(0, 0, w, h);
graphics.drawImage(srcSized, 0, 0, null);
ImageWriter writer = ImageIO.getImageWritersByMIMEType(mimeType).next();
ImageWriteParam params = writer.getDefaultWriteParam();
FileImageOutputStream toFs = new FileImageOutputStream(to);
writer.setOutput(toFs);
IIOImage image = new IIOImage(dest, null, null);
writer.write(null, image, params);
toFs.flush();
toFs.close();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
写个方法测试一下,
上传一个图片,重新生成一个100×100像素大小的图片,并存放在public/images/目录下。
File attachment是上传文件空间file对应的名字,和之前上传文件的一样。
public static void imageresize(File attachment){
if(request.method.equalsIgnoreCase("POST")){
Images.resize(attachment, Play.getFile("public/images/" + attachment.getName()), 100, 100);
render();
}else{
render();
}
}
大功告成。
相关文章推荐
- 对给定的一个图片生成一个指定大小的缩略图(上传图片很大是可用)
- php实现按指定大小等比缩放生成上传图片缩略图的方法
- 原创:彻底解决 ewebeditor 图片上传大小问题
- php实现按指定大小等比缩放生成上传图片缩略图的方法
- php实现按指定大小等比缩放生成上传图片缩略图的方法
- 关于eWebEditor突破上传图片大小限制的问题
- php按指定大小等比缩放生成上传图片缩略图
- 上传图片 生成指定大小的缩略图
- 上传图片,并生成缩略图(指定大小)
- PHP_后台不能上传图片的几种常见问题
- 【CSDN常见问题解答】Java多种数据库连接
- 【CSDN常见问题解答】用HTTPClient自动登录Tomcat管理页面
- 【CSDN常见问题解答】Java多种数据库连接
- JS前端上传图片、压缩、并且处理旋转问题,生成base64数据
- web开发 上传多张图片 遇到的问题
- CSS图片垂直居中方法整理集合 !(常见问题解答)
- WebView 图片大小自适应问题
- javascript 限制上传图片大小 - [WEB]
- object-fit 解决图片指定大小被压缩问题
- CSDN中上传图片的问题