注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

vingo

日月

 
 
 

日志

 
 
 
 

多个图片合并成九宫格 腾讯优云  

2015-10-12 15:55:25|  分类: 默认分类 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

public static String[] getXy(int size){
String[] s = new String[size>9?9:size];
int _x = 0;
int _y = 0;
if(size ==1){
_x = _y = 6;
s[0] = "6,6";
}
if(size == 2){
_x =_y = 4;
s[0] = "4,"+(132/2-60/2);
s[1] = 60+2*_x+","+ (132/2-60/2);
}
if(size == 3){
_x=_y =4;
s[0] = (132/2-60/2)+","+_y;
s[1] = _x+","+(60+2*_y);
s[2] = (60+2*_y)+","+(60+2*_y);
}
if(size ==4){
_x=_y =4;
s[0] = _x+","+_y;
s[1] = (_x*2 + 60)+","+_y;
s[2] = _x+","+(60+2*_y);
s[3] = (60+2*_y)+","+(60+2*_y);
}
if(size == 5){
_x = _y = 3;
s[0] = (132-40*2-_x)/2+","+(132-40*2-_y)/2;
s[1] = ((132-40*2-_x)/2+40+_x)+","+(132-40*2-_y)/2;
s[2] = _x+","+((132-40*2-_x)/2+40+_y);
s[3] = (_x*2+40)+","+((132-40*2-_x)/2+40+_y);
s[4] = (_x*3+40*2)+","+((132-40*2-_x)/2+40+_y);
}
if(size == 6){
_x = _y = 3;
s[0] = _x+","+((132-40*2-_x)/2);
s[1] = (_x*2+40)+","+((132-40*2-_x)/2);
s[2] = (_x*3+40*2)+","+((132-40*2-_x)/2);
s[3] = _x+","+((132-40*2-_x)/2+40+_y);
s[4] = (_x*2+40)+","+((132-40*2-_x)/2+40+_y);
s[5] = (_x*3+40*2)+","+((132-40*2-_x)/2+40+_y);
}
if(size == 7){
_x=_y =3;
s[0] = (132-40)/2+","+_y;
s[1] = _x+","+(_y*2+40);
s[2] = (_x*2+40)+","+(_y*2+40);
s[3] = (_x*3+40*2)+","+(_y*2+40);
s[4] = _x+","+(_y*3+40*2);
s[5] = (_x*2+40)+","+(_y*3+40*2);
s[6] = (_x*3+40*2)+","+(_y*3+40*2);
}
if(size == 8){
_x=_y =3;
s[0] = (132-80-_x)/2+","+_y;
s[1] = ((132-80-_x)/2+_x+40)+","+_y;
s[2] = _x+","+(_y*2+40);
s[3] = (_x*2+40)+","+(_y*2+40);
s[4] = (_x*3+40*2)+","+(_y*2+40);
s[5] = _x+","+(_y*3+40*2);
s[6] = (_x*2+40)+","+(_y*3+40*2);
s[7] = (_x*3+40*2)+","+(_y*3+40*2);
}
if(size == 9){
_x=_y = 3;
s[0]=_x+","+_y;
s[1] = _x*2+40+","+_y;
s[2] = _x*3+40*2 +","+_y;
s[3] = _x+","+(_y*2+40);
s[4] = (_x*2+40)+","+(_y*2+40);
s[5] = (_x*3+40*2)+","+(_y*2+40);
s[6] = _x+","+(_y*3+40*2);
s[7] = (_x*2+40)+","+(_y*3+40*2);
s[8] = (_x*3+40*2)+","+(_y*3+40*2);
}
return s;
}

public static int getWidth(int size){
int width = 0;
if(size == 1){
width = 120;
}
if(size>1 && size<=4){
width = 60;
}
if(size>=5){
width = 40;
}
return width;
}

public static void download(String urlString, String filename,String savePath) throws Exception {
// 构造URL
URL url = new URL(urlString);
// 打开连接
URLConnection con = url.openConnection();
//设置请求超时为5s
con.setConnectTimeout(5*1000);
// 输入流
InputStream is = con.getInputStream();

// 1K的数据缓冲
byte[] bs = new byte[1024];
// 读取到的数据长度
int len;
// 输出的文件流
File sf=new File(savePath);
if(!sf.exists()){
sf.mkdirs();
}
OutputStream os = new FileOutputStream(sf.getPath()+File.separator+filename);
// 开始读取
while ((len = is.read(bs)) != -1) {
os.write(bs, 0, len);
}
// 完毕,关闭所有链接
os.close();
is.close();
}

public static String zoom(String sourcePath,String targetPath,int width,int height) throws IOException{
File imageFile = new File(sourcePath);
if(!imageFile.exists()){
throw new IOException("Not found the images:"+sourcePath);
}
if(targetPath==null || targetPath.isEmpty()) targetPath = sourcePath;
String format = sourcePath.substring(sourcePath.lastIndexOf(".")+1,sourcePath.length());
BufferedImage image = ImageIO.read(imageFile);
image = zoom(image,width,height);
ImageIO.write(image, format, new File(targetPath));
return targetPath;
}

private static BufferedImage zoom(BufferedImage sourceImage , int width , int height){
BufferedImage zoomImage = new BufferedImage(width, height, sourceImage.getType());
Image image = sourceImage.getScaledInstance(width, height, Image.SCALE_SMOOTH);
Graphics gc = zoomImage.getGraphics();
gc.setColor(Color.WHITE);
gc.drawImage( image , 0, 0, null);
return zoomImage;
}

public static void createImage(File[] files,String outPath) throws Exception{
String[] imageSize = getXy(files.length);
int width = getWidth(files.length>9?9:files.length);
BufferedImage ImageNew = new BufferedImage(132,132,BufferedImage.TYPE_INT_RGB);
//设置背景为白色
for(int m=0;m<132;m++){
for(int n=0;n<132;n++){
ImageNew.setRGB(m, n, 0xFFFFFF);
}
}
for(int i=0;i<imageSize.length;i++){
String size = imageSize[i];
String[] sizeArr = size.split(",");
int x = Integer.valueOf(sizeArr[0]);
int y = Integer.valueOf(sizeArr[1]);
String f = zoom(files[i].getPath(),SystemConfig.getFileFsBase()+File.separator+"group"+File.separator+"temp",width,width);
File fileOne = new File(f);
BufferedImage ImageOne = ImageIO.read(fileOne);

//从图片中读取RGB
int[] ImageArrayOne = new int[width*width];
ImageArrayOne = ImageOne.getRGB(0,0,width,width,ImageArrayOne,0,width);
ImageNew.setRGB(x,y,width,width,ImageArrayOne,0,width);//设置左半部分的RGB
}
File outFile = new File(outPath);
ImageIO.write(ImageNew, "png", outFile);//写图片
}

public static void main(String[] args) throws Exception{

File f = new File("D:\\group");
File[] fArr = f.listFiles();
createImage(fArr,"D:\\group\\g.png");
}


===============



/**
* 根据图片数量生成坐标
* @param size
* @return
*/
public static String[] getXy(int size){
String[] s = new String[size>9?9:size];
int _x = 0;
int _y = 0;
if(size ==1){
_x = _y = 6;
s[0] = "6,6";
}
else if(size == 2){
_x =_y = 4;
s[0] = "4,"+(132/2-60/2);
s[1] = 60+2*_x+","+ (132/2-60/2);
}
else if(size == 3){
_x=_y =4;
s[0] = (132/2-60/2)+","+_y;
s[1] = _x+","+(60+2*_y);
s[2] = (60+2*_y)+","+(60+2*_y);
}
else if(size ==4){
_x=_y =4;
s[0] = _x+","+_y;
s[1] = (_x*2 + 60)+","+_y;
s[2] = _x+","+(60+2*_y);
s[3] = (60+2*_y)+","+(60+2*_y);
}
else if(size == 5){
_x = _y = 3;
s[0] = (132-40*2-_x)/2+","+(132-40*2-_y)/2;
s[1] = ((132-40*2-_x)/2+40+_x)+","+(132-40*2-_y)/2;
s[2] = _x+","+((132-40*2-_x)/2+40+_y);
s[3] = (_x*2+40)+","+((132-40*2-_x)/2+40+_y);
s[4] = (_x*3+40*2)+","+((132-40*2-_x)/2+40+_y);
}
else if(size == 6){
_x = _y = 3;
s[0] = _x+","+((132-40*2-_x)/2);
s[1] = (_x*2+40)+","+((132-40*2-_x)/2);
s[2] = (_x*3+40*2)+","+((132-40*2-_x)/2);
s[3] = _x+","+((132-40*2-_x)/2+40+_y);
s[4] = (_x*2+40)+","+((132-40*2-_x)/2+40+_y);
s[5] = (_x*3+40*2)+","+((132-40*2-_x)/2+40+_y);
}
else if(size == 7){
_x=_y =3;
s[0] = (132-40)/2+","+_y;
s[1] = _x+","+(_y*2+40);
s[2] = (_x*2+40)+","+(_y*2+40);
s[3] = (_x*3+40*2)+","+(_y*2+40);
s[4] = _x+","+(_y*3+40*2);
s[5] = (_x*2+40)+","+(_y*3+40*2);
s[6] = (_x*3+40*2)+","+(_y*3+40*2);
}
else if(size == 8){
_x=_y =3;
s[0] = (132-80-_x)/2+","+_y;
s[1] = ((132-80-_x)/2+_x+40)+","+_y;
s[2] = _x+","+(_y*2+40);
s[3] = (_x*2+40)+","+(_y*2+40);
s[4] = (_x*3+40*2)+","+(_y*2+40);
s[5] = _x+","+(_y*3+40*2);
s[6] = (_x*2+40)+","+(_y*3+40*2);
s[7] = (_x*3+40*2)+","+(_y*3+40*2);
}
else if(size >= 9){
_x=_y = 3;
s[0]=_x+","+_y;
s[1] = _x*2+40+","+_y;
s[2] = _x*3+40*2 +","+_y;
s[3] = _x+","+(_y*2+40);
s[4] = (_x*2+40)+","+(_y*2+40);
s[5] = (_x*3+40*2)+","+(_y*2+40);
s[6] = _x+","+(_y*3+40*2);
s[7] = (_x*2+40)+","+(_y*3+40*2);
s[8] = (_x*3+40*2)+","+(_y*3+40*2);
}
return s;
}

/**
* 获取图片大小
* @param size
* @return
*/
public static int getWidth(int size){
int width = 0;
if(size == 1){
width = 120;
}
if(size>1 && size<=4){
width = 60;
}
if(size>=5){
width = 40;
}
return width;
}


/**
* 图片拼接
* @param sourceUrl
* @param width
* @param height
* @return
* @throws IOException
*/
public static BufferedImage zoom(String sourceUrl,int width,int height) throws IOException {
String urlString = "http://"+HabitPicKey.DOMAIN + "/" + sourceUrl+"/small";
URL url = new URL(urlString);
int num = urlString.lastIndexOf('/');
String u = urlString.substring(0,num);
URLConnection connection = url.openConnection();
connection.setDoOutput(true);
connection.setRequestProperty("referer", u); //通过这个http头的伪装来反盗链
BufferedImage image = ImageIO.read(connection.getInputStream());
image = zoom(image,width,height);
return image;
}

/**
* 图片流
* @param sourceImage
* @param width
* @param height
* @return
*/
private static BufferedImage zoom(BufferedImage sourceImage , int width , int height){
BufferedImage zoomImage = new BufferedImage(width, height, sourceImage.getType());
Image image = sourceImage.getScaledInstance(width, height, Image.SCALE_SMOOTH);
Graphics gc = zoomImage.getGraphics();
gc.setColor(Color.WHITE);
gc.drawImage( image , 0, 0, null);
return zoomImage;
}

/**
* 主方法,创建图片
* @param files
* @throws Exception
*/
public static String createImage(String[] files) throws Exception{
String[] imageSize = getXy(files.length);
String qyId = "";
if(imageSize!=null) {
int width = getWidth(files.length>9?9:files.length);
BufferedImage ImageNew = new BufferedImage(132, 132, BufferedImage.TYPE_INT_RGB);
//设置背景为白色
for (int m = 0; m < 132; m++) {
for (int n = 0; n < 132; n++) {
ImageNew.setRGB(m, n, 0xFFFFFF);
}
}
for (int i = 0; i < imageSize.length; i++) {
String size = imageSize[i];
String[] sizeArr = size.split(",");
int x = Integer.valueOf(sizeArr[0]);
int y = Integer.valueOf(sizeArr[1]);
BufferedImage ImageOne = zoom(files[i], width,width);
//从图片中读取RGB
int[] ImageArrayOne = new int[width * width];
ImageArrayOne = ImageOne.getRGB(0, 0, width, width, ImageArrayOne, 0, width);
ImageNew.setRGB(x, y, width, width, ImageArrayOne, 0, width);//设置RGB
}
try {
ImageOutputStream imOut;
ByteArrayOutputStream bs = new ByteArrayOutputStream();
imOut = ImageIO.createImageOutputStream(bs);
ImageIO.write(ImageNew, "png",imOut);
ImageBackup imageBackup = new ImageBackup();
ImageUtil imageUtil = new ImageUtil();
imageBackup = imageUtil.uploadGroup(imageBackup, new ByteArrayInputStream(bs.toByteArray()));
qyId = imageBackup.getPath();

} catch (IOException e) {
e.printStackTrace();
}
}
return qyId;
}

public static void main(String[] args) throws Exception{

String [] images = new String[9];
for(int i=0;i<9;i++)
{
images[i] = "98ac7f85-1d56-4d8c-83d3-fc2e8d65afb0";
}
System.out.println(createImage(images));
}
  评论这张
 
阅读(10)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017