java 图片处理,包括图片裁剪(圆形,矩形),图片的缩放,多张图片组合成一张长图(横向,纵向),边框,水印(45度倾斜),写字,插图,实线、虚线,html格式文本转换成图片(需要html2image-0.9.jar,现在上传下载要5分,我就不上传了,地址https://download.csdn.net/download/u013296643/10115762)等等。
package com.coolsn.modules.tb.imageTools;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Transparency;
import java.awt.geom.Ellipse2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Random;
import javax.imageio.ImageIO;
import gui.ava.html.image.generator.HtmlImageGenerator;
/**
* 图片处理
* @author tb
*
*/
public class ImgTools {
/**
* 裁剪图片-优化版本;以图片中心为源点,按照长宽比进行图片裁剪,裁剪圆形
* @param BufferedImage
* @param widthToHeight 长宽比例,若是为1则是圆形
* @return BufferedImage
* @throws IOException
*/
public static BufferedImage cutPicArc(BufferedImage bi,float widthToHeight) throws IOException{
int width=bi.getWidth();
int height=bi.getHeight();
int dw=0;
int dh=0;
if(width/(height*1.0)>widthToHeight){//截长
dw=Math.round(Math.abs(width-widthToHeight*height));
}else{//截高
dh=Math.round(Math.abs(height-(1/widthToHeight)*width));
}
BufferedImage bi2 = new BufferedImage(width-dw,height-dh,Transparency.TRANSLUCENT);
Ellipse2D.Double shape = new Ellipse2D.Double(0,0,width-dw,height-dh);
Graphics2D g2 = bi2.createGraphics();
g2.setClip(shape);
// 使用 setRenderingHint 设置抗锯齿
g2.drawImage(bi,0,0,null);
g2.dispose();
return bi2;
}
/**
* 裁剪图片-优化版本;以图片中心为源点,按照长宽比进行图片裁剪,裁剪矩形
* @param BufferedImage
* @param widthToHeight 长宽比例,若是为1则是正方形
* @return BufferedImage
* @throws IOException
*/
public static BufferedImage cutPicRect(BufferedImage bi,float widthToHeight) throws IOException{
int width=bi.getWidth();
int height=bi.getHeight();
int dw=0;
int dh=0;
if(width/(height*1.0)>widthToHeight){//截长
dw=Math.round(Math.abs(width-widthToHeight*height));
}else{//截高
dh=Math.round(Math.abs(height-(1/widthToHeight)*width));
}
BufferedImage bi2 = new BufferedImage(width-dw,height-dh,Transparency.TRANSLUCENT);
Graphics2D g2 = bi2.createGraphics();
// 使用 setRenderingHint 设置抗锯齿
g2.drawImage(bi,-dw/2,-dh/2,null);
g2.dispose();
return bi2;
}
/**
* 缩放图片
* @param BufferedImage
* @param scale 比例饿
* @return BufferedImage
*/
public static BufferedImage humbnailPic(BufferedImage bi,float scale) throws IOException{
int width=bi.getWidth();
int height=bi.getHeight();
BufferedImage bi2 = new BufferedImage(Math.round(width*scale),Math.round(height*scale),Transparency.TRANSLUCENT);
Graphics2D g2 = bi2.createGraphics();
// 使用 setRenderingHint 设置抗锯齿
g2.drawImage(bi, 0, 0, Math.round(width*scale),Math.round(height*scale), 0, 0, width, height, null);
g2.dispose();
return bi2;
}
/**
* 缩放图片-按最小长宽进行压缩
* @param BufferedImage
* @param scale 比例饿
* @return BufferedImage
*/
public static BufferedImage humbnailPicMin(BufferedImage bi,int minWidth,int minHeight) throws IOException{
int width=bi.getWidth();
int height=bi.getHeight();
float scale = 0f;
if(minWidth/width>minHeight/height) {//取小比例
scale = 1.0f*minHeight/height;
}else scale = 1.0f*minWidth/width;
BufferedImage bi2 = new BufferedImage(Math.round(width*scale),Math.round(height*scale),Transparency.TRANSLUCENT);
Graphics2D g2 = bi2.createGraphics();
// 使用 setRenderingHint 设置抗锯齿
g2.drawImage(bi, 0, 0, Math.round(width*scale),Math.round(height*scale), 0, 0, width, height, null);
g2.dispose();
return bi2;
}
/**
* 多张图片合成为一张图-竖向
* @param imagePaths 图片路径数组
* @param width 合成后图片宽度
* @param widthFixing 是否固宽(固宽即是所有的图片的宽度一致,大于此宽度的等比例压缩,小于的等比例扩大)
* @return
*/
public static BufferedImage composeImageVertical(String[] imagePaths,int width) {
File[] files = new File[imagePaths.length];
for(int index=0; index圆
* @param g graphics
* @param wR 横半径
* @param hR 纵半径
* @param startW 开始角度
* @param endW 结束角度
* 注:角度均是0到360
* @return
*/
public static BufferedImage drawCirCle(BufferedImage bi,int wR,int hR,int startW,int endW){
Graphics2D g = bi.createGraphics();
setRandomColor(g);
g.drawArc(0, 0, wR, hR,startW,endW);
//画椭圆的,不过感觉是多余的
//g.drawOval(0, 0, wR, hR);
g.dispose();
return bi;
}
/**
* 自带的画圆很有局限性,该方法可以将圆设置为多彩色
* 注:内嵌了随机颜色生成,故而产生的线是多彩色
* @param g graphics
* @param xDs 圆心的x坐标
* @param yDs 圆心的y坐标
* @param r 该圆的半径
* @return graphics
*/
public static BufferedImage drawCirCleByMine(BufferedImage bi,int xDs,int yDs,int r){
Graphics2D g = bi.createGraphics();
int yD=yDs;
int xD=xDs-20;
int y1=yD;
int x1=0;
int x2=0;
int y2=0;
for(int j=0;j<=r*2;j++){
x2=x1+1;
y2=(int) Math.round(Math.sqrt(r*r-(x2-r)*(x2-r)))+yD;
setRandomColor(g);
g.drawLine(x1+xD, y1, x2+xD, y2);
x1=x2;
y1=y2;
}
x1=0;
y1=yD;
for(int j=0;j<=r*2;j++){
x2=x1+1;
y2=yD-(int) Math.round(Math.sqrt(r*r-(x2-r)*(x2-r)));
setRandomColor(g);
g.drawLine(x1+xD, y1, x2+xD, y2);
x1=x2;
y1=y2;
}
g.dispose();
return bi;
}
/**
* 设置随机颜色
* @param g
* @return
*/
public static Graphics setRandomColor(Graphics g){
int red=0;
int green=0;
int blue=0;
Random random=new Random();
// 产生随机的颜色分量来构造颜色值,这样输出的每位数字的颜色值都将不同。
red = random.nextInt(255);
green = random.nextInt(255);
blue = random.nextInt(255);
// 用随机产生的颜色将验证码绘制到图像中。
g.setColor(new Color(red, green, blue));
return g;
}
/**
* html格式文本转换成图片
* @param htmlStr
* @return
*/
public static BufferedImage html2image(String htmlStr) {
HtmlImageGenerator imageGenerator = new HtmlImageGenerator();
imageGenerator.loadHtml(htmlStr);
BufferedImage bufferedImage = imageGenerator.getBufferedImage();
return bufferedImage;
}
/**
* 画线-实线
* @param bi
* @param x1 起点x坐标
* @param y1 起点y坐标
* @param x2 终点x坐标
* @param y2 终点y坐标
* @param color 颜色
* @param size 粗细
* @return
*/
public static BufferedImage drawLine(BufferedImage bi,int x1,int y1, int x2, int y2,Color color,float size) {
Graphics2D g = bi.createGraphics();
g.setColor(color);
g.setStroke(new BasicStroke(size));
g.drawLine(x1, y1, x2, y2);
g.dispose();
return bi;
}
/**
* 画线-虚线
* @param bi
* @param x1 起点x坐标
* @param y1 起点y坐标
* @param x2 终点x坐标
* @param y2 终点y坐标
* @param color 颜色
* @param size 粗细
* @param lineSize 线段长度
* @param disSize 间隔长度
* @return
*/
public static BufferedImage drawLine(BufferedImage bi,int x1,int y1, int x2, int y2,Color color,float size,int lineLen,int disLen) {
Graphics2D g = bi.createGraphics();
g.setColor(color);
g.setStroke(new BasicStroke(size));
int lenX = x2-x1;
int lenY = y2-y1;
if (lenX != 0 && lenY != 0) {//画斜线
int disXu = (int) Math.round(Math.sqrt((Math.pow(lenY, 2)*Math.pow(lineLen, 2))/(Math.pow(lenX, 2)+Math.pow(lenY, 2))));//x的步长
int disXdu = (int) Math.round(Math.sqrt((Math.pow(lenY, 2)*Math.pow(disLen, 2))/(Math.pow(lenX, 2)+Math.pow(lenY, 2))));//x的间隔步长
int disYu = (int) Math.round(lenY*1.0*disXu/lenX);//y的步长
int disYdu = (int) Math.round(lenY*1.0*disXdu/lenX);//y的间隔步长
if (disXu == 0) {
disXu = 1;
}
if (disXdu == 0) {
disXdu = 1;
}
for(int disX=0; disX<=lenX; disX+=(disXu+disXdu)) {
x2 = x1+disXu;
y2 = y1+disYu;
g.drawLine(x1, y1, x2, y2);
x1 = x2+disXdu;
y1 = y2+disYdu;
}
}else {//画横竖线
if (lenX == 0) {//画横线
for(int disY=0; disY<=lenY; disY+=(lineLen+disLen)) {
y2 = y1+lineLen;
g.drawLine(x1, y1, x2, y2);
y1 =y2+disLen;
}
}else {
for(int disX=0; disX<=lenX; disX+=(lineLen+disLen)) {
x2 = x1+lineLen;
g.drawLine(x1, y1, x2, y2);
x1 =x2+disLen;
}
}
}
g.dispose();
return bi;
}
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
/*BufferedImage bi=ImageIO.read(new File("F:\\1.jpeg"));
bi=cutPicRect(bi,1);
ImageIO.write(bi, "png", new File("F:\\1_1.jpeg"));*/
/*BufferedImage bImage = composeImageVertical(new String[] {"F:\\picture\\壁纸\\14ce36d3d539b6004675e6c6ea50352ac65cb70c.jpg",
"F:\\picture\\壁纸\\138-140F9154532.jpg",
"F:\\picture\\壁纸\\82240c15ed7bc76f2af9cb0882770dd6.jpg",
"F:\\picture\\壁纸\\22197937_1371459132142.jpg",
"F:\\picture\\壁纸\\c0e0b81d5066c275d6f8c954b1202386.jpg"},2000);*/
//BufferedImage bImage = composeImageVertical("F:\\picture\\壁纸",500);
//ImageIO.write(bImage, "png", new File("F:\\picture\\壁纸\\compose.jpg"));
BufferedImage bi=createImage(450, 630, Color.WHITE);
bi = drawFont(bi,"测试", Color.RED, 10, 10, 15);
ImageIO.write(bi, "jpg", new File("F:\\picture\\test\\参赛证.jpg"));
}
}