- hank
- 等级: 初级会员
- 性别:
- 文章: 8
- 积分: 70
- 来自: 北京
|
web页面上的图片显示可以指定了一个url, 直接访问后台的一个servlet. 在后台数据库可以专门建一个图片库. 以下是一个简单的示例.在这里采用了imageJ-v1.37项目(网址: http://rsb.info.nih.gov/ij/ ) 进行图片处理.
1, ImageViewServlet
java 代码
- package com.hank.web.servlet;
-
- import java.io.IOException;
- import java.io.OutputStream;
-
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
-
- import org.apache.commons.logging.Log;
- import org.apache.commons.logging.LogFactory;
- import org.springframework.context.ApplicationContext;
- import org.springframework.web.context.support.WebApplicationContextUtils;
-
- import com.hank.model.ConAttachs;
- import com.hank.service.ConAttachsManager;
- import com.hank.util.ImageUtil;
-
-
-
-
-
-
- public class ImageViewServlet extends HttpServlet {
- private static final long serialVersionUID = 714920135153152003L;
- protected final transient Log log = LogFactory.getLog(getClass());
- @Override
- protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- execute(request, response);
- }
-
- @Override
- protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- execute(request, response);
- }
-
- public void execute(HttpServletRequest request, HttpServletResponse response)
- throws IOException, ServletException {
- String[] infos = request.getPathInfo().split("/");
- String name=infos[1];
- String id=infos[2].substring(0, infos[2].lastIndexOf('.'));
-
- OutputStream out = response.getOutputStream();
-
- ApplicationContext ctx =
- WebApplicationContextUtils.getWebApplicationContext(getServletContext());
-
- if (name.equals("conAttach")){
- ConAttachsManager mgr = (ConAttachsManager)ctx.getBean("conAttachsManager");
- ConAttachs conAttachs = mgr.getConAttachsByContextId(id);
-
- if(conAttachs.getFType().intValue()==1){
- return;
- }
- byte[] images = conAttachs.getContextFile();
- try {
- response.setContentType(conAttachs.getFhttpContent());
- ImageUtil.outLogo(images,out,200,100);
- response.getOutputStream().flush();
- } catch (Exception e) {
- e.printStackTrace();
- }finally{
- if(out!=null){
- out.close();
- }
- }
- }else if(){
- ... ...
- }
- }
- }
2, ImageUtil
java 代码
- package com.hank.util;
-
- import ij.ImagePlus;
- import ij.io.Opener;
- import ij.process.Blitter;
- import ij.process.ImageProcessor;
-
- import java.awt.Graphics;
- import java.awt.Image;
- import java.awt.image.BufferedImage;
- import java.io.BufferedInputStream;
- import java.io.BufferedOutputStream;
- import java.io.ByteArrayInputStream;
- import java.io.ByteArrayOutputStream;
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.OutputStream;
- import java.io.PrintWriter;
-
- import org.apache.commons.logging.Log;
- import org.apache.commons.logging.LogFactory;
-
- import com.sun.image.codec.jpeg.ImageFormatException;
- import com.sun.image.codec.jpeg.JPEGCodec;
- import com.sun.image.codec.jpeg.JPEGImageEncoder;
-
-
-
-
-
-
- public class ImageUtil {
- protected final transient Log log = LogFactory.getLog(getClass());
-
- public static void outLogo(byte[] source,OutputStream out,int dwidth,int dheight) throws Exception{
- BufferedInputStream stream = new BufferedInputStream((new ByteArrayInputStream(source)),8092);
- Image src = javax.imageio.ImageIO.read(stream);
- int width=src.getWidth(null);
- int height=src.getHeight(null);
- int towidth,toheight;
- if (width>dwidth || height>dheight){
- if (((float)width/dwidth)>=((float)height/dheight)){
- towidth = dwidth;
- toheight = (height*dwidth)/width;
- }else{
- toheight = dheight;
- towidth = (width* dheight)/height;
- }
- }else{
- towidth= width;
- toheight=height;
- }
-
- BufferedImage tag = new BufferedImage(towidth,toheight,BufferedImage.TYPE_INT_RGB);
- tag.getGraphics().drawImage(src,0,0,towidth,toheight,null);
- JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
- encoder.encode(tag);
- }
- }
3 ,在web.xml中配置 servlet
4 , 页面上直接使用img标签指定src即可
- ij.jar (1 MB)
- 描述: imageJ包
- 下载次数: 270
声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
|
返回顶楼 |
|
|
- yourgame
- 等级: 初级会员
- 性别:
- 文章: 75
- 积分: 20
- 来自: 深圳
|
import ij.ImagePlus;
import ij.io.Opener;
import ij.process.Blitter;
import ij.process.ImageProcessor;
貌似没有使用imageJ
|
返回顶楼 |
|
|
- wy36928
- 等级: 初级会员
- 性别:
- 文章: 1
- 积分: 30
- 来自: 上海
|
hank 写道 web页面上的图片显示可以指定了一个url, 直接访问后台的一个servlet. 在后台数据库可以专门建一个图片库. 以下是一个简单的示例.在这里采用了imageJ-v1.37项目(网址: http://rsb.info.nih.gov/ij/ ) 进行图片处理. 1, ImageViewServlet java 代码 - package com.hank.web.servlet;
-
- import java.io.IOException;
- testtest
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
-
- import org.apache.commons.logging.Log;
- import org.apache.commons.logging.LogFactory;
- import org.springframework.context.ApplicationContext;
- import org.springframework.web.context.support.WebApplicationContextUtils;
-
- import com.hank.model.ConAttachs;
- import com.hank.service.ConAttachsManager;
- import com.hank.util.ImageUtil;
-
-
-
-
-
-
- public class ImageViewServlet extends HttpServlet {
- private static final long serialVersionUID = 714920135153152003L;
- protected final transient Log log = LogFactory.getLog(getClass());
- @Override
- protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- execute(request, response);
- }
-
- @Override
- protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- execute(request, response);
- }
-
- public void execute(HttpServletRequest request, HttpServletResponse response)
- throws IOException, ServletException {
- String[] infos = request.getPathInfo().split("/");
- String name=infos[1];
- String id=infos[2].substring(0, infos[2].lastIndexOf('.'));
-
- OutputStream out = response.getOutputStream();
-
- ApplicationContext ctx =
- WebApplicationContextUtils.getWebApplicationContext(getServletContext());
-
- if (name.equals("conAttach")){
- ConAttachsManager mgr = (ConAttachsManager)ctx.getBean("conAttachsManager");
- ConAttachs conAttachs = mgr.getConAttachsByContextId(id);
-
- if(conAttachs.getFType().intValue()==1){
- return;
- }
- byte[] images = conAttachs.getContextFile();
- try {
- response.setContentType(conAttachs.getFhttpContent());
- ImageUtil.outLogo(images,out,200,100);
- response.getOutputStream().flush();
- } catch (Exception e) {
- e.printStackTrace();
- }finally{
- if(out!=null){
- out.close();
- }
- }
- }else if(){
- ... ...
- }
- }
- }
2, ImageUtil java 代码 - package com.hank.util;
-
- import ij.ImagePlus;
- import ij.io.Opener;
- import ij.process.Blitter;
- import ij.process.ImageProcessor;
-
- import java.awt.Graphics;
- import java.awt.Image;
- import java.awt.image.BufferedImage;
- import java.io.BufferedInputStream;
- import java.io.BufferedOutputStream;
- import java.io.ByteArrayInputStream;
- import java.io.ByteArrayOutputStream;
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.OutputStream;
- import java.io.PrintWriter;
-
- import org.apache.commons.logging.Log;
- import org.apache.commons.logging.LogFactory;
-
- import com.sun.image.codec.jpeg.ImageFormatException;
- import com.sun.image.codec.jpeg.JPEGCodec;
- import com.sun.image.codec.jpeg.JPEGImageEncoder;
-
-
-
-
-
-
- public class ImageUtil {
- protected final transient Log log = LogFactory.getLog(getClass());
-
- public static void outLogo(byte[] source,OutputStream out,int dwidth,int dheight) throws Exception{
- BufferedInputStream stream = new BufferedInputStream((new ByteArrayInputStream(source)),8092);
- Image src = javax.imageio.ImageIO.read(stream);
- int width=src.getWidth(null);
- int height=src.getHeight(null);
- int towidth,toheight;
- if (width>dwidth || height>dheight){
- if (((float)width/dwidth)>=((float)height/dheight)){
- towidth = dwidth;
- toheight = (height*dwidth)/width;
- }else{
- toheight = dheight;
- towidth = (width* dheight)/height;
- }
- }else{
- towidth= width;
- toheight=height;
- }
-
- BufferedImage tag = new BufferedImage(towidth,toheight,BufferedImage.TYPE_INT_RGB);
- tag.getGraphics().drawImage(src,0,0,towidth,toheight,null);
- JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
- encoder.encode(tag);
- }
- }
3 ,在web.xml中配置 servlet 4 , 页面上直接使用img标签指定src即可
|
返回顶楼 |
|
|
- liangguanhui
- 等级:
- 性别:
- 文章: 242
- 积分: 308
|
这样生成的缩略图貌似很粗糙的。
|
返回顶楼 |
|
|