📄 imageutils.java
字号:
package com.set.utils;
import javax.swing.ImageIcon;
import java.awt.image.BufferedImage;
import java.awt.image.CropImageFilter;
import java.awt.image.FilteredImageSource;
import java.awt.image.ImageFilter;
import java.awt.image.ImageProducer;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import com.sun.org.apache.bcel.internal.verifier.structurals.Frame;
import java.awt.*;
import java.io.*;
import com.sun.image.codec.jpeg.*;
public class ImageUtils {
/**
*
* @param filePath
* @param watermark
* @return
*/
public static boolean createMark(String filePath, String watermark,
String text) {
ImageIcon imgIcon = new ImageIcon(filePath);
Image theImg = imgIcon.getImage();
ImageIcon waterIcon = new ImageIcon(watermark);
Image waterImg = waterIcon.getImage();
int width = theImg.getWidth(null);
int height = theImg.getHeight(null);
BufferedImage bimage = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
Graphics2D g = bimage.createGraphics();
g.setColor(Color.red);
g.setBackground(Color.white);
g.drawImage(theImg, 0, 0, null);
g.drawImage(waterImg, 100, 100, null);
g.drawString(text, 10, 10); // 添加文字
g.dispose();
try {
FileOutputStream out = new FileOutputStream(filePath);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bimage);
param.setQuality(0.5f, true);
encoder.encode(bimage, param);
out.close();
} catch (Exception e) {
return false;
}
return true;
}
public static boolean createMark(String filePath, String text) {
ImageIcon imgIcon = new ImageIcon(filePath);
Image theImg = imgIcon.getImage();
int width = theImg.getWidth(null);
int height = theImg.getHeight(null);
BufferedImage bimage = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
Graphics2D g = bimage.createGraphics();
g.setColor(Color.LIGHT_GRAY);
g.setFont(new Font("serif", Font.PLAIN, 18));
g.setBackground(Color.white);
g.drawImage(theImg, 0, 0, null);
g.drawString(text, width / 2 - 60, height / 2); // 添加文字,居中显示
g.dispose();
try {
FileOutputStream out = new FileOutputStream(filePath);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bimage);
param.setQuality(0.5f, true);
encoder.encode(bimage, param);
out.close();
} catch (Exception e) {
return false;
}
return true;
}
/**
* 等比压缩图片,形成新的缩略图,如果图片宽度小于预定值,则不进行压缩
*
* @param fileName
* String
* @throws Exception
*/
public static void zipImage(String fileName, String destFileName,
int defaultWidth) throws Exception {
File _file = new File(fileName); // 读入文件
Image src = javax.imageio.ImageIO.read(_file); // 构造Image对象
int width = src.getWidth(null); // 得到源图宽
int height = src.getHeight(null); // 得到源图长
if (width > defaultWidth) {
BufferedImage tag = new BufferedImage(defaultWidth,
(height * defaultWidth) / width, BufferedImage.TYPE_INT_RGB);
tag.getGraphics().drawImage(src, 0, 0, defaultWidth,
(height * defaultWidth) / width, null); // 绘制缩小后的图
FileOutputStream out = new FileOutputStream(destFileName); // 输出到文件流
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(tag); // 近JPEG编码
// System.out.print(wideth + "*" + height);
out.close();
} else {
// 将原来的文件拷贝到目标目录文件
FileUtils.copyFile(fileName, destFileName);
}
}
/**
* 定宽/定高
*
* @param fileName
* String
* @param destFileName
* String
* @param defaultWidth
* int
* @param defaultHeight
* int
* @throws Exception
*/
public static void zipImage(String fileName, String destFileName,
int defaultWidth, int defaultHeight) throws Exception {
File _file = new File(fileName); // 读入文件
Image src = javax.imageio.ImageIO.read(_file); // 构造Image对象
int width = src.getWidth(null); // 得到源图宽
int height = src.getHeight(null); // 得到源图长
int newWidth, newHeight;
if (width > defaultWidth || height > defaultHeight) {
if (width >= height) {
newHeight = (height * defaultWidth) / width;
newWidth = defaultWidth;
} else {
newWidth = (width * defaultHeight) / height;
newHeight = defaultHeight;
}
BufferedImage tag = new BufferedImage(newWidth, newHeight,
BufferedImage.TYPE_INT_RGB);
tag.getGraphics().drawImage(src, 0, 0, newWidth, newHeight, null); // 绘制缩小后的图
FileOutputStream out = new FileOutputStream(destFileName); // 输出到文件流
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(tag); // 近JPEG编码
// System.out.print(wideth + "*" + height);
out.close();
} else {
// 将原来的文件拷贝到目标目录文件
FileUtils.copyFile(fileName, destFileName);
}
}
public static double zipImage(String fileName, String destFileName,
int defaultWidth, int defaultHeight, int currentWidth,
int currentHeight, int x, int y) {
try {
File _file = new File(fileName); // 读入文件
Image sourceImage = javax.imageio.ImageIO.read(_file);
int newWidth = 0, newHeight = 0;
/*
* 获得压缩或放大后的图片,替换掉原图片
*/
BufferedImage tag = new BufferedImage(currentWidth, currentHeight,
BufferedImage.TYPE_INT_RGB);
tag.getGraphics().drawImage(sourceImage, 0, 0, currentWidth,
currentHeight, null); // 绘制缩小后的图
FileOutputStream out = new FileOutputStream(fileName); // 输出到文件流
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(tag); // 近JPEG编码
out.close();
if (currentWidth > defaultWidth || currentHeight > defaultHeight) {
if (currentWidth >= defaultWidth + x) {
newWidth = defaultWidth;
} else {
newWidth = currentWidth - x;
}
if (currentHeight >= defaultHeight + y) {
newHeight = defaultHeight;
} else {
newHeight = currentHeight - y;
}
ImageFilter filter = new CropImageFilter(x, y, newWidth,
newHeight);// 根据图像裁剪过滤器产生过滤器
// 下面根据过滤器产生图像生产者
File file = new File(fileName); // 读入文件
Image src = javax.imageio.ImageIO.read(file);
ImageProducer producer = new FilteredImageSource(src
.getSource(), filter);
Image img = Toolkit.getDefaultToolkit().createImage(producer);// 根据图像生产者产生新图像
BufferedImage bi = new BufferedImage(currentWidth,
currentHeight, BufferedImage.TYPE_INT_RGB).getSubimage(
0, 0, newWidth, newHeight);
bi.getGraphics()
.drawImage(img, 0, 0, newWidth, newHeight, null);
FileOutputStream outs = new FileOutputStream(destFileName); // 输出到文件流
JPEGImageEncoder iencoder = JPEGCodec.createJPEGEncoder(outs);
iencoder.encode(bi); // 近JPEG编码
outs.close();
} else {
// 将原来的文件拷贝到目标目录文件
FileUtils.copyFile(fileName, destFileName);
}
File f = new File(destFileName);
return f.length();
} catch (IOException e) {
e.printStackTrace();
}
return 0;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -