package jp.dip.arimodoki.cntl;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.Iterator;
import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;
import org.apache.commons.codec.binary.Base64;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import jp.dip.arimodoki.common.CConst;
@Scope("prototype")
@Controller
public class ImgConv implements CConst {
static final int IMG_MAX_PIXCEL = 320;
@RequestMapping(value = "/imgconv")
public String imgconv() throws Exception {
return "imgconv";
}
@RequestMapping(value = "/imgconvert")
public String imgconvert(
@RequestParam MultipartFile uploadfile,
Model model
) throws Exception {
String[] tmpfilename = {"c:/temp/" + uploadfile.getName()};
StringBuffer data = new StringBuffer();
InputStream is = uploadfile.getInputStream();
this.compress(is, tmpfilename);
is.close();
is = new FileInputStream( tmpfilename[0] );
ByteArrayOutputStream os = new ByteArrayOutputStream();
byte[] indata = new byte[10240*16];
int siz;
while( ( siz = is.read(indata, 0, indata.length) ) > 0 ) {
os.write( indata, 0, siz );
}
is.close();
String base64 = new String(Base64.encodeBase64(os.toByteArray()), "ASCII");
data.append("data:image/jpeg;base64,");
data.append(base64);
model.addAttribute("base64data", data.toString());
return "imgconv";
}
private void compress(InputStream dataStream, String[] filename) throws Exception {
Exception exception = null;
String target = filename[0];
int img_type = 0;
String type_str = "JPEG";
BufferedImage bimg = null;
BufferedImage tmpImage = null;
int width = 0;
int height = 0;
int z_size = IMG_MAX_PIXCEL;
boolean compress = false;
byte buf[] = new byte[1024*100];
int size;
File outfile = new File(target);
FileOutputStream OutFile = new FileOutputStream( outfile );
while((size = dataStream.read(buf)) > 0) {
OutFile.write( buf, 0, size);
}
OutFile.flush();
OutFile.close();
dataStream.close();
bimg = ImageIO.read( outfile );
width = bimg.getWidth();
height = bimg.getHeight();
FileInputStream inFile = new FileInputStream(outfile);
ImageInputStream imageIn = ImageIO.createImageInputStream(inFile);
ImageReader rd = null;
Iterator<ImageReader> readers = ImageIO.getImageReaders(imageIn);
if (readers.hasNext()) {
rd = readers.next();
type_str = rd.getFormatName().toUpperCase();
logger.log_info(this,"src format=["+rd.getFormatName()+"]:["+target+"]");
rd.dispose();
} else {
logger.log_info(this,"src unknown image format["+target+"]");
}
imageIn.close();
inFile..close();
if(type_str.equals("JPEG")) {
img_type = 0;
target += ".jpg";
}
filename[0] = target;
double ratio = 0;
if ( width > height) {
if(width > z_size) {
compress = true;
}
ratio = (double)z_size / (double)width;
} else {
if(height > z_size) {
compress = true;
}
ratio = (double)z_size / (double)height;
}
OutFile = new FileOutputStream( target );
if(compress == true && img_type==0) {
int shrinkedWidth = (int)(width * ratio);
int shrinkedHeight = (int)(height * ratio);
if( bimg.getType() == 0 ) {
tmpImage = new BufferedImage(shrinkedWidth, shrinkedHeight,
BufferedImage.TYPE_4BYTE_ABGR_PRE);
} else {
tmpImage = new BufferedImage(shrinkedWidth, shrinkedHeight, bimg.getType());
}
Graphics aig = tmpImage.getGraphics();
aig.drawImage(bimg.getScaledInstance(shrinkedWidth, shrinkedHeight, Image.SCALE_SMOOTH), 0, 0, null);
ImageIO.write(tmpImage, "jpeg", OutFile);
OutFile.close();
outfile.delete();
} else {
inFile = new FileInputStream( outfile );
while((size = inFile.read(buf)) > 0) {
OutFile.write( buf, 0, size);
}
inFile.close();
OutFile.close();
outfile.delete();
}
}
}
|