package com.sony.vaiogate.ads.common.util;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
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.ImageReadParam;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;
import javax.imageio.stream.ImageOutputStream;
public class ImageTools {
/**cut out picture*/
public static File cutting(String path,File file,int x,int y,int width,int height){
try{
String endName=file.getName();
endName=endName.substring(endName.lastIndexOf(".")+1);
Iterator<ImageReader> readers=ImageIO.getImageReadersByFormatName(endName);
ImageReader reader = (ImageReader)readers.next();
InputStream is=new FileInputStream(file);
ImageInputStream iis = ImageIO.createImageInputStream(is);
reader.setInput(iis,true);
ImageReadParam param = reader.getDefaultReadParam();
Rectangle rect = new Rectangle(x,y,width,height);
param.setSourceRegion(rect);
BufferedImage bi = reader.read(0,param);
File newFile=new File(path+"c_"+file.getName());
//if(!newFile.exists())newFile.mkdirs();
ImageOutputStream out=ImageIO.createImageOutputStream(new FileOutputStream(newFile));
ImageIO.write(bi,endName,out);
file=newFile;
}catch(Exception e){
e.printStackTrace();
}
return file;
}
public static String CutOutPicture(String path,String pictureName,int previewWidth,int prebiweHeight){
BufferedImage image = null;
try {
File file=new File(path+pictureName);
image = ImageIO.read(file);
int pictureWidth = image.getWidth();// width
int pictureHeight = image.getHeight();// height
int cutWidth = 0; //cut width
int cutHeight = 0;//cut height
int xWidth = 0; //start cut x
int yHeight = 0; //start cut y
if((float)pictureWidth/(float)pictureHeight > (float)previewWidth/(float)prebiweHeight){
cutWidth = (int)((float)pictureHeight * (float)previewWidth / (float)prebiweHeight);
xWidth = (pictureWidth-cutWidth)/2;
cutHeight = pictureHeight;
}
if((float)pictureWidth/(float)pictureHeight < (float)previewWidth/(float)prebiweHeight){
cutHeight = (int)((float)pictureWidth * (float)prebiweHeight / (float)previewWidth);
yHeight = (pictureHeight-cutHeight)/2;
cutWidth = pictureWidth;
}
System.out.println("pictureWidth=" + pictureWidth);
System.out.println("pictureHeight=" + pictureHeight);
System.out.println("xWidth=" + xWidth);
System.out.println("yHeight=" + yHeight);
System.out.println("cutWidth=" + cutWidth);
System.out.println("cutHeight=" + cutHeight);
ImageTools.cutting(path,file,xWidth,yHeight,cutWidth,cutHeight);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static void main(String args[]){
String pictureName = "xx1.jpg";
String path = "D:/";
int previewWidth = 114; //previewWidth
int prebiweHeight = 64; //prebiweHeight
ImageTools.CutOutPicture(path,pictureName,previewWidth,prebiweHeight);
}
/*
//Draw picture.
{
int iWidth = m_pBitmap->GetWidth();
int iHeight = m_pBitmap->GetHeight();
CRect rct(0, 0, iWidth, iHeight);
if ((float)rct.Width() / (float)rct.Height() > (float)m_rctClient.Width() / (float)m_rctClient.Height())
{
iWidth = (int)((float)iHeight * (float) m_rctClient.Width() / (float)m_rctClient.Height());
int iMargin = (rct.Width() - iWidth) / 2;
rct.left += iMargin;
rct.right -= iMargin;
}
if ((float)rct.Width() / (float)rct.Height() < (float)m_rctClient.Width() / (float)m_rctClient.Height())
{
iHeight = (int)((float)iWidth * (float)m_rctClient.Height() / (float)m_rctClient.Width());
int iMargin = (rct.Height() - iHeight) / 2;
rct.top += iMargin;
rct.bottom -= iMargin;
}
pRT->DrawBitmap(m_pBitmap, GetRect_2(m_rctClient), GetRect_2(rct), 1.0F, FALSE);
}
*/
}