MARCO's FREE MEMO

携帯フォトや備忘録などをちょこっと…

BLOBの画像に描画して表示

2007年08月18日 09時47分17秒 | JSP(Java)
たとえば、地図のjpegに同心円を描画するための手順。

//BLOBからイメージを取得
public BufferedImage outputBLOB(int id) throws Exception {
 ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
 InputStream in = null;
 byte b[] = new byte[4096];

 try {
  // --データベースをオープンする処理を記述--

  sql = "select BFILE from BLOB_DATA where ID="+id;
  rs = stmt.executeQuery(sql);
  if( rs.next() ){
   in = rs.getBinaryStream("BFILE");
  }
  while (true) {
   int bytes = in.read(b);
   if (bytes == -1)break;
   byteOut.write(b,0,bytes);
  }
  in.close();
  // --データベースをクローズする処理を記述--

  byte[] data = byteOut.toByteArray();
  ByteArrayInputStream byteIn = new ByteArrayInputStream(data);
  BufferedImage image = ImageIO.read(byteIn);
  getModifiedImage(image);

  return image;

 }catch (Exception e) {
  e.printStackTrace();
  return null;
 }
}

//イメージに同心円を描画
public BufferedImage getModifiedImage(BufferedImage im){
 Graphics g = im.createGraphics();

 g.setColor(Color.YELLOW);
 int cx = 250;
 int cy = 250;
 g.drawOval(cx-100, cy-100, 200, 200);
 g.drawOval(cx-200, cy-200, 400, 400);
 g.drawOval(cx-300, cy-300, 600, 600);

 g.dispose();
 return im;
}

#半年ほど放置していたが、近頃また新しい手法に挑戦しはじめたのでメモっておこう・・・。
#そういえば、ここあさんちに赤ちゃんが生まれたらしい。あとでお祝いにいこうっと♪
↑一部の人にしか通じない話題