|
|
@@ -19,8 +19,6 @@ public class ImageUtils {
|
|
|
private final static JLabel J_LABEL = new JLabel();
|
|
|
|
|
|
|
|
|
-
|
|
|
-
|
|
|
public static byte[] changeImageSize(InputStream inputStream, Integer size) {
|
|
|
try {
|
|
|
BufferedImage oldBufferedImage = ImageIO.read(inputStream);
|
|
|
@@ -150,7 +148,7 @@ public class ImageUtils {
|
|
|
}
|
|
|
int directionSurplus = width % directionValue;
|
|
|
if (directionSurplus > 0) {
|
|
|
- byte[] bytes = cropImage(bufferedImage, height - directionSurplus, 0, directionSurplus, height);
|
|
|
+ byte[] bytes = cropImage(bufferedImage, height - directionSurplus, 0, directionSurplus, height);
|
|
|
byteToFile(bytes, fileName + "-" + (i + 1), "jpg", path);
|
|
|
}
|
|
|
}
|
|
|
@@ -189,30 +187,31 @@ public class ImageUtils {
|
|
|
|
|
|
/**
|
|
|
* 设置透明度
|
|
|
+ *
|
|
|
* @param bufferedImage
|
|
|
* @param alpha
|
|
|
* @param bufferedImageType
|
|
|
* @return
|
|
|
*/
|
|
|
- public static BufferedImage setBufferedImageAlpha(BufferedImage bufferedImage,Integer alpha,Integer bufferedImageType){
|
|
|
- if(alpha < 0 || alpha > 255){
|
|
|
+ public static BufferedImage setBufferedImageAlpha(BufferedImage bufferedImage, Integer alpha, Integer bufferedImageType) {
|
|
|
+ if (alpha < 0 || alpha > 255) {
|
|
|
throw new RuntimeException("范围错误[0,255]");
|
|
|
}
|
|
|
int width = bufferedImage.getWidth();
|
|
|
int height = bufferedImage.getHeight();
|
|
|
- BufferedImage newBufferedImage = new BufferedImage(width,height, bufferedImageType);
|
|
|
+ BufferedImage newBufferedImage = new BufferedImage(width, height, bufferedImageType);
|
|
|
int argb;
|
|
|
int oa;
|
|
|
int a;
|
|
|
int rgb;
|
|
|
- for(int y = 0; y < height; y++){
|
|
|
- for(int x = 0; x < width; x++){
|
|
|
- argb = bufferedImage.getRGB(x,y);
|
|
|
+ for (int y = 0; y < height; y++) {
|
|
|
+ for (int x = 0; x < width; x++) {
|
|
|
+ argb = bufferedImage.getRGB(x, y);
|
|
|
rgb = argb & 0x00FFFFFF;
|
|
|
oa = argb >>> 24;
|
|
|
- a = oa == 0?0:alpha<<24;
|
|
|
- argb = a^rgb;
|
|
|
- newBufferedImage.setRGB(x,y,argb);
|
|
|
+ a = oa == 0 ? 0 : alpha << 24;
|
|
|
+ argb = a ^ rgb;
|
|
|
+ newBufferedImage.setRGB(x, y, argb);
|
|
|
}
|
|
|
}
|
|
|
return newBufferedImage;
|
|
|
@@ -221,6 +220,7 @@ public class ImageUtils {
|
|
|
|
|
|
/**
|
|
|
* 放大缩小
|
|
|
+ *
|
|
|
* @param bufferedImage
|
|
|
* @param enlargementTimes
|
|
|
* @return
|
|
|
@@ -286,7 +286,7 @@ public class ImageUtils {
|
|
|
graphics2D.setColor(color);
|
|
|
graphics2D.setFont(font);
|
|
|
graphics2D.drawString(str, pointX, pointY);*/
|
|
|
- writeFontBufferedImage(graphics2D,str,pointX,pointY,font,color);
|
|
|
+ writeFontBufferedImage(graphics2D, str, pointX, pointY, font, color);
|
|
|
return bufferedImage;
|
|
|
}
|
|
|
|
|
|
@@ -347,6 +347,7 @@ public class ImageUtils {
|
|
|
|
|
|
/**
|
|
|
* 为图片打马赛克
|
|
|
+ *
|
|
|
* @param bufferedImage
|
|
|
* @param pointX
|
|
|
* @param pointY
|
|
|
@@ -436,18 +437,19 @@ public class ImageUtils {
|
|
|
|
|
|
/**
|
|
|
* 把字符串拆成行
|
|
|
- * @param context 文本内容
|
|
|
+ *
|
|
|
+ * @param context 文本内容
|
|
|
* @param fontMetrics
|
|
|
- * @param pointX 开始点的x轴坐标
|
|
|
- * @param pointY 开始点的y轴坐标
|
|
|
- * @param width 文本域宽度
|
|
|
- * @param height 文本域高度
|
|
|
- * @param wordSpace 字间距
|
|
|
- * @param lineSpace 行间距
|
|
|
+ * @param pointX 开始点的x轴坐标
|
|
|
+ * @param pointY 开始点的y轴坐标
|
|
|
+ * @param width 文本域宽度
|
|
|
+ * @param height 文本域高度
|
|
|
+ * @param wordSpace 字间距
|
|
|
+ * @param lineSpace 行间距
|
|
|
* @param horizontalType 1 右对齐 2 居中 3 左对齐
|
|
|
* @return
|
|
|
*/
|
|
|
- public static List<LineText> splitContext(String context,FontMetrics fontMetrics,int pointX,int pointY,int width,int height,int wordSpace,int lineSpace,Integer horizontalType,Integer verticalType){
|
|
|
+ public static List<LineText> splitContext(String context, FontMetrics fontMetrics, int pointX, int pointY, int width, int height, int wordSpace, int lineSpace, Integer horizontalType, Integer verticalType) {
|
|
|
context = new String(context.getBytes(StandardCharsets.UTF_8));
|
|
|
Font font = fontMetrics.getFont();
|
|
|
char[] chars = context.toCharArray();
|
|
|
@@ -460,33 +462,33 @@ public class ImageUtils {
|
|
|
//字体宽度
|
|
|
int charSize = 0;
|
|
|
List<CharText> charTextList = new ArrayList<>();
|
|
|
- for(char c:chars){
|
|
|
+ for (char c : chars) {
|
|
|
charSize = fontMetrics.charWidth(c);
|
|
|
currentLineWidth += charSize;
|
|
|
- if(currentLineWidth >= width){
|
|
|
- returnLineList.add(new LineText(pointX,width,currentHeight,sb.toString(),currentLineWidth - charSize - wordSpace,charTextList,horizontalType));
|
|
|
+ if (currentLineWidth >= width) {
|
|
|
+ returnLineList.add(new LineText(pointX, width, currentHeight, sb.toString(), currentLineWidth - charSize - wordSpace, charTextList, horizontalType));
|
|
|
currentHeight += font.getSize();
|
|
|
currentHeight += lineSpace;
|
|
|
currentLineWidth = charSize;
|
|
|
charTextList = new ArrayList<>();
|
|
|
sb = new StringBuilder();
|
|
|
}
|
|
|
- charTextList.add(new CharText(currentLineWidth - charSize,currentHeight,c));
|
|
|
+ charTextList.add(new CharText(currentLineWidth - charSize, currentHeight, c));
|
|
|
currentLineWidth += wordSpace;
|
|
|
sb.append(c);
|
|
|
- if(currentHeight > height){
|
|
|
+ if (currentHeight > height) {
|
|
|
sb = new StringBuilder();
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
- if(sb.length() > 0){
|
|
|
- returnLineList.add(new LineText(pointX,width,currentHeight,sb.toString(),currentLineWidth - wordSpace,charTextList,horizontalType));
|
|
|
+ if (sb.length() > 0) {
|
|
|
+ returnLineList.add(new LineText(pointX, width, currentHeight, sb.toString(), currentLineWidth - wordSpace, charTextList, horizontalType));
|
|
|
}
|
|
|
- if(verticalType.equals(2)){
|
|
|
- int allHeight = returnLineList.size()* font.getSize() + lineSpace * returnLineList.size() - 1;
|
|
|
- pointY = (height - allHeight)/2 + pointY;
|
|
|
- }else if (verticalType.equals(3)){
|
|
|
- int allHeight = returnLineList.size()* font.getSize() + lineSpace * returnLineList.size() - 1;
|
|
|
+ if (verticalType.equals(2)) {
|
|
|
+ int allHeight = returnLineList.size() * font.getSize() + lineSpace * returnLineList.size() - 1;
|
|
|
+ pointY = (height - allHeight) / 2 + pointY;
|
|
|
+ } else if (verticalType.equals(3)) {
|
|
|
+ int allHeight = returnLineList.size() * font.getSize() + lineSpace * returnLineList.size() - 1;
|
|
|
pointY = pointY + height - allHeight;
|
|
|
}
|
|
|
int finalPointY = pointY;
|
|
|
@@ -502,24 +504,25 @@ public class ImageUtils {
|
|
|
|
|
|
/**
|
|
|
* 旋转图片
|
|
|
+ *
|
|
|
* @param bufferedImage
|
|
|
* @param angle
|
|
|
* @return
|
|
|
*/
|
|
|
- public static BufferedImage rotateImage(BufferedImage bufferedImage,double angle){
|
|
|
- Rectangle rectangle = getRotateRectangle(bufferedImage.getWidth(),bufferedImage.getHeight(),angle);
|
|
|
- BufferedImage image = new BufferedImage((int) rectangle.getWidth(),(int) rectangle.getHeight(), BufferedImage.TYPE_4BYTE_ABGR);
|
|
|
+ public static BufferedImage rotateImage(BufferedImage bufferedImage, double angle) {
|
|
|
+ Rectangle rectangle = getRotateRectangle(bufferedImage.getWidth(), bufferedImage.getHeight(), angle);
|
|
|
+ BufferedImage image = new BufferedImage((int) rectangle.getWidth(), (int) rectangle.getHeight(), BufferedImage.TYPE_4BYTE_ABGR);
|
|
|
Graphics2D graphics2D = image.createGraphics();
|
|
|
- graphics2D.translate((rectangle.getWidth() - bufferedImage.getWidth()) / 2,(rectangle.getHeight() - bufferedImage.getHeight()) / 2);
|
|
|
- graphics2D.rotate(Math.toRadians(angle), BigDecimal.valueOf(bufferedImage.getWidth()/2).doubleValue(),BigDecimal.valueOf(bufferedImage.getHeight()/2).doubleValue());
|
|
|
- graphics2D.drawImage(bufferedImage,null,0,0);
|
|
|
+ graphics2D.translate((rectangle.getWidth() - bufferedImage.getWidth()) / 2, (rectangle.getHeight() - bufferedImage.getHeight()) / 2);
|
|
|
+ graphics2D.rotate(Math.toRadians(angle), BigDecimal.valueOf(bufferedImage.getWidth() / 2).doubleValue(), BigDecimal.valueOf(bufferedImage.getHeight() / 2).doubleValue());
|
|
|
+ graphics2D.drawImage(bufferedImage, null, 0, 0);
|
|
|
return image;
|
|
|
}
|
|
|
|
|
|
|
|
|
- public static Rectangle getRotateRectangle(double width,double height,double angle){
|
|
|
+ public static Rectangle getRotateRectangle(double width, double height, double angle) {
|
|
|
if (angle >= 90) {
|
|
|
- if((int) angle / 90 % 2 == 1){
|
|
|
+ if ((int) angle / 90 % 2 == 1) {
|
|
|
double temp = height;
|
|
|
height = width;
|
|
|
width = temp;
|
|
|
@@ -528,43 +531,44 @@ public class ImageUtils {
|
|
|
}
|
|
|
double j1Angle = Math.atan(height / width);
|
|
|
angle = Math.toRadians(angle);
|
|
|
- double r = Math.sqrt(Math.pow(width,2)+Math.pow(height,2)) / 2;
|
|
|
+ double r = Math.sqrt(Math.pow(width, 2) + Math.pow(height, 2)) / 2;
|
|
|
double newWidth = Math.cos(j1Angle - angle) * r * 2;
|
|
|
double newHeight = Math.sin(j1Angle + angle) * r * 2;
|
|
|
newHeight = Math.abs(newHeight);
|
|
|
newWidth = Math.abs(newWidth);
|
|
|
- return new Rectangle(0,0,(int) newWidth,(int) newHeight);
|
|
|
+ return new Rectangle(0, 0, (int) newWidth, (int) newHeight);
|
|
|
}
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 解析16进制颜色
|
|
|
+ *
|
|
|
* @param value
|
|
|
* @return
|
|
|
*/
|
|
|
- public static Color getColor(String value){
|
|
|
- if(!value.startsWith("#")){
|
|
|
+ public static Color getColor(String value) {
|
|
|
+ if (!value.startsWith("#")) {
|
|
|
throw new RuntimeException("格式错误");
|
|
|
}
|
|
|
- return new Color(Integer.parseInt(value.substring(1,3),16),Integer.parseInt(value.substring(3,5),16),Integer.parseInt(value.substring(5,7),16));
|
|
|
+ return new Color(Integer.parseInt(value.substring(1, 3), 16), Integer.parseInt(value.substring(3, 5), 16), Integer.parseInt(value.substring(5, 7), 16));
|
|
|
}
|
|
|
|
|
|
- public static Color getColor(String value,Integer alpha){
|
|
|
- if(alpha == null){
|
|
|
+ public static Color getColor(String value, Integer alpha) {
|
|
|
+ if (alpha == null) {
|
|
|
return getColor(value);
|
|
|
}
|
|
|
- if(!value.startsWith("#")){
|
|
|
+ if (!value.startsWith("#")) {
|
|
|
throw new RuntimeException("格式错误");
|
|
|
}
|
|
|
- return new Color(Integer.parseInt(value.substring(1,3),16),Integer.parseInt(value.substring(3,5),16),Integer.parseInt(value.substring(5,7),16),alpha);
|
|
|
+ return new Color(Integer.parseInt(value.substring(1, 3), 16), Integer.parseInt(value.substring(3, 5), 16), Integer.parseInt(value.substring(5, 7), 16), alpha);
|
|
|
}
|
|
|
|
|
|
|
|
|
- public static FontMetrics getFontMetrics(Font font){
|
|
|
+ public static FontMetrics getFontMetrics(Font font) {
|
|
|
return J_LABEL.getFontMetrics(font);
|
|
|
}
|
|
|
|
|
|
- public static class LineText{
|
|
|
+ public static class LineText {
|
|
|
|
|
|
private Integer pointX;
|
|
|
|
|
|
@@ -576,17 +580,17 @@ public class ImageUtils {
|
|
|
|
|
|
private List<CharText> charTextList;
|
|
|
|
|
|
- public LineText(Integer pointX,Integer width,Integer pointY,String text,Integer length,List<CharText> charTextList,Integer type){
|
|
|
+ public LineText(Integer pointX, Integer width, Integer pointY, String text, Integer length, List<CharText> charTextList, Integer type) {
|
|
|
this.text = text;
|
|
|
this.length = length;
|
|
|
this.pointY = pointY;
|
|
|
- if(type.equals(1)){
|
|
|
+ if (type.equals(1)) {
|
|
|
this.pointX = pointX;
|
|
|
- }else if(type.equals(2)){
|
|
|
- this.pointX = pointX+(width - length)/2;
|
|
|
- }else if(type.equals(3)){
|
|
|
+ } else if (type.equals(2)) {
|
|
|
+ this.pointX = pointX + (width - length) / 2;
|
|
|
+ } else if (type.equals(3)) {
|
|
|
this.pointX = pointX + width - length;
|
|
|
- }else {
|
|
|
+ } else {
|
|
|
throw new RuntimeException("不支持的类型");
|
|
|
}
|
|
|
charTextList.forEach(charText -> {
|
|
|
@@ -636,7 +640,7 @@ public class ImageUtils {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public static class CharText{
|
|
|
+ public static class CharText {
|
|
|
|
|
|
private Integer pointX;
|
|
|
|
|
|
@@ -644,7 +648,7 @@ public class ImageUtils {
|
|
|
|
|
|
private Character value;
|
|
|
|
|
|
- public CharText(Integer pointX,Integer pointY,Character value){
|
|
|
+ public CharText(Integer pointX, Integer pointY, Character value) {
|
|
|
this.pointX = pointX;
|
|
|
this.pointY = pointY;
|
|
|
this.value = value;
|