image java dans du html

image java dans du html - HTML/CSS - Programmation

Marsh Posté le 15-09-2006 à 15:26:04    

Bonjour,
 
j'ai un petit probleme, j'aimerais savoir s'il est possible d'afficher dans une page jsp qui
génère du code html une image crée en java avec la classe java.awt.Image? si oui
comment? J'ai pas arréter de chercher avec google mais je dois etre manchot du clavier  
parceque je n'ai rien trouvé :/
 
merci d'avance à ceux qui m'aideront  :love:

Reply

Marsh Posté le 15-09-2006 à 15:26:04   

Reply

Marsh Posté le 15-09-2006 à 15:34:09    

tu crées un second JSP.
 
tu lui met des header correspondant à ceux d'une image (content-type: image/png) par exemple
 
ensuite, dans cette page, tu joues avec ta lib d'image. normalement, tu dois avoir une méthode pour enregistrer l'image en question dans un fichier selon un format (prend le même que celui choisi en header).
 
tu lances cette méthode pour récupérer un stream de bytes (et non un fichier)
 
puis tu fais un binarywrite de ce stream dans ta page.
 
logiquement, ta page va s'afficher comme une image (et être appelable dans un tag <img src="mapage.jsp"/> )
 
attention, je resté général dans ma réponse, je me base sur PHP, ASP et C#, puisque je n'ai jamais touché une ligne de Java ;) Mais y'a pas de raison pour qu'il se comporte différement des autres ;)


Message édité par MagicBuzz le 15-09-2006 à 15:35:17
Reply

Marsh Posté le 15-09-2006 à 15:38:18    

Genre, un exemple en C# qui :
- Récupère en paramètre le nom d'une image à afficher
- Va chercher l'image sur un FTP
- Redimensionne l'image
- Affiche l'image
 
Cette page est appelée comme j'ai dit dans une balise HTML <img>.
 
Tu devrais pouvoir retrouver un truc similaire avec Java
 

Code :
  1. using System;
  2. using System.Drawing;
  3. using System.Drawing.Imaging;
  4. using System.Drawing.Drawing2D;
  5. using System.IO;
  6. using System.Data;
  7. using EnterpriseDT.Net.Ftp;
  8. namespace bci
  9. {
  10. /// <summary>
  11. /// Summary description for showPict.
  12. /// </summary>
  13. public class showThb : System.Web.UI.Page
  14. {
  15.  private Queries commander;
  16.  private const float WIDTH = 128;
  17.  private const float HEIGHT = 96;
  18.  private void Page_Load(object sender, System.EventArgs e)
  19.  {
  20.   // Ajouter le code pour bloquer si Session vide !
  21.   if (Session.Count == 0 || Session["login"] == null || Session["typtie"] == null || Session["codsoc"] == null)
  22.   {
  23.    Session.Clear();
  24.    Response.Redirect("Default.aspx", true);
  25.   }
  26.   // On vérifie que les critères de recherche on bien été remplis
  27.   if (Request.QueryString["chemin_acces"] == null)
  28.   {
  29.    Session.Clear();
  30.    Response.Redirect("Default.aspx", true);
  31.   }
  32.   if ((string)Request.QueryString["chemin_acces"] != string.Empty)
  33.   {
  34.    FTPClient cFtp = new FTPClient();
  35.    cFtp.RemoteHost = "192.168.0.253";
  36.    if (!cFtp.IsConnected)
  37.    {
  38.     cFtp.Connect();
  39.    }
  40.    cFtp.Login("anonymous", "toto@dtc.com" );
  41.    cFtp.TransferType = FTPTransferType.BINARY;
  42.    MemoryStream memStream = new MemoryStream();
  43.    byte[] img = cFtp.Get(((string) Request.QueryString["chemin_acces"]).Replace("ap$photo:", "" ));
  44.    Stream s = new MemoryStream(img);
  45.    Image ori = Image.FromStream(s);
  46.    Image thb = new Bitmap((int)WIDTH, (int)HEIGHT);
  47.    Graphics g = Graphics.FromImage(thb);
  48.    float oriRatio = (float)ori.Width / (float)ori.Height;
  49.    float destRatio = WIDTH / HEIGHT;
  50.    float w, h;
  51.    if ((float)ori.Width > WIDTH && (float)ori.Height > HEIGHT)
  52.    {
  53.     if (oriRatio >= destRatio)
  54.     {
  55.      w = (float)WIDTH;
  56.      h = (float)ori.Height * (float)WIDTH / (float)ori.Width;
  57.     }
  58.     else
  59.     {
  60.      h = (float)HEIGHT;
  61.      w = (float)ori.Width * (float)HEIGHT / (float)ori.Height;
  62.     }
  63.    }
  64.    else
  65.    {
  66.     w = (float)ori.Width;
  67.     h = (float)ori.Height;
  68.    }
  69.    g.Clear(Color.Transparent);
  70.    float wo = (WIDTH - w) / 2f;
  71.    float ho = (HEIGHT - h) / 2f;
  72.    g.DrawImage(ori, wo, ho, w, h);
  73.    thb.Save(memStream, ImageFormat.Png);
  74.    thb.Dispose();
  75.    ori.Dispose();
  76.    Response.Clear();
  77.    Response.ContentType="image/png";
  78.    memStream.WriteTo(Response.OutputStream);
  79.    cFtp.Quit();
  80.   }
  81.   else
  82.   {
  83.    Image img = new Bitmap(MapPath("medias/sans.jpg" ));
  84.    MemoryStream memStream = new MemoryStream();
  85.    img.Save(memStream, ImageFormat.Jpeg);
  86.    img.Dispose();
  87.    Response.Clear();
  88.    Response.ContentType="image/jpeg";
  89.    memStream.WriteTo(Response.OutputStream);
  90.   }
  91.  }
  92.  private void Page_Unload(object sender, System.EventArgs e)
  93.  {
  94.   try{commander.Close();} catch {};
  95.   commander = null;
  96.  }
  97.  #region Web Form Designer generated code
  98.  override protected void OnInit(EventArgs e)
  99.  {
  100.   //
  101.   // CODEGEN: This call is required by the ASP.NET Web Form Designer.
  102.   //
  103.   InitializeComponent();
  104.   base.OnInit(e);
  105.  }
  106.  /// <summary>
  107.  /// Required method for Designer support - do not modify
  108.  /// the contents of this method with the code editor.
  109.  /// </summary>
  110.  private void InitializeComponent()
  111.  {   
  112.   this.Load += new System.EventHandler(this.Page_Load);
  113.   this.Unload += new System.EventHandler(this.Page_Unload);
  114.  }
  115.  #endregion
  116. }
  117. }

Reply

Marsh Posté le 15-09-2006 à 15:41:24    

merci je regarde tout ca pour voir si j'arrive a l'adapter en java ^^

Reply

Marsh Posté le 15-09-2006 à 16:40:53    

j'arrive pas a le faire marcher, je tente une autre methode

Reply

Sujets relatifs:

Leave a Replay

Make sure you enter the(*)required information where indicate.HTML code is not allowed