十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
把图片按照规定的比例压缩,然后保存至FTP,列表读取缩略图,单击显示原图。
创新互联公司是专业的本溪网站建设公司,本溪接单;提供成都网站设计、网站制作,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行本溪网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!
/**
* 压缩图片方法一(高质量)
* @param oldFile 将要压缩的图片
* @param width 压缩宽
* @param height 压缩高
* @param smallIcon 压缩图片后,添加的扩展名(在图片后缀名前添加)
* @param quality 压缩质量 范围:i0.0-1.0/i 高质量:i0.75/i 中等质量:i0.5/i 低质量:i0.25/i
* @param percentage 是否等比压缩 若true宽高比率将将自动调整
*/
public static void compressImage(String oldFile, int width, int height, String smallIcon,
float quality, boolean percentage) {
try {
File file = new File(oldFile);
// 验证文件是否存在
if(!file.exists())
throw new FileNotFoundException("找不到原图片!");
// 获取图片信息
BufferedImage image = ImageIO.read(file);
int orginalWidth = image.getWidth();
int orginalHeight = image.getHeight();
// 验证压缩图片信息
if (width = 0 || height = 0 || !Pattern.matches("^[1-9]\\d*$", String.valueOf(width))
|| !Pattern.matches("^[1-9]\\d*$", String.valueOf(height)))
throw new Exception("图片压缩后的高宽有误!");
// 等比压缩
if (percentage) {
double rate1 = ((double) orginalWidth) / (double) width + 0.1;
double rate2 = ((double) orginalHeight) / (double) height + 0.1;
double rate = rate1 rate2 ? rate1 : rate2;
width = (int) (((double) orginalWidth) / rate);
height = (int) (((double) orginalHeight) / rate);
}
// 压缩后的文件名
String filePrex = oldFile.substring(0, oldFile.lastIndexOf('.'));
String newImage = filePrex + smallIcon + oldFile.substring(filePrex.length());
// 压缩文件存放位置
File savedFile = new File(newImage);
// 创建一个新的文件
savedFile.createNewFile();
// 创建原图像的缩放版本
Image image2 = image.getScaledInstance(width, height, Image.SCALE_AREA_AVERAGING);
// 创建数据缓冲区图像
BufferedImage bufImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
// 创建一个Graphics2D
Graphics2D g2 = bufImage.createGraphics();
// 重绘图像
g2.drawImage(image2, 0, 0, width, height, null);
g2.dispose();
// 过滤像素矩阵
float[] kernelData = {
-0.125f, -0.125f, -0.125f,
-0.125f, 2, -0.125f, -0.125f,
-0.125f, -0.125f };
Kernel kernel = new Kernel(3, 3, kernelData);
// 按核数学源图像边缘的像素复制为目标中相应的像素输出像素
ConvolveOp cOp = new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP, null);
// 转换像素
bufImage = cOp.filter(bufImage, null);
FileOutputStream out = new FileOutputStream(savedFile);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bufImage);
// 设置压缩质量
param.setQuality(quality, true);
encoder.encode(bufImage, param);
out.close();
System.out.println(newImage);
} catch (Exception e) {
e.printStackTrace();
System.out.println("压缩失败!" + e.getMessage());
}
}
可以使用Draw这个类,通过改变像素来改变存储大小,实例如下:
public static boolean compressPic(String srcFilePath, String descFilePath) throws IOException {
File file = null;
BufferedImage src = null;
FileOutputStream out = null;
ImageWriter imgWrier;
ImageWriteParam imgWriteParams;
// 指定写图片的方式为 jpg
imgWrier = ImageIO.getImageWritersByFormatName("jpg").next();
imgWriteParams = new javax.imageio.plugins.jpeg.JPEGImageWriteParam(
null);
// 要使用压缩,必须指定压缩方式为MODE_EXPLICIT
imgWriteParams.setCompressionMode(imgWriteParams.MODE_EXPLICIT);
// 这里指定压缩的程度,参数qality是取值0~1范围内,
imgWriteParams.setCompressionQuality((float) 1);
imgWriteParams.setProgressiveMode(imgWriteParams.MODE_DISABLED);
ColorModel colorModel =ImageIO.read(new File(srcFilePath)).getColorModel();// ColorModel.getRGBdefault();
// 指定压缩时使用的色彩模式
// imgWriteParams.setDestinationType(new javax.imageio.ImageTypeSpecifier(
// colorModel, colorModel.createCompatibleSampleModel(16, 16)));
imgWriteParams.setDestinationType(new javax.imageio.ImageTypeSpecifier(
colorModel, colorModel.createCompatibleSampleModel(16, 16)));
try {
if (isBlank(srcFilePath)) {
return false;
} else {
file = new File(srcFilePath);System.out.println(file.length());
src = ImageIO.read(file);
out = new FileOutputStream(descFilePath);
imgWrier.reset();
// 必须先指定 out值,才能调用write方法, ImageOutputStream可以通过任何
// OutputStream构造
imgWrier.setOutput(ImageIO.createImageOutputStream(out));
// 调用write方法,就可以向输入流写图片
imgWrier.write(null, new IIOImage(src, null, null),
imgWriteParams);
out.flush();
out.close();
}
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
public static boolean isBlank(String string) {
if (string == null || string.length() == 0 || string.trim().equals("")) {
return true;
}
return false;
}
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
public class ReadBook extends JFrame {
JTextArea jta;
JTextField jtf;
JButton jb;
public ReadBook() {
jta = new JTextArea();
jtf = new JTextField(30);
jtf.setText("文件保存路径如c:\\ab.txt");
jb = new JButton("保存文字");
JPanel jp = new JPanel();
jp.add(jtf);
jp.add(jb);
add(jta);
add(jp, BorderLayout.SOUTH);
setBounds(500, 100, 500, 380);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
jb.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//-------------核心代码---------
String path = jtf.getText();
File f = new File(path);
String txt = jta.getText().replaceAll("\n", "\r\n");
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(f));
bw.write(txt);//写入文件中
bw.close();
} catch (Exception e1) {
e1.printStackTrace();
}
//-------------核心代码---------
}
});
}
public static void main(String[] args) {
new ReadBook();
}
}