package webService; import java.awt.*; import java.awt.image.*; import java.io.*; import java.util.ResourceBundle; import com.sun.image.codec.jpeg.*; import javax.imageio.ImageIO; import jmatlink.JMatLink; import org.apache.axis.encoding.Base64; public class goOnlineWebService { // Suitable for Document/Literal (Wrapped) Generation // Java -> Document/Literal (Wrapped) // C# -> RPC/Encoded public static String Code_Dir; public static String Image_Dir; public static JMatLink jMat; public void engineOpen() { ResourceBundle rb = ResourceBundle.getBundle("MSP"); Code_Dir = rb.getString("Code_directory"); Image_Dir = rb.getString("Image_directory"); jMat = new JMatLink(); jMat.engOpenSingleUse(); jMat.engEvalString("cd "+ Code_Dir); System.out.println("MATLAB Server Pages Engine"); } public void engineClose() { jMat.engClose(); jMat.kill(); } public String thumbnail(String imagename, String width, String height) { // Written by Marco Schmidt // Changed for MSP by Ali KIZIL try { // load image from INFILE Image image = Toolkit.getDefaultToolkit().getImage( Image_Dir + "\\" + imagename + ".jpg"); MediaTracker mediaTracker = new MediaTracker(new Container()); mediaTracker.addImage(image, 0); mediaTracker.waitForID(0); // determine thumbnail size from WIDTH and HEIGHT int thumbWidth = Integer.parseInt(width); int thumbHeight = Integer.parseInt(height); double thumbRatio = (double) thumbWidth / (double) thumbHeight; int imageWidth = image.getWidth(null); int imageHeight = image.getHeight(null); double imageRatio = (double) imageWidth / (double) imageHeight; if (thumbRatio < imageRatio) { thumbHeight = (int) (thumbWidth / imageRatio); } else { thumbWidth = (int) (thumbHeight * imageRatio); } // draw original image to thumbnail image object and // scale it to the new size on-the-fly BufferedImage thumbImage = new BufferedImage(thumbWidth, thumbHeight, BufferedImage.TYPE_INT_RGB); Graphics2D graphics2D = thumbImage.createGraphics(); graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); graphics2D.drawImage(image, 0, 0, thumbWidth, thumbHeight, null); // save thumbnail image to OUTFILE BufferedOutputStream out = new BufferedOutputStream( new FileOutputStream(Image_Dir + "\\" + imagename + "_tb.jpg")); JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); JPEGEncodeParam param = encoder .getDefaultJPEGEncodeParam(thumbImage); int quality = Integer.parseInt("100"); quality = Math.max(0, Math.min(quality, 100)); param.setQuality((float) quality / 100.0f, false); encoder.setJPEGEncodeParam(param); encoder.encode(thumbImage); out.close(); } catch (NumberFormatException e) { System.out.println("MSP Error: "+e.getMessage()); } catch (FileNotFoundException e) { System.out.println("MSP Error: "+e.getMessage()); } catch (ImageFormatException e) { System.out.println("MSP Error: "+e.getMessage()); } catch (InterruptedException e) { System.out.println("MSP Error: "+e.getMessage()); } catch (IOException e) { System.out.println("MSP Error: "+e.getMessage()); } return (imagename + "_tb.jpg"); } public String imageToString(String filename) { // Using Base64 Encoding to serialize image. String strPic = null; try { BufferedImage srcImage = ImageIO.read( new File(filename)); ByteArrayOutputStream streamIn = new ByteArrayOutputStream(); ImageIO.write( srcImage , "jpg", streamIn ); byte[] bytes = streamIn.toByteArray(); strPic = Base64.encode(bytes); } catch (IOException e) { System.out.println("MSP Error: "+e.getMessage()); } //ByteArrayInputStream streamOut = new ByteArrayInputStream( bytes ); //BufferedImage writeImage = ImageIO.read( streamOut ); //ImageIO.write( writeImage, "jpg", new File( pathOut )); return strPic; } public void eval(String command){ jMat.engEvalString(command); } public double[][] getArray(String var) { double[][] array; array = jMat.engGetArray(var); return array; } public double[] getVector(String var) { double[][] array; double[] vector; array = jMat.engGetArray(var); vector = array[0]; return vector; } public double getScalar(String var) { double scalar; scalar = jMat.engGetScalar(var); return scalar; } public String[] getString(String var) { String[] str; str = jMat.engGetCharArray(var); return str; } public void putArray(String var, double[][] value) { jMat.engPutArray(var,value); } public void putVector(String var, double[] value) { jMat.engPutArray(var,value); } public void putScalar(String var, double value) { jMat.engPutArray(var,value); } public String getImage(String command, String name, String width, String height) { jMat.engEvalString(command); jMat.engEvalString("saveas(gcf,'"+name+"','jpg')"); jMat.engEvalString("movefile('"+name+".jpg','"+Image_Dir+"','f')"); thumbnail(name,width,height); return imageToString(Image_Dir + "\\" + name + "_tb.jpg"); } }