java拼接图片

/**
* 根据群组成员拼接群组头像
* @param user_id
* @param group_name
* @throws IOException
*/
public static void getPicPlus(long user_id, String group_name) throws IOException{
List<Chat> chats = Chat.find("user_id != ? and group_name=?", user_id, group_name).fetch(9);
if(chats != null){
int size = chats.size();
int widthNew = 0;
int heightNew = 0;
List<Integer> widths = new ArrayList<Integer>();
List<Integer> heights = new ArrayList<Integer>();
List<BufferedImage> images = new ArrayList<BufferedImage>();

for(Chat t: chats){
User u  = User.findById(t.user_id);
if(u.photoPath != null){
java.io.File f = new  java.io.File(Blob.getStore(), u.photoPath);
BufferedImage image = ImageIO.read(f);

images.add(image);
widths.add(image.getWidth());
heights.add(image.getHeight());
widthNew = 300;
heightNew = 300;
}
}
BufferedImage imageNew = new BufferedImage(widthNew, heightNew, BufferedImage.TYPE_INT_RGB);
Graphics g = imageNew.getGraphics();
g.setColor(Color.WHITE);
g.fillRect(0,0,widthNew,heightNew);


int widthTemp = 0;
int heightTemp = 0;
int[] imageArray = new int[100*100];
switch(size){
case(1): 
imageArray = new int[widths.get(0) * heights.get(0)];
imageArray = images.get(0).getRGB(0, 0, widths.get(0), heights.get(0),
imageArray, 0, widths.get(0));
imageNew.setRGB(100, 100, widths.get(0), heights.get(0), imageArray,
0, widths.get(0));

break;
case(2): 
widthTemp = widths.get(0)/2;
heightTemp = heights.get(0);
for(int i = 0; i < images.size(); i++){
// 绘制图片 draw the picture
imageArray = new int[widths.get(i) * heights.get(i)];
imageArray = images.get(i).getRGB(0, 0, widths.get(i), heights.get(i),
imageArray, 0, widths.get(i));
imageNew.setRGB(widthTemp, heightTemp, widths.get(i), heights.get(i), imageArray,
0, widths.get(i));
widthTemp += widths.get(i);
}
break;
case(3): 
widthTemp = widths.get(0);
heightTemp = heights.get(0)/2;
imageArray = images.get(0).getRGB(0, 0, widths.get(0), heights.get(0),
imageArray, 0, widths.get(0));
imageNew.setRGB(widthTemp, heightTemp, widths.get(0), heights.get(0), imageArray,
0, widths.get(0));

widthTemp = widths.get(0)/2;
heightTemp += heights.get(0);
for(int i = 1; i < size; ++i){
imageArray = new int[widths.get(i) * heights.get(i)];
imageArray = images.get(i).getRGB(0, 0, widths.get(i), heights.get(i),
imageArray, 0, widths.get(i));
imageNew.setRGB(widthTemp, heightTemp, widths.get(i), heights.get(i), imageArray,
0, widths.get(i));
widthTemp += widths.get(i);
}
break;
case(4):
widthTemp = widths.get(0)/2;
heightTemp = heights.get(0)/2;
for(int i = 0; i < 2; ++i){
imageArray = new int[widths.get(i) * heights.get(i)];
imageArray = images.get(i).getRGB(0, 0, widths.get(i), heights.get(i),
imageArray, 0, widths.get(i));
imageNew.setRGB(widthTemp, heightTemp, widths.get(i), heights.get(i), imageArray,
0, widths.get(i));
widthTemp += widths.get(i);
}
widthTemp = widths.get(0)/2;
heightTemp += heights.get(0);

for(int i = 2; i < size; ++i){
imageArray = new int[widths.get(i) * heights.get(i)];
imageArray = images.get(i).getRGB(0, 0, widths.get(i), heights.get(i),
imageArray, 0, widths.get(i));
imageNew.setRGB(widthTemp, heightTemp, widths.get(i), heights.get(i), imageArray,
0, widths.get(i));
widthTemp += widths.get(i);
}
break;
case(5):
widthTemp = widths.get(0)/2;
heightTemp = heights.get(0)/2;

for(int i = 0; i < 2; ++i){
imageArray = new int[widths.get(i) * heights.get(i)];
imageArray = images.get(i).getRGB(0, 0, widths.get(i), heights.get(i),
imageArray, 0, widths.get(i));
imageNew.setRGB(widthTemp, heightTemp, widths.get(i), heights.get(i), imageArray,
0, widths.get(i));
widthTemp += widths.get(i);
}

widthTemp = 0;
heightTemp += heights.get(0);
for(int i = 2; i < size; ++i){
imageArray = new int[widths.get(i) * heights.get(i)];
imageArray = images.get(i).getRGB(0, 0, widths.get(i), heights.get(i),
imageArray, 0, widths.get(i));
imageNew.setRGB(widthTemp, heightTemp, widths.get(i), heights.get(i), imageArray,
0, widths.get(i));
widthTemp += widths.get(i);
}
break;
case(6):
widthTemp = 0;
heightTemp = heights.get(0)/2;

for(int i = 0; i < 3; ++i){
imageArray = new int[widths.get(i) * heights.get(i)];
imageArray = images.get(i).getRGB(0, 0, widths.get(i), heights.get(i),
imageArray, 0, widths.get(i));
imageNew.setRGB(widthTemp, heightTemp, widths.get(i), heights.get(i), imageArray,
0, widths.get(i));
widthTemp += widths.get(i);
}

widthTemp = 0;
heightTemp += heights.get(0);
for(int i = 3; i < size; ++i){
imageArray = new int[widths.get(i) * heights.get(i)];
imageArray = images.get(i).getRGB(0, 0, widths.get(i), heights.get(i),
imageArray, 0, widths.get(i));
imageNew.setRGB(widthTemp, heightTemp, widths.get(i), heights.get(i), imageArray,
0, widths.get(i));
widthTemp += widths.get(i);
}
break;
case(7):
widthTemp = widths.get(0);
heightTemp = 0;
imageArray = images.get(0).getRGB(0, 0, widths.get(0), heights.get(0),
imageArray, 0, widths.get(0));
imageNew.setRGB(widthTemp, heightTemp, widths.get(0), heights.get(0), imageArray,
0, widths.get(0));

widthTemp = 0;
heightTemp += heights.get(0);
for(int i = 1; i < 4; ++i){
imageArray = new int[widths.get(i) * heights.get(i)];
imageArray = images.get(i).getRGB(0, 0, widths.get(i), heights.get(i),
imageArray, 0, widths.get(i));
imageNew.setRGB(widthTemp, heightTemp, widths.get(i), heights.get(i), imageArray,
0, widths.get(i));
widthTemp += widths.get(i);
}

widthTemp = 0;
heightTemp += heights.get(0);
for(int i = 4; i < size; ++i){
imageArray = new int[widths.get(i) * heights.get(i)];
imageArray = images.get(i).getRGB(0, 0, widths.get(i), heights.get(i),
imageArray, 0, widths.get(i));
imageNew.setRGB(widthTemp, heightTemp, widths.get(i), heights.get(i), imageArray,
0, widths.get(i));
widthTemp += widths.get(i);
}
case(8):
widthTemp = widths.get(0)/2;
heightTemp = 0;
for(int i = 0; i < 2; ++i){
imageArray = new int[widths.get(i) * heights.get(i)];
imageArray = images.get(i).getRGB(0, 0, widths.get(i), heights.get(i),
imageArray, 0, widths.get(i));
imageNew.setRGB(widthTemp, heightTemp, widths.get(i), heights.get(i), imageArray,
0, widths.get(i));
widthTemp += widths.get(i);
}

widthTemp = 0;
heightTemp += widths.get(0);
for(int i = 2; i < 5; ++i){
imageArray = new int[widths.get(i) * heights.get(i)];
imageArray = images.get(i).getRGB(0, 0, widths.get(i), heights.get(i),
imageArray, 0, widths.get(i));
imageNew.setRGB(widthTemp, heightTemp, widths.get(i), heights.get(i), imageArray,
0, widths.get(i));
widthTemp += widths.get(i);
}

widthTemp = 0;
heightTemp += widths.get(0);
for(int i = 5; i < size; ++i){
imageArray = new int[widths.get(i) * heights.get(i)];
imageArray = images.get(i).getRGB(0, 0, widths.get(i), heights.get(i),
imageArray, 0, widths.get(i));
imageNew.setRGB(widthTemp, heightTemp, widths.get(i), heights.get(i), imageArray,
0, widths.get(i));
widthTemp += widths.get(i);
}
case(9):
widthTemp = 0;
heightTemp = 0;
for(int i = 0; i < 3; ++i){
imageArray = new int[widths.get(i) * heights.get(i)];
imageArray = images.get(i).getRGB(0, 0, widths.get(i), heights.get(i),
imageArray, 0, widths.get(i));
imageNew.setRGB(widthTemp, heightTemp, widths.get(i), heights.get(i), imageArray,
0, widths.get(i));
widthTemp += widths.get(i);
}

widthTemp = 0;
heightTemp += heights.get(0);
for(int i = 3; i < 6; ++i){
imageArray = new int[widths.get(i) * heights.get(i)];
imageArray = images.get(i).getRGB(0, 0, widths.get(i), heights.get(i),
imageArray, 0, widths.get(i));
imageNew.setRGB(widthTemp, heightTemp, widths.get(i), heights.get(i), imageArray,
0, widths.get(i));
widthTemp += widths.get(i);
}

widthTemp = 0;
heightTemp += heights.get(0);
for(int i = 6; i < size; ++i){
imageArray = new int[widths.get(i) * heights.get(i)];
imageArray = images.get(i).getRGB(0, 0, widths.get(i), heights.get(i),
imageArray, 0, widths.get(i));
imageNew.setRGB(widthTemp, heightTemp, widths.get(i), heights.get(i), imageArray,
0, widths.get(i));
widthTemp += widths.get(i);
}
break;
}

String fileName = "group_"+group_name+"_photo.jpg";
java.io.File outFile = new java.io.File(Blob.getStore(), fileName);
if (!outFile.exists()) {
outFile.createNewFile();
}
ImageIO.write(imageNew, "jpeg", outFile);


String downLoadPath = "/data/attachments/group_"+group_name+"_photo.jpg";
dataMap.put("status_code", status_code);
dataMap.put("msg", msg);
dataMap.put("photoPath", downLoadPath.replace(".", "_sub."));
renderJSON(dataMap);
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值