package iu.ce.bean;
import iu.ce.service.FileProcessiongService;
import iu.ce.utils.Util;
import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.RenderingHints;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.ejb.Remove;
import javax.ejb.Stateful;
import javax.imageio.ImageIO;
import org.jboss.seam.annotations.Begin;
import org.jboss.seam.annotations.Destroy;
import org.jboss.seam.annotations.End;
import org.jboss.seam.annotations.Name;
@Stateful
@Name("fileProcessiongServiceBean")
public class FileProcessiongServiceBean implements FileProcessiongService {
@End
public boolean writeFile(String filepath, InputStream is) {
try {
FileOutputStream fos = null;
fos = new FileOutputStream(filepath);
int read = 0;
byte[] buf = new byte[256];
while ((read = is.read(buf)) > 0) {
fos.write(buf, 0, read);
}
fos.close();
return true;
} catch (FileNotFoundException e) {
e.printStackTrace();
return false;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
@End
public boolean processImage(String filepath, String thumbnailpath,
int width, int height, InputStream is) {
File file = new File(filepath);
File out = new File(thumbnailpath);
BufferedImage image = null;
try {
GraphicsConfiguration gc = getDefaultConfiguration();
image = toCompatibleImage(ImageIO.read(is), gc);
int w = 0;
int h = 0;
if (width == 0 && height == 0) {
w = image.getWidth();
h = image.getHeight();
} else if (width != 0 && height != 0) {
w = width;
h = height;
} else {
if (height == 0) {
h = (width * image.getHeight()) / image.getWidth();
w = width;
} else {
w = (image.getWidth() * height) / image.getHeight();
h = height;
}
}
BufferedImage resize = getScaledInstance(image, w, h, gc);
String suffix = Util.getFileSuffix(filepath).toLowerCase();
try {
if (suffix.equals("gif")) {
ImageIO.write(resize, "png", out);
} else {
//ImageIO.write(resize, "png", out);
ImageIO.write(resize, suffix, out);
}
ImageIO.write(image, suffix, file);
return true;
} catch (IOException e) {
e.printStackTrace();
return false;
}
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
@End
public boolean processImage(String path, int width, int height,
InputStream is) {
File out = new File(path);
BufferedImage image = null;
try {
GraphicsConfiguration gc = getDefaultConfiguration();
image = toCompatibleImage(ImageIO.read(is), gc);
int w = 0;
int h = 0;
if (width == 0 && height == 0) {
w = image.getWidth();
h = image.getHeight();
} else if (width != 0 && height != 0) {
w = width;
h = height;
} else {
if (height == 0) {
h = (width * image.getHeight()) / image.getWidth();
w = width;
} else {
w = (image.getWidth() * height) / image.getHeight();
h = height;
}
}
BufferedImage resize = getScaledInstance(image, w, h, gc);
String suffix = Util.getFileSuffix(path).toLowerCase();
try {
if (suffix.equals("gif")) {
ImageIO.write(resize, "png", out);
} else {
//ImageIO.write(resize, "png", out);
ImageIO.write(resize, suffix, out);
}
return true;
} catch (IOException e) {
e.printStackTrace();
return false;
}
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
public GraphicsConfiguration getDefaultConfiguration() {
GraphicsEnvironment ge = GraphicsEnvironment
.getLocalGraphicsEnvironment();
GraphicsDevice gd = ge.getDefaultScreenDevice();
return gd.getDefaultConfiguration();
}
public BufferedImage toCompatibleImage(BufferedImage image,
GraphicsConfiguration gc) {
if (gc == null)
gc = getDefaultConfiguration();
int w = image.getWidth();
int h = image.getHeight();
int transparency = image.getColorModel().getTransparency();
BufferedImage result = gc.createCompatibleImage(w, h, transparency);
Graphics2D g2 = result.createGraphics();
g2.drawRenderedImage(image, null);
g2.dispose();
return result;
}
public BufferedImage copy(BufferedImage source, BufferedImage target) {
Graphics2D g2 = target.createGraphics();
g2.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING,
RenderingHints.VALUE_COLOR_RENDER_SPEED);
double scalex = (double) target.getWidth() / source.getWidth();
double scaley = (double) target.getHeight() / source.getHeight();
AffineTransform xform = AffineTransform
.getScaleInstance(scalex, scaley);
g2.drawRenderedImage(source, xform);
g2.dispose();
return target;
}
public BufferedImage getScaledInstance(BufferedImage image, int width,
int height, GraphicsConfiguration gc) {
if (gc == null)
gc = getDefaultConfiguration();
int transparency = image.getColorModel().getTransparency();
return copy(image, gc
.createCompatibleImage(width, height, transparency));
}
@Begin
public void begin() {
}
@Destroy
@Remove
public void destroy() {
}
}
N/A
|