자바 그룹 프로필 사진 생 성
참고:http://www.2cto.com/weixin/201503/382943.html
자바 를 통 해 위 챗 과 유사 한 그룹 프로필 사진 생 성:
package com.tch.test.common.image;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import javax.imageio.ImageIO;
/**
*
*/
public final class ImageUtil {
/** :JPG */
private static final String PICTRUE_FORMATE_JPG = "JPG";
private static final int PIC_WIDTH = 400; //
private static final int PIC_HEIGHT = 400; //
private ImageUtil() {
}
public static void main(String[] args) throws IOException {
String destPath = "/media/tch/disk1/study/temp/folder/";
List sourcePics = new ArrayList<>();
sourcePics.add("http://h.hiphotos.baidu.com/zhidao/pic/item/eac4b74543a9822628850ccc8c82b9014b90eb91.jpg");
sourcePics.add("http://h.hiphotos.baidu.com/zhidao/pic/item/3812b31bb051f81991b9d8dbdcb44aed2f73e787.jpg");
sourcePics.add("http://cdn.duitang.com/uploads/item/201412/19/20141219154459_3P4G2.jpeg");
sourcePics.add("http://img4.duitang.com/uploads/item/201603/12/20160312211255_nB5mQ.thumb.700_0.jpeg");
sourcePics.add("http://h.hiphotos.baidu.com/zhidao/pic/item/c8177f3e6709c93d0f5f00e79b3df8dcd1005474.jpg");
sourcePics.add("http://ww1.sinaimg.cn/crop.7.22.1192.1192.1024/5c6defebjw8epti0r9noaj20xc0y1n0x.jpg");
sourcePics.add("http://ww1.sinaimg.cn/crop.0.0.800.800.1024/735510dbjw8eoo1nn6h22j20m80m8t9t.jpg");
sourcePics.add("http://ww2.sinaimg.cn/crop.0.0.1242.1242.1024/005EWUXPjw8eto7cdd42wj30yi0yiabz.jpg");
sourcePics.add("http://ww2.sinaimg.cn/crop.0.0.1080.1080.1024/d773ebfajw8eum57eobkwj20u00u075w.jpg");
for(int i = 1; i <= sourcePics.size(); i++){
getGroupAvatar(sourcePics.subList(0, i), destPath + "out" + i + ".jpg");
}
}
/**
*
* @param userAvatars
* @throws IOException
*/
public static void getGroupAvatar(List userAvatars, String destPath) throws IOException {
int totalPicNum = userAvatars.size();
PicInfo picInfo = getPicInfo(totalPicNum);
List bufferedImages = new ArrayList();
//
for (int i = 0; i < totalPicNum; i++) {
bufferedImages.add(resizeNetWorkImage(userAvatars.get(i), picInfo.getPerPicWith(), picInfo.getPerPicHeight(), true));
}
BufferedImage outImage = new BufferedImage(PIC_WIDTH, PIC_HEIGHT, BufferedImage.TYPE_INT_RGB);
//
Graphics graphics = outImage.getGraphics();
Graphics2D graphics2d = (Graphics2D) graphics;
graphics2d.setBackground(new Color(231,231,231));
graphics2d.clearRect(0, 0, PIC_WIDTH, PIC_HEIGHT);
//
for (int picIndex = 0; picIndex < bufferedImages.size(); picIndex++) {
if(totalPicNum == 2 || totalPicNum == 5 || totalPicNum == 6){
// ,
specialDraw(bufferedImages.get(picIndex), picIndex, picInfo, graphics2d, totalPicNum);
}else{
// ,
normalDraw(bufferedImages.get(picIndex), picIndex, picInfo, graphics2d);
}
}
//
ImageIO.write(outImage, PICTRUE_FORMATE_JPG, new File(destPath));
}
private static void specialDraw(BufferedImage bufferedImage, int picIndex, PicInfo picInfo, Graphics2D graphics2d, int totalPicNum) {
int xIndex = (picIndex % picInfo.getPicNumPerRow());
int y = 0;
if(totalPicNum == 2){
y = PIC_HEIGHT / 4;
}else if(totalPicNum == 5 || totalPicNum == 6){
if(picIndex < 3){
y = (PIC_HEIGHT / 2 - PIC_HEIGHT / 3);
}else{
y = PIC_HEIGHT / 2;
}
}
graphics2d.drawImage(bufferedImage, xIndex * picInfo.getPerPicWith(), y, null);
}
private static void normalDraw(BufferedImage bufferedImage, int picIndex, PicInfo picInfo, Graphics2D graphics2d) {
int xIndex = (picIndex % picInfo.getPicNumPerRow());
int yIndex = (picIndex / picInfo.getPicNumPerRow());
graphics2d.drawImage(bufferedImage, xIndex * picInfo.getPerPicWith(), yIndex * picInfo.getPerPicHeight(), null);
}
/**
*
* @param filePath
* @param width
* @param height
* @param fillWhite
*/
public static BufferedImage resizeImage(BufferedImage bufferedImage, int width, int height, boolean fillWhite) {
double ratio = 0; //
Image newImage = bufferedImage.getScaledInstance(width, height, Image.SCALE_SMOOTH);
//
if ((bufferedImage.getHeight() > height) || (bufferedImage.getWidth() > width)) {
if (bufferedImage.getHeight() > bufferedImage.getWidth()) {
ratio = (new Integer(height)).doubleValue()/bufferedImage.getHeight();
} else {
ratio = (new Integer(width)).doubleValue()/bufferedImage.getWidth();
}
AffineTransformOp affineTransformOp = new AffineTransformOp(AffineTransform.getScaleInstance(ratio, ratio), null);
newImage = affineTransformOp.filter(bufferedImage, null);
}
if (fillWhite) {
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g = image.createGraphics();
g.setColor(Color.white);
g.fillRect(0, 0, width, height);
if (width == newImage.getWidth(null)){
g.drawImage(newImage, 0, (height - newImage.getHeight(null))/2, newImage.getWidth(null), newImage.getHeight(null), Color.white, null);
}else{
g.drawImage(newImage, (width - newImage.getWidth(null))/2, 0, newImage.getWidth(null), newImage.getHeight(null), Color.white, null);
}
g.dispose();
newImage = image;
}
return (BufferedImage) newImage;
}
/**
*
* @param file
* @param width
* @param height
* @param fillWhite
* @return
*/
public static BufferedImage resizeLocalImage(File file, int width, int height, boolean fillWhite) {
try {
//
BufferedImage bufferedImage = ImageIO.read(file);
return resizeImage(bufferedImage, width, height, fillWhite);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/**
*
* @param imageUrl
* @param width
* @param height
* @param fillWhite
* @return
*/
public static BufferedImage resizeNetWorkImage(String imageUrl, int width, int height, boolean fillWhite) {
try {
//
BufferedImage bufferedImage = ImageIO.read(new URL(imageUrl));
return resizeImage(bufferedImage, width, height, fillWhite);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/**
* /
* @param picNum
* @return
*/
private static PicInfo getPicInfo(int totalPicNum){
//
int picNumPerRow = 1;
//
int picNumPerCol = 1;
switch (totalPicNum) {
case 1:
picNumPerRow = 1;
picNumPerCol = 1;
break;
case 2:
picNumPerRow = 2;
picNumPerCol = 1;
break;
case 3:
case 4:
picNumPerRow = 2;
picNumPerCol = 2;
break;
case 5:
case 6:
picNumPerRow = 3;
picNumPerCol = 2;
break;
case 7:
case 8:
case 9:
picNumPerRow = 3;
picNumPerCol = 3;
break;
default:
picNumPerRow = 1;
picNumPerCol = 1;
break;
}
int perPicWith = PIC_WIDTH/picNumPerRow;
int perPicHeight = PIC_HEIGHT/picNumPerCol;
// /
int effectWithHeight = Math.min(perPicWith, perPicHeight);
PicInfo picInfo = new PicInfo();
//
picInfo.setPerPicWith(effectWithHeight);
picInfo.setPerPicHeight(effectWithHeight);
picInfo.setPicNumPerRow(picNumPerRow);
picInfo.setPicNumPerCol(picNumPerCol);
return picInfo;
}
/**
*
* @author tianchaohui
*/
public static class PicLocation{
//
int xIndex = 0;
//
int yIndex = 0;
public int getxIndex() {
return xIndex;
}
public void setxIndex(int xIndex) {
this.xIndex = xIndex;
}
public int getyIndex() {
return yIndex;
}
public void setyIndex(int yIndex) {
this.yIndex = yIndex;
}
}
/**
*
* @author tianchaohui
*/
public static class PicInfo{
//
private int perPicWith;
//
private int perPicHeight;
//
int picNumPerRow;
//
int picNumPerCol;
public int getPerPicWith() {
return perPicWith;
}
public void setPerPicWith(int perPicWith) {
this.perPicWith = perPicWith;
}
public int getPerPicHeight() {
return perPicHeight;
}
public void setPerPicHeight(int perPicHeight) {
this.perPicHeight = perPicHeight;
}
public int getPicNumPerRow() {
return picNumPerRow;
}
public void setPicNumPerRow(int picNumPerRow) {
this.picNumPerRow = picNumPerRow;
}
public int getPicNumPerCol() {
return picNumPerCol;
}
public void setPicNumPerCol(int picNumPerCol) {
this.picNumPerCol = picNumPerCol;
}
@Override
public String toString() {
return "PicInfo [perPicWith=" + perPicWith + ", perPicHeight=" + perPicHeight + ", picNumPerRow="
+ picNumPerRow + ", picNumPerCol=" + picNumPerCol + "]";
}
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.