J
john_pallozzi
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);
}
}
}
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);
}
}
}