posts - 431,  comments - 344,  trackbacks - 0

import Java.awt.Dimension;
import Java.awt.Graphics2D;
import Java.awt.geom.Rectangle2D;
import Java.awt.image.BufferedImage;
import Java.io.OutputStream;
import Java.io.StringReader;
import Java.util.Iterator;

import javax.servlet.http.HttpServletResponse;
import javax.vecmath.Point2d;

import org.apache.log4j.Logger;
import org.openscience.cdk.Molecule;
import org.openscience.cdk.interfaces.IAtom;
import org.openscience.cdk.interfaces.IMolecule;
import org.openscience.cdk.io.MDLReader;
import org.openscience.cdk.layout.StructureDiagramGenerator;
import org.openscience.cdk.renderer.Renderer2DModel;
import org.openscience.cdk.renderer.SimpleRenderer2D;

public class ImageTypeExporterUtil {
 private static final Logger logger = Logger.getLogger(ImageTypeExporterUtil.class);
 
 /**
  * show molecule structure to image type (png, jpeg)
  *
  * @param mol String molecule stucture
  * @param length width and height
  * @param response HttpServletResponse object
  * @throws Exception
  *             if occurred exception ,then throw Exception
  */
 public static void showAsImage(String stucture, Integer length, HttpServletResponse response) throws Exception {
  logger.debug("ImageTypeExporterUtil.showAsImage..");
  
  StringReader mdl = new StringReader(stucture);
  MDLReader cdkMDL = new MDLReader(mdl);
  Molecule mol = new Molecule();
  cdkMDL.read(mol);
  // null coordinates
  Iterator<IAtom> itatoms = mol.atoms();
  while (itatoms.hasNext()) {
   IAtom atom = itatoms.next();
   atom.setPoint2d(null);
   atom.setPoint3d(null);
  }
  // generate 2D coordinates
  StructureDiagramGenerator sdg = new StructureDiagramGenerator();
  sdg.setMolecule(mol);
  try {
   sdg.generateCoordinates();
  } catch (Exception ex) {
   ex.printStackTrace();
  }
  IMolecule layedOutMol = sdg.getMolecule();
  // scale molecule
  final double UNDEF_POS = 100000;
  double minX = UNDEF_POS, minY = UNDEF_POS, maxX = UNDEF_POS, maxY = UNDEF_POS;
  itatoms = layedOutMol.atoms();
  while (itatoms.hasNext()) {
   IAtom atom = itatoms.next();
   Point2d point2d = atom.getPoint2d();
   if (minX == UNDEF_POS || minX > point2d.x)
    minX = point2d.x;
   if (minY == UNDEF_POS || minY > point2d.y)
    minY = point2d.y;
   if (maxX == UNDEF_POS || maxX < point2d.x)
    maxX = point2d.x;
   if (maxY == UNDEF_POS || maxY < point2d.y)
    maxY = point2d.y;
  }
  double scaleX = length / (maxX - minX + 1);
  double scaleY = length / (maxY - minY + 1);
  double scale = scaleX > scaleY ? scaleY : scaleX;
  double centreX = scale * (maxX + minX) / 2.;
  double centreY = scale * (maxY + minY) / 2.;
  double offsetX = length / 2. - centreX;
  double offsetY = length / 2. - centreY;
  itatoms = layedOutMol.atoms();
  while (itatoms.hasNext()) {
   IAtom atom = itatoms.next();
   Point2d a = atom.getPoint2d();
   Point2d b = new Point2d();
   b.x = a.x * scale + offsetX;
   b.y = a.y * scale + offsetY;
   atom.setPoint2d(b);
  }
  // set rendering properties
  Renderer2DModel r2dm = new Renderer2DModel();
  r2dm.setDrawNumbers(false);
  r2dm.setUseAntiAliasing(true);
  r2dm.setColorAtomsByType(true);
  r2dm.setShowAtomTypeNames(false);
  r2dm.setShowAromaticity(true);
  r2dm.setShowImplicitHydrogens(false);
  r2dm.setShowReactionBoxes(false);
  r2dm.setKekuleStructure(false);
  Dimension dim = new Dimension();
  dim.setSize(length, length);
  r2dm.setBackgroundDimension(dim);
  r2dm.setBackColor(java.awt.Color.WHITE);
  // render the image
  SimpleRenderer2D renderer = new SimpleRenderer2D();
  renderer.setRenderer2DModel(r2dm);
  BufferedImage bufferedImage = new BufferedImage(length, length,
    BufferedImage.TYPE_INT_RGB);
  Graphics2D graphics = bufferedImage.createGraphics();
  graphics.setPaint(java.awt.Color.WHITE);
  Rectangle2D.Float rectangle = new Rectangle2D.Float(0, 0, length, length);
  graphics.fill(rectangle);
  renderer.paintMolecule(layedOutMol, graphics);
  // write the image to response
  response.setContentType("image/png");
  OutputStream out = response.getOutputStream();
  try {
   javax.imageio.ImageIO.write(bufferedImage, "png", out);
  } finally {
   out.close();
  }
 }
}

posted on 2009-10-22 08:51 周锐 阅读(752) 评论(0)  编辑  收藏 所属分类: ChemistryJavaCDK

只有注册用户登录后才能发表评论。


网站导航: