package com.blogspot.na5cent.utils;
import java.awt.AlphaComposite;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
/**
*
* @author redcrow
*/
public class PhotoUtils {
private static final int DEFAULT_TYPE = 5;
public static BufferedImage resize(BufferedImage image, int newWidth, int newHeight) {
int oldWidth = image.getWidth();
int oldHeight = image.getHeight();
if (oldHeight < newHeight || oldWidth < newWidth) {
newHeight = oldHeight;
newWidth = oldWidth;
}else{
float percentWidth = newWidth / (float) oldWidth;
float percentHeight = newHeight / (float) oldHeight;
if (newWidth == 0) {
newWidth = (int) (percentHeight * oldWidth);
}
if (newHeight == 0) {
newHeight = (int) (percentWidth * oldHeight);
}
}
BufferedImage resized = new BufferedImage(newWidth, newHeight, DEFAULT_TYPE);
Graphics2D g = resized.createGraphics();
g.drawImage(image, 0, 0, newWidth, newHeight, null);
g.dispose();
return resized;
}
}
Example to use PhotoService.java (Spring MVC : controller)
package com.blogspot.na5cent.service;
import com.google.common.io.ByteStreams;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.blogspot.na5cent.utils.PhotoUtils;
import com.blogspot.na5cent.configs.FileConfigs;
/**
*
* @author redcrow
*/
@Controller
public class PhotoService {
private static final File DIRECTORY_UPLOAD = FileConfigs.DIRECTORY_UPLOAD;
@RequestMapping(value = "/service/photo", method = RequestMethod.GET)
@ResponseBody
public String getFile(HttpServletRequest request, HttpServletResponse response) throws IOException {
//
String fileName = request.getParameter("name");
String width = request.getParameter("width");
String height = request.getParameter("height");
String type = request.getServletContext().getMimeType(fileName);
int widthInt = (width == null) ? 0 : Integer.parseInt(width);
int heightInt = (height == null) ? 0 : Integer.parseInt(height);
if (widthInt == 0 && heightInt == 0) {
response.sendError(HttpServletResponse.SC_NOT_FOUND); // 404.
return null;
}
File file = new File(DIRECTORY_UPLOAD, fileName);
BufferedImage image;
try {
image = ImageIO.read(file);
} catch (Exception ex) {
response.sendError(HttpServletResponse.SC_NOT_FOUND); // 404.
return null;
}
image = PhotoUtils.resize(image, widthInt, heightInt); //*****
response.reset();
response.setBufferSize(1024);
response.setContentType(type);
response.setHeader("Content-Disposition", "inline; filename=\"" + fileName + "\"");
InputStream inputStream = null;
ByteArrayOutputStream baos = null;
try {
baos = new ByteArrayOutputStream();
ImageIO.write(image, "png", baos);
inputStream = new ByteArrayInputStream(baos.toByteArray());
response.setHeader("Content-Length", String.valueOf(baos.size()));
ByteStreams.copy(inputStream, response.getOutputStream());
} finally {
if (inputStream != null) {
inputStream.close();
}
if (baos != null) {
baos.close();
}
}
return null;
}
}
html
<!-- image --> <img src="http://na5cent.blogspot.com/service/photo?name=duck.png&width=200"/> <!-- image -->Web
Origin


ไม่มีความคิดเห็น:
แสดงความคิดเห็น