Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Converting transparent GIF to JPEG via servlet

Reply
Thread Tools

Converting transparent GIF to JPEG via servlet

 
 
john_pallozzi@itworld.com
Guest
Posts: n/a
 
      04-29-2005
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[i]);
}

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 >> & 0xff;
int blue = rgba & 0xff;
int alpha = (rgba >> 24) & 0xff;
bi.setRGB(a,b,((red << 16) | (green << | 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);
}
}

}

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Converting Gif to JPEG marydeepthy@gmail.com C Programming 2 07-05-2006 01:13 PM
Transparent Gif not working in IE tshad ASP .Net 4 01-26-2005 10:59 PM
Making Background of an image control transparent to view designed Gif =?Utf-8?B?Um9iZXJ0?= ASP .Net 0 04-06-2004 12:21 PM
Servlet question(Tomcat, web.xml, servlet-class, servlet-name) circuit_breaker Java 2 04-04-2004 03:26 AM
How to draw a transparent gif in web Minhua Fu ASP .Net 1 12-30-2003 06:16 PM



Advertisments