001: package org.claros.intouch.webdisk.controllers;
002:
003: import java.awt.Container;
004: import java.awt.Graphics2D;
005: import java.awt.Image;
006: import java.awt.MediaTracker;
007: import java.awt.Toolkit;
008: import java.awt.image.BufferedImage;
009: import java.io.BufferedInputStream;
010: import java.io.ByteArrayOutputStream;
011: import java.io.FileInputStream;
012: import java.io.FileNotFoundException;
013: import java.io.IOException;
014: import java.util.StringTokenizer;
015:
016: import org.claros.intouch.webdisk.models.Thumbnail;
017:
018: public class ImageController {
019:
020: /**
021: * Herhangi bir url'deki resmi okur ve image objesi olarak döner.
022: *
023: * @return
024: * @throws Exception
025: */
026: private static BufferedImage getImage(String path) throws Exception {
027: Toolkit toolkit = Toolkit.getDefaultToolkit();
028: Image image = toolkit.createImage(path);
029: MediaTracker mediaTracker = new MediaTracker(new Container());
030: mediaTracker.addImage(image, 0);
031: mediaTracker.waitForID(0);
032:
033: BufferedImage img = new BufferedImage(image.getWidth(null),
034: image.getHeight(null), BufferedImage.TYPE_INT_RGB);
035: Graphics2D graphics2D = img.createGraphics();
036: graphics2D.drawImage(image, 0, 0, image.getWidth(null), image
037: .getHeight(null), null);
038: try {
039: graphics2D.dispose();
040: } catch (Exception e) {
041: // Do nothing sier
042: }
043: return img;
044: }
045:
046: /**
047: *
048: * @param newWidth
049: * @param newHeight
050: * @param myObjWidth
051: * @param myObjHeight
052: * @param destObjWidth
053: * @param destObjHeight
054: */
055: private static String calculateRatio(int myObjWidth,
056: int myObjHeight, int destObjWidth, int destObjHeight) {
057: double widthRatio = (double) myObjWidth / (double) destObjWidth; // 2.2846
058: double heightRatio = (double) myObjHeight
059: / (double) destObjHeight; // 2.02
060:
061: double ratio = widthRatio;
062: if (heightRatio > widthRatio) {
063: ratio = heightRatio;
064: }
065: int newWidth = (int) (myObjWidth / ratio); // 151
066: int newHeight = (int) (myObjHeight / ratio); // 67
067: return newWidth + "_" + newHeight;
068: }
069:
070: /**
071: *
072: * @return
073: */
074: private static byte[] getCustomThumbImgBytes(String fileName,
075: int width, int height) {
076: byte[] bytesOut = null;
077: try {
078: Thumbnail th = new Thumbnail();
079: bytesOut = th.getThumb(fileName, width, height);
080: } catch (IOException e) {
081: e.printStackTrace();
082: } catch (Exception e) {
083: e.printStackTrace();
084: }
085: return bytesOut;
086: }
087:
088: /**
089: *
090: * @return
091: */
092: public static byte[] getImgBytes(String path) {
093: ByteArrayOutputStream baos = new ByteArrayOutputStream();
094: try {
095: BufferedInputStream is = new BufferedInputStream(
096: new FileInputStream(path));
097: int byte_;
098: while ((byte_ = is.read()) != -1) {
099: baos.write(byte_);
100: }
101: is.close();
102: baos.close();
103: } catch (FileNotFoundException e) {
104: e.printStackTrace();
105: } catch (IOException e) {
106: e.printStackTrace();
107: }
108: return baos.toByteArray();
109: }
110:
111: /**
112: *
113: * @param width
114: * @param height
115: * @return
116: */
117: public static byte[] getCustomImgBytes(String path, int width,
118: int height) {
119: byte[] bytesOut = null;
120: try {
121: BufferedImage img = getImage(path);
122: int w = img.getWidth();
123: int h = img.getHeight();
124: StringTokenizer token = new StringTokenizer(ImageController
125: .calculateRatio(w, h, width, height), "_");
126: bytesOut = ImageController.getCustomThumbImgBytes(path,
127: Integer.parseInt(token.nextToken()), Integer
128: .parseInt(token.nextToken()));
129: } catch (Exception e) {
130: e.printStackTrace();
131: }
132: return bytesOut;
133: }
134: }
|