public class ImageUtil extends Object
| 构造器和说明 |
|---|
ImageUtil() |
| 限定符和类型 | 方法和说明 |
|---|---|
static void |
addText(String destImage,
String text) |
static void |
compressImage(String imagePath,
float quality) |
static BufferedImage |
createHeadlessBufferedImage(BufferedImage image,
String ext,
int width,
int height)
Creates a
BufferedImage from an Image. |
static BufferedImage |
createHeadlessSmoothBufferedImage(BufferedImage source,
String ext,
int width,
int height)
Creates a
BufferedImage from an Image. |
static void |
cutImage(InputStream input,
OutputStream out,
int x,
int y,
int width,
int height,
String imageType) |
static void |
cutJPG(InputStream input,
OutputStream out,
int x,
int y,
int width,
int height) |
static void |
cutPNG(InputStream input,
OutputStream out,
int x,
int y,
int width,
int height) |
static Graphics2D |
drawAL(int sx,
int sy,
int ex,
int ey,
Graphics2D g2) |
static boolean |
fromBase64(String imgStr,
String imgFilePath)
字符串进行Base64解码并生成图片
|
static String |
getImageExtension(String imagePath) |
static int |
getLength(String text)
获取字符长度,一个汉字作为 1 个字符, 一个英文字母作为 0.5 个字符
|
static String |
getSuffix(String fileName,
String defaultExt) |
static boolean |
hasAlpha(Image image)
Determines if the image has transparent pixels.
|
static void |
pressImage(String targetImg,
String waterImg,
int x,
int y,
float alpha)
添加图片水印
|
static void |
pressText(String targetImg,
String pressText,
String fontName,
int fontStyle,
int fontSize,
Color color,
int x,
int y,
float alpha)
添加文字水印
|
static void |
resize(String filePath,
int height,
int width,
boolean bb)
图片缩放
|
static BufferedImage |
resizeImage(BufferedImage image,
String ext,
int maxWidth,
int maxHeight)
Resizes an image.
|
static BufferedImage |
resizeImage(String imgName,
String ext,
int maxWidth,
int maxHeight)
Resizes an image
|
static BufferedImage |
rotate(BufferedImage image,
int degree,
Color bgcolor) |
static InputStream |
rotateImg(BufferedImage image,
int degree,
Color bgcolor) |
static void |
saveCompressedImage(BufferedImage image,
String toFileName,
String ext)
Compress and save an image to the disk.
|
static boolean |
saveImage(BufferedImage image,
String toFileName,
String ext)
Saves an image to the disk.
|
static void |
scaleByWidth(String filePath,
int newWidth) |
static String |
toBase64(String imgFilePath)
将图片文件 Base64 String
|
public static BufferedImage resizeImage(String imgName, String ext, int maxWidth, int maxHeight) throws IOException
imgName - The image name to resize. Must be the complet path to the filetype - intmaxWidth - The image's max widthmaxHeight - The image's max heightBufferedImageIOExceptionpublic static BufferedImage resizeImage(BufferedImage image, String ext, int maxWidth, int maxHeight)
image - The image to resizemaxWidth - The image's max widthmaxHeight - The image's max heighttype - intBufferedImagepublic static boolean saveImage(BufferedImage image, String toFileName, String ext) throws IOException
image - The image to savetoFileName - The filename to usetype - The image type. Use ImageUtils.IMAGE_JPEG to save
as JPEG images, or ImageUtils.IMAGE_PNG to save
as PNG.false if no appropriate writer is foundIOExceptionpublic static void saveCompressedImage(BufferedImage image, String toFileName, String ext) throws IOException
image - The image to savetoFileName - The filename to usetype - The image type. Use ImageUtils.IMAGE_JPEG to save
as JPEG images, or ImageUtils.IMAGE_PNG to save
as PNG.IOExceptionpublic static void compressImage(String imagePath, float quality) throws IOException
IOExceptionpublic static BufferedImage createHeadlessBufferedImage(BufferedImage image, String ext, int width, int height)
BufferedImage from an Image. This
method can function on a completely headless system. This especially
includes Linux and Unix systems that do not have the X11 libraries
installed, which are required for the AWT subsystem to operate. This
method uses nearest neighbor approximation, so it's quite fast.
Unfortunately, the result is nowhere near as nice looking as the
createHeadlessSmoothBufferedImage method.image - The image to convertw - The desired image widthh - The desired image heighttype - intpublic static BufferedImage createHeadlessSmoothBufferedImage(BufferedImage source, String ext, int width, int height)
BufferedImage from an Image. This
method can function on a completely headless system. This especially
includes Linux and Unix systems that do not have the X11 libraries
installed, which are required for the AWT subsystem to operate. The
resulting image will be smoothly scaled using bilinear filtering.source - The image to convertw - The desired image widthh - The desired image heighttype - intpublic static boolean hasAlpha(Image image)
image - The image to check for transparent pixel.strue of false, according to the resultpublic static final void pressImage(String targetImg, String waterImg, int x, int y, float alpha)
targetImg - 目标图片路径,如:C://myPictrue//1.jpgwaterImg - 水印图片路径,如:C://myPictrue//logo.pngx - 水印图片距离目标图片左侧的偏移量,如果x<0, 则在正中间y - 水印图片距离目标图片上侧的偏移量,如果y<0, 则在正中间alpha - 透明度(0.0 -- 1.0, 0.0为完全透明,1.0为完全不透明)public static void pressText(String targetImg, String pressText, String fontName, int fontStyle, int fontSize, Color color, int x, int y, float alpha)
targetImg - 目标图片路径,如:C://myPictrue//1.jpgpressText - 水印文字, 如:中国证券网fontName - 字体名称, 如:宋体fontStyle - 字体样式,如:粗体和斜体(Font.BOLD|Font.ITALIC)fontSize - 字体大小,单位为像素color - 字体颜色x - 水印文字距离目标图片左侧的偏移量,如果x<0, 则在正中间y - 水印文字距离目标图片上侧的偏移量,如果y<0, 则在正中间alpha - 透明度(0.0 -- 1.0, 0.0为完全透明,1.0为完全不透明)public static int getLength(String text)
text - public static void resize(String filePath, int height, int width, boolean bb)
filePath - 图片路径height - 高度width - 宽度bb - 比例不对时是否需要补白public static void scaleByWidth(String filePath, int newWidth)
public static BufferedImage rotate(BufferedImage image, int degree, Color bgcolor) throws IOException
IOExceptionpublic static InputStream rotateImg(BufferedImage image, int degree, Color bgcolor) throws IOException
IOExceptionpublic static Graphics2D drawAL(int sx, int sy, int ex, int ey, Graphics2D g2)
public static void addText(String destImage, String text) throws IOException
IOExceptionpublic static String toBase64(String imgFilePath)
imgFilePath - public static boolean fromBase64(String imgStr, String imgFilePath)
imgStr - imgFilePath - public static void cutJPG(InputStream input, OutputStream out, int x, int y, int width, int height)
public static void cutPNG(InputStream input, OutputStream out, int x, int y, int width, int height)
public static void cutImage(InputStream input, OutputStream out, int x, int y, int width, int height, String imageType)
Copyright © 2018. All rights reserved.