Content type

Hi ,I do have a image as BLOB in db , when i retrieve it in to byte[] is there any way to get the content type of the image from it's header, can any one help me out ?Regards ,AK
[207 byte] By [scs_aka] at [2007-11-26 19:54:01]
# 1

BufferedImage captureBuff = null;

...

// The user moves a rectangle over an image on the screen to select the area to save

captureBuff = null;

Robot robot = new Robot();

// get upper left point of rectangle

Point p = rect.getLocation();

SwingUtilities.convertPointToScreen(p, panel);

// I know the rectangle size is fixed at 100,127

Rectangle actualPhotoArea = new Rectangle(p.x + 1, p.y + 1, 100, 127);

// capture screen image

captureBuff = robot.createScreenCapture(actualPhotoArea);

// convert to jpeg and then a byte array

byte[] byteImage = toByteArray(100, 127, captureBuff);

// setup stream for blob creation

ByteArrayInputStream inStream = new ByteArrayInputStream(byteImage);

// open connection

Connection photoConnection = startup.openSQLConnection(SQLDriver, SQLConnectionString);

// simply delete existing record if it exists otherwise must check if exists and update

String sqlStatement = "delete from employeephoto where Employee_ID = '"+employeeID+"'";

PreparedStatement ps = photoConnection.prepareStatement(sqlStatement);

ps.executeUpdate();

// create insert statement string

sqlStatement = "insert into employeephoto (Employee_ID,Binary_Photo) values ('"+employeeID+"', ?)";

// create prepared statement

ps = photoConnection.prepareStatement(sqlStatement);

// set up input stream

ps.setBinaryStream(1,inStream,inStream.available());

// execute statement

ps.executeUpdate();

// close connection

startup.closeSQLConnection(photoConnection);

captureBuff = null;

Convert captured screen image to jpeg byte array used above:

public byte[] toByteArray(int width, int height, BufferedImage imageBuff) throws java.io.IOException{

BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

bi.getGraphics().drawImage(imageBuff, 0, 0, null);

ByteArrayOutputStream out = new ByteArrayOutputStream();

JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);

JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bi);

param.setQuality(0.75f, false);

encoder.setJPEGEncodeParam(param);

encoder.encode(bi);

return out.toByteArray();

}

Retrieving the saved image:

ImageIcon dPhoto = null;

// open connection

Connection photoConnection = startup.openSQLConnection(SQLDriver, SQLConnectionString);

// setup select string

String sqlStatement = "select Binary_Photo from employeephoto where Employee_ID = '"+employeeID+"'";

try {

// perform select

ResultSet rs = photoConnection.createStatement().executeQuery(sqlStatement);

// if record found process blob

if (rs.next()) {

// get blob

Blob image = rs.getBlob("Binary_Photo");

// setup the streams to process blob

InputStream input = image.getBinaryStream();

ByteArrayOutputStream output = new ByteArrayOutputStream();

// set read buffer size

byte[] rb = new byte[1024];

int ch = 0;

// process blob

while ((ch=input.read(rb)) != -1) {

output.write(rb, 0, ch);

}

// transfer to byte buffer

byte[] b = output.toByteArray();

input.close();

output.close();

// load final buffer to image icon

dPhoto = new ImageIcon(b);

}

}

catch (Exception exc) {// do your exception processing}

// close connection

startup.closeSQLConnection(photoConnection);

// dPhoto holds image or is null at this point.

csatheesha at 2007-7-9 22:46:20 > top of Java-index,Java Essentials,Java Programming...