仿微信9宮格羣組頭像

開發了一個聊天工具,羣組頭像像仿照微信的頭像那樣顯示成員頭像合成圖片,百度了一下,發現沒有順手的,就自己動手寫了一個,現在分享給大家。
我所有用戶圖片都是120*120的,合成圖片爲132*132,九宮格的話用戶圖片邊到合成圖片邊爲3,依次類推,裏面的120,132,40,30等大小根據自己的頭像大小靈活變動即可

public static String[] getXy(int size){
String[] s = new String[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);
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");
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章