📄 imgutil.java
字号:
package com.comm.util;
import java.awt.*;
import java.awt.image.*;
import java.io.*;
import java.text.*;
import java.util.*;
import javax.imageio.*;
import javax.swing.*;
import com.sun.image.codec.jpeg.*;
public class ImgUtil {
private BufferedImage bimage;
private int width;
private int height;
public ImgUtil() {}
public ImgUtil(String filename) {
try {
bimage = ImageIO.read(new File(filename));
} catch (Exception ex) {
throw new RuntimeException(ex);
}
height = bimage.getHeight();
width = bimage.getWidth();
}
public void makeThumbnail(String filename, int maxWidth, int maxHeight) throws
IOException {
double scale = cScale(width, height, maxWidth, maxHeight);
BufferedImage bimage2 = new BufferedImage((int) ((double) width * scale),
(int) ((double) height *
scale), 1);
Graphics2D g2d = (Graphics2D) bimage2.getGraphics();
g2d.scale(scale, scale);
g2d.drawImage(bimage, 0, 0, new Frame());
ImageIO.write(bimage2, "jpg", new File(filename));
// System.out.println("in makeThumbnail");
// double scale = cScale(width, height, maxWidth, maxHeight);
// Image Itemp = bimage.getScaledInstance(maxWidth, maxHeight,
// bimage.SCALE_SMOOTH);
// System.out.println("after getScaledInstance");
// AffineTransformOp op = new AffineTransformOp(AffineTransform.
// getScaleInstance(scale, scale), null);
// Itemp = op.filter(bimage, null);
// ImageIO.write((BufferedImage) Itemp, "jpg", new File(filename));
}
public static double cScale(int w, int h, int maxWidth, int maxHeight) {
double scale = 1.0D;
scale = (double) maxWidth / (double) w >=
(double) maxHeight / (double) h ?
(double) maxHeight / (double) h :
(double) maxWidth / (double) w;
return scale;
}
public int getHeight() {
return height;
}
public int getWidth() {
return width;
}
public static String noNull(String p) {
return p != null ? p.trim() : "";
}
public static String getCurrentDate(Date d) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
return sdf.format(d);
}
public static int parseInt(String p, int d) {
int result = d;
if (p != null) {
try {
result = Integer.parseInt(p.trim());
} catch (Exception e) {}
}
return result;
}
//生成缩略图
public void reduce(String srcFileName, String tagFileName, int maxWidth,
int maxHeight) {
//设置新文件的宽和高
ImageIcon srcFile = new ImageIcon(srcFileName); //读入源文件
double scale = cScale(srcFile.getIconWidth(), srcFile.getIconHeight(),
maxWidth, maxHeight);
int tagImageWidth = (int) ((double) srcFile.getIconWidth() * scale);
int tagImageHeight = (int) ((double) srcFile.getIconHeight() * scale);
Image srcImage = srcFile.getImage(); //构造Image对象
BufferedImage tagImage = new BufferedImage(tagImageWidth,
tagImageHeight, BufferedImage.TYPE_INT_RGB); //构造图像缓存对象
tagImage.getGraphics().drawImage(srcImage, 0, 0, tagImageWidth,
tagImageHeight, null); //绘制缩小后的图
paint(tagImage, tagFileName);
}
//生成水印
public void watermark(String srcFileName, String logoFileName, String text,
int watermarkLocationX, int watermarkLocationY,
int wordLocationX, int wordLocationY) {
Color color = Color.BLACK; //前景色
Color backgroundColor = Color.WHITE; //背景色
ImageIcon srcFile = new ImageIcon(srcFileName); //读入源文件
Image srcImage = srcFile.getImage(); //构造Image对象
ImageIcon watermarkFile = new ImageIcon(logoFileName); //读入水印文件
Image watermarkImage = watermarkFile.getImage(); //构造水印图片对象
BufferedImage tagImage = new BufferedImage(srcImage.getWidth(null),
srcImage.getHeight(null), BufferedImage.TYPE_INT_RGB); //构造图像缓存对象
Graphics2D g = tagImage.createGraphics();
g.setColor(color); //设置绘图笔的前景色
g.setBackground(backgroundColor); //设置绘图笔的背景色
g.drawImage(srcImage, 0, 0, null); //先绘制源图
g.drawImage(watermarkImage, watermarkLocationX, watermarkLocationY, null); //添加水印图片
g.drawString(text, wordLocationX, wordLocationY); //添加文字
g.dispose();
paint(tagImage, srcFileName);
}
//生成水印
public void watermark2(String srcFileName, String logoFileName, String text,
int watermarkLocationX, int watermarkLocationY,
int wordLocationX, int wordLocationY) {
Color color = Color.BLACK; //前景色
Color backgroundColor = Color.WHITE; //背景色
ImageIcon srcFile = new ImageIcon(srcFileName); //读入源文件
Image srcImage = srcFile.getImage(); //构造Image对象
ImageIcon watermarkFile = new ImageIcon(logoFileName); //读入水印文件
int w = watermarkFile.getIconWidth();
int h = watermarkFile.getIconHeight();
if (w < 140) {
watermarkLocationX = (140 - w) / 2;
watermarkLocationY = 0;
} else {
watermarkLocationX = 0;
watermarkLocationY = (140 - h) / 2;
}
Image watermarkImage = watermarkFile.getImage(); //构造水印图片对象
BufferedImage tagImage = new BufferedImage(srcImage.getWidth(null),
srcImage.getHeight(null), BufferedImage.TYPE_INT_RGB); //构造图像缓存对象
Graphics2D g = tagImage.createGraphics();
g.setColor(color); //设置绘图笔的前景色
g.setBackground(backgroundColor); //设置绘图笔的背景色
g.drawImage(srcImage, 0, 0, null); //先绘制源图
g.drawImage(watermarkImage, watermarkLocationX, watermarkLocationY, null); //添加水印图片
g.drawString(text, wordLocationX, wordLocationY); //添加文字
g.dispose();
paint(tagImage, logoFileName);
}
private void paint(BufferedImage tagImage, String tagFilename) {
try {
FileOutputStream tagFile = new FileOutputStream(tagFilename);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(tagFile);
JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(tagImage);
param.setQuality(50f, true);
encoder.encode(tagImage, param);
tagFile.close();
} catch (Exception e) {}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -