java图片压缩库-java图片压缩比为1
java如何实现把一个大图片压缩到指定大小的图片且长宽比不变?

java要实现把一个大图片压缩到指定大小的图片且长宽比不变可以尝试以下操作:
建立一个AffineTransform
AffineTransform(double m00, double m10, double m01, double m11, double m02, double m12)
转换矩阵,缩放比较简单(矩阵可以干很多事情,想做图像处理软件可以研究下)
[ x'] [ m00 m01 m02 ] [ x ] [ m00x + m01y + m02 ]
[ y'] = [ m10 m11 m12 ] [ y ] = [ m10x + m11y + m12 ]
[ 1 ] [ 0 0 1 ] [ 1 ] [ 1 ]
10倍比较难算(根号10啊,当然你想算也行),9倍好点(9的开方是3),m00为1/3,m01为0,m02为0,m10为0,m11为1/3,m12为0。
再建一个AffineTransformOp,把上面的转换传进去
AffineTransformOp(AffineTransform xform, int interpolationType)
最后调用AffineTransformOp的BufferedImage filter(BufferedImage src, BufferedImage dst) ,src传原图片,返回值就是想要的Image,注意是返回值,不是dst,不明白可以看下Java API
java图片压缩比为1

java压缩图片,按照比例进行压缩
public static void main(String[] args) {
try {
//图片所在路径
BufferedImage templateImage = (new File("C:\\Users\\晏丁丁\\Pictures\\图片1.png"));
//原始图片的长度和宽度
int height = ();
int width = ();
//通过比例压缩
float scale = 0.5f;
//通过固定长度压缩
/*int doWithHeight = 100;
int dowithWidth = 300;*/
//压缩之后的长度和宽度
int doWithHeight = (int) (scale * height);
int dowithWidth = (int) (scale * width);
BufferedImage finalImage = new BufferedImage(dowithWidth, doWithHeight, _INT_RGB);
().drawImage((dowithWidth, doWithHeight, _SMOOTH), 0, 0, null);
//图片输出路径,以及图片名
FileOutputStream fileOutputStream = new FileOutputStream("D:/image/");
JPEGImageEncoder encoder = (fileOutputStream);
(finalImage);
();
} catch (IOException e) {
e.printStackTrace();
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
文章知
java如何实现压缩图片且保留图片原文件属性,比如拍摄日期

import ;
import ;
import ;
import ;
import ;
import ;public class Zip {
// 压缩
public static void zip(String zipFileName, String inputFile)
throws Exception {
File f = new File(inputFile);
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(
zipFileName));
zip(out, f, null);
("zip done");
();
} private static void zip(ZipOutputStream out, File f, String base)
throws Exception {
("zipping " + f.getAbsolutePath());
if (f != null && f.isDirectory()) {
File[] fc = f.listFiles();
if (base != null)
(new ZipEntry(base + "/"));
base = base == null ? "" : base + "/";
for (int i = 0; i < ; i++) {
zip(out, fc[i], base + fc[i].getName());
}
} else {
(new ZipEntry(base));
FileInputStream in = new FileInputStream(f);
int b;
while ((b = ()) != -1)
(b);
();
}
} // 解压
public static void unzip(String zipFileName, String outputDirectory)
throws Exception {
ZipInputStream in = new ZipInputStream(new FileInputStream(zipFileName));
ZipEntry z;
while ((z = ()) != null) {
String name = z.getName();
if (z.isDirectory()) {
name = (0, () - 1);
File f = new File(outputDirectory + + name);
f.mkdir();
("MD " + outputDirectory +
+ name);
} else {
("unziping " + z.getName());
File f = new File(outputDirectory + + name);
f.createNewFile();
FileOutputStream out = new FileOutputStream(f);
int b;
while ((b = ()) != -1)
(b);
();
}
}
();
} public void deleteFolder(File dir) {
File filelist[] = ();
int listlen = ;
for (int i = 0; i < listlen; i++) {
if (filelist[i].isDirectory()) {
deleteFolder(filelist[i]);
} else {
filelist[i].delete();
}
}
();// 删除当前目录
} public static void main(String[] args) {
try {
// TestZip t = new TestZip();
// t.zip("c:\\","c:\\test");
// t.unzip("c:\\","c:\\test2");
} catch (Exception e) {
e.printStackTrace();
}
}
}
-
果盘绘画图片大全简单创作方法快速制作技巧2024-01-02 16:57:54
-
小年插画图片创作方法快速制作技巧2024-01-02 16:57:20
-
小年绘画作品制作技巧2024-01-02 16:55:45
-
小年插画图片创作方法快速制作技巧2024-01-02 16:55:02
-
立春插画图片如何制作2024-01-02 16:54:19
-
立春插画图片创作方法快速制作技巧2024-01-02 16:53:32






扫描二维码添加客服微信
扫描二维码关注