Converting transparent GIF to JPEG via servlet

Discussion in 'Java' started by john_pallozzi@itworld.com, Apr 29, 2005.

  1. Guest

    Hello,

    Using JAI I'm writing a servlet to decode a transparent GIF and write
    it to the output stream as an encoded JPEG. I can't quite get it to
    work. I either get a broken image or a solid color. Below is my source
    code. Any help would be greatly appriciated.

    Thanks

    John P.

    package leads;

    import java.io.*;
    import javax.servlet.*;
    import javax.servlet.http.*;
    import java.util.*;
    import java.sql.*;
    import oracle.sql.*;
    import java.awt.*;
    import java.awt.image.*;
    import java.awt.color.*;
    import java.awt.image.renderable.*;
    import com.sun.media.jai.codec.*;
    import javax.media.jai.*;
    import javax.media.jai.operator.*;

    public class GetImageConveter extends HttpServlet
    {
    public void doGet(HttpServletRequest req, HttpServletResponse resp)
    throws ServletException, IOException
    {
    System.out.println("YOU GOT HERE - VERY BEGINNING");
    Statement stmt = null;
    ResultSet result = null;
    String id = req.getParameter("id");
    System.out.println("YOU GOT HERE - ID: " + id);
    if (id == null || id.trim().length() == 0)
    return;

    String query = "select img_blob from lds_image where id = " + id;
    Connection conn = LeadsUtil.getConnection();
    InputStream is = null;

    try
    {
    stmt = conn.createStatement();
    result = stmt.executeQuery(query);
    if (!result.next())
    throw new Exception("Debug: cannot not fetch image from db");

    resp.setContentType("image/jpeg");
    is = result.getBinaryStream(1);
    System.out.println("YOU GOT HERE 1");
    ForwardSeekableStream Fstream = new ForwardSeekableStream(is);
    String[] names = ImageCodec.getDecoderNames(Fstream);

    for(int i = 0; i < names.length; i++){
    System.out.println("STREAM TYPE: " + names);
    }

    ImageEncoder imgEnc = null;
    ImageDecoder decoder =
    ImageCodec.createImageDecoder("GIF",is,null);
    RenderedImage img = decoder.decodeAsRenderedImage();
    imgEnc = ImageCodec.createImageEncoder("JPEG",
    resp.getOutputStream(), null);

    if(img.getColorModel().hasAlpha() == true){
    Raster img_raster = decoder.decodeAsRaster();
    int height = img_raster.getHeight();
    int width = img_raster.getWidth();
    Rectangle img_rect = new Rectangle(0, 0, width, height);
    WritableRaster writable_raster =
    img_raster.createCompatibleWritableRaster();
    BufferedImage bi = new BufferedImage(img.getColorModel(),
    writable_raster, false, null);
    int counter = 0;
    for(int a = 0; a < width; a++){
    for(int b = 0; b < height; b++){
    int rgba = bi.getRGB(a, b);
    int red = (rgba >> 16) & 0xff;
    int green = (rgba >> 8) & 0xff;
    int blue = rgba & 0xff;
    int alpha = (rgba >> 24) & 0xff;
    bi.setRGB(a,b,((red << 16) | (green << 8) | blue));
    counter++;
    }
    }
    System.out.println("Pixel Counter: " +
    Integer.toString(counter));
    System.out.println("Second Branch");
    width = bi.getWidth();
    height = bi.getHeight();
    // BufferedImage rgbImage = new BufferedImage(width,
    height, BufferedImage.TYPE_INT_RGB);
    BufferedImage rgbImage = new BufferedImage(width, height,
    BufferedImage.TYPE_INT_RGB);
    Graphics2D g = rgbImage.createGraphics();
    g.drawImage(bi, 0, 0, width, height, Color.white, null);
    // g.drawImage(bi, null, 0, 0);
    g.dispose();
    bi = rgbImage;
    RenderedOp op = FormatDescriptor.create(bi, new
    Integer(DataBuffer.TYPE_BYTE), null);
    imgEnc.encode(op);
    System.out.println("YOU GOT HERE 2");
    }
    else{
    System.out.println("Second Branch");
    imgEnc.encode(img);
    }


    }
    catch (Exception e)
    {
    System.out.println("Debug: cannot get image id = " + id + " " +
    e.toString());
    }
    finally{
    try{
    if(result != null)
    result.close();
    if(stmt != null)
    stmt.close();
    }
    catch(SQLException e)
    {
    System.out.println("Debug: cannot close result set/
    statement: " + e.toString());
    }
    LeadsUtil.freeConnection(conn);
    }
    }

    }
    , Apr 29, 2005
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Minhua Fu

    How to draw a transparent gif in web

    Minhua Fu, Dec 30, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    2,027
    George Ter-Saakov
    Dec 30, 2003
  2. =?Utf-8?B?Um9iZXJ0?=
    Replies:
    0
    Views:
    445
    =?Utf-8?B?Um9iZXJ0?=
    Apr 6, 2004
  3. tshad

    Transparent Gif not working in IE

    tshad, Oct 29, 2004, in forum: ASP .Net
    Replies:
    4
    Views:
    6,386
    =?Utf-8?B?T2xkZ3V5Mg==?=
    Jan 26, 2005
  4. =?Utf-8?B?TWVrZW4=?=

    Resize transparent gif images

    =?Utf-8?B?TWVrZW4=?=, Jul 20, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    598
    =?Utf-8?B?TWVrZW4=?=
    Jul 20, 2005
  5. Converting Gif to JPEG

    , Jul 5, 2006, in forum: C Programming
    Replies:
    2
    Views:
    273
    Roberto Waltman
    Jul 5, 2006
Loading...

Share This Page